Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : KA Apify Actors9# File : ka-tiktok/src/main.py10# Module : ka-tiktok11# Repository : https://git.spboucher.ai/ka-apify-actors12# Created : 2026-08-1813# Last Modified : 2026-08-1814#15# =============================================================================16# AUTHOR & PROFESSIONAL AFFILIATION17# =============================================================================18#19# Author : Simon-Pierre Boucher20# Position : Professor of Finance21# Institution : Université du Québec en Outaouais (UQO)22# Department : Département des sciences administratives23# Organization : SP Boucher24#25# Website : https://spboucher.ai26# Git : https://git.spboucher.ai27#28# =============================================================================29# OFFICIAL CONTACT DIRECTORY30# =============================================================================31#32# General : info@spboucher.ai33# Professional : contact@spboucher.ai34# Direct : simon@spboucher.ai35# Research : research@spboucher.ai36# Development : dev@spboucher.ai37# Support : support@spboucher.ai38# Security/Admin : admin@spboucher.ai39# Notifications : notifications@spboucher.ai40#41# Preferred Contact: contact@spboucher.ai42#43# =============================================================================44# COPYRIGHT & INTELLECTUAL PROPERTY45# =============================================================================46#47# Copyright (c) 202648# Simon-Pierre Boucher49#50# All rights reserved where applicable.51#52# This file is governed by the applicable project license,53# copyright notices, institutional agreements and relevant law.54#55# Unless expressly authorized by the applicable license or56# rights holder, copying, redistribution, sublicensing,57# publication and commercial exploitation are not permitted.58#59# Existing third-party rights and notices remain unaffected.60#61# =============================================================================62# CONFIDENTIALITY & SECURITY63# =============================================================================64#65# Classification : Public66# Distribution : Public repository; use is governed by the project license67# Security Contact : admin@spboucher.ai68#69# Access to this source does not independently grant permission70# to redistribute or disclose confidential materials.71#72# Do not embed credentials, private keys, access tokens,73# certificates or confidential personal information.74#75# =============================================================================76# LICENSE & MAINTENANCE77# =============================================================================78#79# License : None specified (no license file in this repository)80# Maintainer : Simon-Pierre Boucher81# Technical Contact: dev@spboucher.ai82#83# =============================================================================84# END OF SP BOUCHER SOURCE HEADER85# =============================================================================8687# ==============================================================================88# Author: Simon-Pierre Boucher <contact@spboucher.ai>89# File: src/main.py (ka-tiktok)90# Desc: Profils TikTok publics détaillés via la page @handle : le JSON SSR91# __UNIVERSAL_DATA_FOR_REHYDRATION__ contient user + stats complètes92# (abonnés, cœurs, vidéos) ; les vidéos récentes sont extraites quand93# TikTok les inclut dans le SSR (ItemModule / itemList).94# ==============================================================================95from __future__ import annotations9697import asyncio98import json99import re100101from apify import Actor102103from .net import Fetcher104105PROFILE_URL = "https://www.tiktok.com/@{u}"106_STATE_RE = re.compile(107 r'<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__"[^>]*>(.*?)</script>',108 re.S)109_ITEM_RE = re.compile(r'"ItemModule"\s*:\s*(\{.*?\})\s*,\s*"[A-Z]', re.S)110111112def parse_videos(html: str, data: dict) -> list[dict]:113 """Vidéos récentes si le SSR les inclut (souvent absent — best effort)."""114 items: list[dict] = []115 raw: dict = {}116 scope = data.get("__DEFAULT_SCOPE__") or {}117 detail = scope.get("webapp.user-detail") or {}118 for it in (detail.get("itemList") or []):119 raw[it.get("id", str(len(raw)))] = it120 if not raw:121 m = _ITEM_RE.search(html)122 if m:123 try:124 raw = json.loads(m.group(1))125 except Exception:126 raw = {}127 for vid in list(raw.values())[:20]:128 stats = vid.get("stats") or vid.get("statsV2") or {}129 author = vid.get("author")130 handle = author if isinstance(author, str) else \131 (author or {}).get("uniqueId", "")132 items.append({133 "id": vid.get("id"),134 "url": f"https://www.tiktok.com/@{handle}/video/{vid.get('id')}",135 "caption": (vid.get("desc") or "")[:500],136 "views": _num(stats.get("playCount")),137 "likes": _num(stats.get("diggCount")),138 "comments": _num(stats.get("commentCount")),139 "shares": _num(stats.get("shareCount")),140 "timestamp": vid.get("createTime"),141 })142 return items143144145def _num(v):146 try:147 return int(v)148 except (TypeError, ValueError):149 return None150151152async def main() -> None:153 async with Actor:154 inp = await Actor.get_input() or {}155 usernames = [u.strip().lstrip("@").lower()156 for u in (inp.get("usernames") or []) if u and u.strip()]157 proxy = await Actor.create_proxy_configuration(158 actor_proxy_input=inp.get("proxyConfiguration"))159 fetcher = Fetcher(proxy, delay=float(inp.get("requestDelay") or 1))160 sem = asyncio.Semaphore(int(inp.get("concurrency") or 3))161162 async def one(u: str) -> None:163 async with sem:164 miss = {"kind": "profile", "platform": "tiktok",165 "username": u, "found": False}166 resp = None167 m = None168 for attempt in range(3): # coquille sans SSR → nouvelle IP169 try:170 resp = await fetcher.get(PROFILE_URL.format(u=u),171 session_id=f"{u}t{attempt}")172 except Exception as exc:173 await Actor.push_data({**miss,174 "error": str(exc)[:200]})175 return176 if resp.status_code == 404:177 await Actor.push_data({**miss, "error": "http_404"})178 return179 m = _STATE_RE.search(resp.text or "")180 if m and "webapp.user-detail" in (resp.text or ""):181 break182 info = None183 data: dict = {}184 if m:185 try:186 data = json.loads(m.group(1))187 info = ((data.get("__DEFAULT_SCOPE__") or {})188 .get("webapp.user-detail") or {}).get("userInfo")189 except Exception:190 info = None191 if not info or not (info.get("user") or {}).get("uniqueId"):192 await Actor.push_data(193 {**miss, "error": f"shell_page_{resp.status_code}"})194 return195 user = info.get("user") or {}196 stats = info.get("statsV2") or info.get("stats") or {}197 if user.get("privateAccount"):198 await Actor.push_data({**miss, "error": "private"})199 return200 followers = _num(stats.get("followerCount"))201 videos = parse_videos(resp.text, data)202 views = [v["views"] for v in videos203 if isinstance(v["views"], int)]204 likes = [v["likes"] for v in videos205 if isinstance(v["likes"], int)]206 comments = [v["comments"] for v in videos207 if isinstance(v["comments"], int)]208 shares = [v["shares"] for v in videos209 if isinstance(v["shares"], int)]210 avg_views = round(sum(views) / len(views)) if views else None211 avg_likes = round(sum(likes) / len(likes)) if likes else None212 engagement = None213 if followers and avg_likes is not None:214 engagement = round(215 (avg_likes216 + (round(sum(comments) / len(comments))217 if comments else 0)218 + (round(sum(shares) / len(shares))219 if shares else 0)) / followers * 100, 3)220 top_video = max(221 (v for v in videos if isinstance(v["views"], int)),222 key=lambda v: v["views"], default=None)223 commerce = user.get("commerceUserInfo") or {}224 bio_link = ((user.get("bioLink") or {}).get("link")225 or "").strip()226 total_likes = _num(stats.get("heartCount")227 or stats.get("heart"))228 videos_count = _num(stats.get("videoCount"))229 await Actor.push_data({230 "kind": "profile",231 "platform": "tiktok",232 "found": True,233 "id": user.get("id"),234 "sec_uid": user.get("secUid"),235 "username": user.get("uniqueId"),236 "full_name": user.get("nickname"),237 "biography": user.get("signature"),238 "bio_link": bio_link,239 "followers": followers,240 "following": _num(stats.get("followingCount")),241 "friends": _num(stats.get("friendCount")),242 "total_likes": total_likes,243 "videos_count": videos_count,244 "avg_likes_per_video_lifetime": (245 round(total_likes / videos_count)246 if total_likes and videos_count else None),247 "is_verified": bool(user.get("verified")),248 "is_organization": bool(user.get("isOrganization")),249 "is_seller": bool(user.get("ttSeller")),250 "is_live_now": bool(user.get("roomId")),251 "commerce_category": (commerce.get("category") or None252 if commerce.get("commerceUser")253 else None),254 "account_created_at": user.get("createTime"),255 "region": user.get("region"),256 "language": user.get("language"),257 "avatar": user.get("avatarLarger")258 or user.get("avatarMedium"),259 # agrégats sur les vidéos présentes dans le SSR260 "avg_views": avg_views,261 "avg_likes": avg_likes,262 "avg_comments": (round(sum(comments) / len(comments))263 if comments else None),264 "avg_shares": (round(sum(shares) / len(shares))265 if shares else None),266 "engagement_rate_pct": engagement,267 "top_video": ({"url": top_video["url"],268 "views": top_video["views"],269 "likes": top_video["likes"]}270 if top_video else None),271 "recent_videos": videos,272 })273274 await asyncio.gather(*[one(u) for u in usernames])275 Actor.log.info(f"terminé : {len(usernames)} profils")276