SPB Git

Repository Public

main.py 4.9 KB 122 lines python

1# ==============================================================================2# Author: Simon-Pierre Boucher <contact@spboucher.ai>3# File:   src/main.py (ka-facebook)4# Desc:   Pages Facebook publiques — extraction best-effort du HTML servi aux5#         navigateurs non connectés : compteur d'abonnés (JSON embarqué6#         follower_count / texte « N followers/abonnés »), nom (og:title),7#         avatar (og:image), catégorie. EXPÉRIMENTAL : FB mure agressivement ;8#         un résultat found=False n'est pas une erreur du pipeline.9# ==============================================================================10from __future__ import annotations1112import asyncio13import html as htmllib14import re1516from apify import Actor1718from .net import Fetcher1920PAGE_URL = "https://www.facebook.com/{u}"2122_OG_RE = {23    "title": re.compile(r'<meta property="og:title" content="([^"]*)"'),24    "image": re.compile(r'<meta property="og:image" content="([^"]*)"'),25    "description": re.compile(26        r'<meta (?:property="og:description"|name="description") '27        r'content="([^"]*)"'),28}29_FOLLOWER_RES = (30    re.compile(r'"follower_count"\s*:\s*(\d+)'),31    re.compile(r'"global_likers_count"\s*:\s*(\d+)'),32)33# texte « 1,2 M followers » / « 12 k abonnés » / « 4 016 971 mentions J’aime »34_TEXT_COUNT_RE = re.compile(35    r'([\d][\d\s  .,]*)\s*([KkMm]?)\s*'36    r'(?:followers|abonnés|mentions\s+J’aime|J’aime|likes)',37    re.I)38_CATEGORY_RE = re.compile(r'"category_name"\s*:\s*"([^"]+)"')39_VERIFIED_RE = re.compile(r'"is_verified"\s*:\s*(true|false)')404142def parse_text_count(text: str) -> int | None:43    m = _TEXT_COUNT_RE.search(text or "")44    if not m:45        return None46    num = m.group(1).replace(" ", "").replace(" ", "").replace(",", ".")47    try:48        val = float(num)49    except ValueError:50        return None51    unit = (m.group(2) or "").lower()52    if unit == "k":53        val *= 1_00054    elif unit == "m":55        val *= 1_000_00056    return int(val)575859async def main() -> None:60    async with Actor:61        inp = await Actor.get_input() or {}62        usernames = []63        for raw in (inp.get("usernames") or []):64            raw = (raw or "").strip()65            if not raw:66                continue67            raw = re.sub(r"^https?://(www\.|m\.)?facebook\.com/", "", raw)68            usernames.append(raw.strip("/").split("?")[0])69        proxy = await Actor.create_proxy_configuration(70            actor_proxy_input=inp.get("proxyConfiguration"))71        fetcher = Fetcher(proxy, delay=float(inp.get("requestDelay") or 2))72        sem = asyncio.Semaphore(int(inp.get("concurrency") or 2))7374        async def one(u: str) -> None:75            async with sem:76                miss = {"kind": "profile", "platform": "facebook",77                        "username": u, "found": False}78                try:79                    resp = await fetcher.get(PAGE_URL.format(u=u),80                                             session_id=u)81                except Exception as exc:82                    await Actor.push_data({**miss, "error": str(exc)[:200]})83                    return84                text = resp.text or ""85                if resp.status_code != 200 or len(text) < 5000:86                    await Actor.push_data(87                        {**miss, "error": f"http_{resp.status_code}"})88                    return89                followers = None90                for rx in _FOLLOWER_RES:91                    m = rx.search(text)92                    if m:93                        followers = int(m.group(1))94                        break95                og = {k: htmllib.unescape(m.group(1)) if (m := rx.search(text))96                      else None for k, rx in _OG_RE.items()}97                if followers is None:98                    followers = parse_text_count(og.get("description") or "")99                if followers is None:  # texte « N followers/abonnés » du corps100                    followers = parse_text_count(text)101                if followers is None and not og.get("title"):102                    await Actor.push_data({**miss, "error": "login_wall"})103                    return104                cat = _CATEGORY_RE.search(text)105                ver = _VERIFIED_RE.search(text)106                await Actor.push_data({107                    "kind": "profile",108                    "platform": "facebook",109                    "found": True,110                    "username": u,111                    "full_name": og.get("title"),112                    "biography": og.get("description"),113                    "followers": followers,114                    "category": cat.group(1) if cat else None,115                    "is_verified": (ver.group(1) == "true") if ver else None,116                    "avatar": og.get("image"),117                    "og_description": og.get("description"),118                })119120        await asyncio.gather(*[one(u) for u in usernames])121        Actor.log.info(f"terminé : {len(usernames)} pages")122
Files main
Menu

Theme