Repository Public
1# ==============================================================================2# Author: Simon-Pierre Boucher <contact@spboucher.ai>3# File: src/main.py (ka-onlyfans)4# Desc: Profils OnlyFans publics (page de profil hors connexion) — extraction5# best-effort des compteurs embarqués : posts, photos, vidéos, J'aime6# (favoritedCount), prix d'abonnement, badge, bio, avatar/bannière.7# EXPÉRIMENTAL : OnlyFans change souvent son rendu ; found=False n'est8# pas une erreur du pipeline.9# ==============================================================================10from __future__ import annotations1112import asyncio13import html as htmllib14import json15import re1617from apify import Actor1819from .net import Fetcher2021PROFILE_URL = "https://onlyfans.com/{u}"2223_NUM_RES = {24 "posts_count": re.compile(r'"postsCount"\s*:\s*(\d+)'),25 "photos_count": re.compile(r'"photosCount"\s*:\s*(\d+)'),26 "videos_count": re.compile(r'"videosCount"\s*:\s*(\d+)'),27 "likes": re.compile(r'"favoritedCount"\s*:\s*(\d+)'),28 "streams_count": re.compile(r'"finishedStreamsCount"\s*:\s*(\d+)'),29}30_PRICE_RE = re.compile(r'"subscribePrice"\s*:\s*([\d.]+)')31_STR_RES = {32 "full_name": re.compile(r'"name"\s*:\s*"((?:[^"\\]|\\.)*)"'),33 "biography": re.compile(r'"rawAbout"\s*:\s*"((?:[^"\\]|\\.)*)"'),34 "location": re.compile(r'"location"\s*:\s*"((?:[^"\\]|\\.)*)"'),35 "website": re.compile(r'"website"\s*:\s*"((?:[^"\\]|\\.)*)"'),36 "avatar": re.compile(r'"avatar"\s*:\s*"((?:[^"\\]|\\.)*)"'),37 "banner": re.compile(r'"header"\s*:\s*"((?:[^"\\]|\\.)*)"'),38}39_VERIFIED_RE = re.compile(r'"isVerified"\s*:\s*(true|false)')40_OG_TITLE_RE = re.compile(r'<meta property="og:title" content="([^"]*)"')414243def _dec(raw: str) -> str:44 try:45 return htmllib.unescape(json.loads(f'"{raw}"'))46 except Exception:47 return raw484950async def main() -> None:51 async with Actor:52 inp = await Actor.get_input() or {}53 usernames = [u.strip().lstrip("@").lower()54 for u in (inp.get("usernames") or []) if u and u.strip()]55 proxy = await Actor.create_proxy_configuration(56 actor_proxy_input=inp.get("proxyConfiguration"))57 fetcher = Fetcher(proxy, delay=float(inp.get("requestDelay") or 2))58 sem = asyncio.Semaphore(int(inp.get("concurrency") or 2))5960 async def one(u: str) -> None:61 async with sem:62 miss = {"kind": "profile", "platform": "onlyfans",63 "username": u, "found": False}64 resp = None65 for attempt in range(3): # mur Cloudflare → nouvelle IP66 try:67 resp = await fetcher.get(PROFILE_URL.format(u=u),68 session_id=f"{u}o{attempt}")69 except Exception as exc:70 await Actor.push_data({**miss,71 "error": str(exc)[:200]})72 return73 if resp.status_code == 404:74 await Actor.push_data({**miss, "error": "http_404"})75 return76 if resp.status_code == 200 and \77 "postsCount" in (resp.text or ""):78 break79 text = resp.text or ""80 if resp.status_code != 200 or "postsCount" not in text:81 await Actor.push_data(82 {**miss, "error": f"wall_{resp.status_code}"})83 return84 rec: dict = {"kind": "profile", "platform": "onlyfans",85 "found": True, "username": u}86 for key, rx in _NUM_RES.items():87 m = rx.search(text)88 rec[key] = int(m.group(1)) if m else None89 for key, rx in _STR_RES.items():90 m = rx.search(text)91 rec[key] = _dec(m.group(1)) if m else None92 if rec.get("biography"):93 rec["biography"] = re.sub(r"<[^>]+>", " ",94 rec["biography"])[:1000].strip()95 m = _PRICE_RE.search(text)96 rec["subscribe_price_usd"] = float(m.group(1)) if m else None97 rec["is_free"] = (rec["subscribe_price_usd"] == 0.098 if rec["subscribe_price_usd"] is not None99 else None)100 m = _VERIFIED_RE.search(text)101 rec["is_verified"] = (m.group(1) == "true") if m else None102 if not rec.get("full_name"):103 m = _OG_TITLE_RE.search(text)104 if m:105 rec["full_name"] = htmllib.unescape(106 m.group(1)).split(" OnlyFans")[0].strip()107 # « followers » au sens crea-ka : le compteur public = J'aime108 rec["followers"] = None109 await Actor.push_data(rec)110111 await asyncio.gather(*[one(u) for u in usernames])112 Actor.log.info(f"terminé : {len(usernames)} profils")113