# -*- coding: utf-8 -*- """Clone TiktokAutoUploader vendor + install tiktok-signature npm deps.""" from __future__ import annotations import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] VENDOR = ROOT / "backend" / "third_party" / "TiktokAutoUploader" SIG = VENDOR / "tiktok_uploader" / "tiktok-signature" REPO = "https://github.com/makiisthenes/TiktokAutoUploader.git" def run(cmd: list[str], cwd: Path | None = None) -> None: print("$", " ".join(cmd)) subprocess.check_call(cmd, cwd=str(cwd) if cwd else None) def main() -> int: if not VENDOR.is_dir(): VENDOR.parent.mkdir(parents=True, exist_ok=True) run(["git", "clone", "--depth", "1", REPO, str(VENDOR)]) _patch_vendor_upload() if SIG.is_dir(): run(["npm", "install"], cwd=SIG) run(["npx", "playwright", "install", "chromium"], cwd=SIG) print("OK:", VENDOR) return 0 def _patch_vendor_upload() -> None: """Cookie upload patches: visibility args + no top-level Browser/Video imports.""" path = VENDOR / "tiktok_uploader" / "tiktok.py" if not path.is_file(): return text = path.read_text(encoding="utf-8") changed = False old_imports = ( "from tiktok_uploader.Browser import Browser\n" "from tiktok_uploader.bot_utils import *\n" "from tiktok_uploader import Config, Video, eprint\n" ) new_imports = ( "from tiktok_uploader.bot_utils import *\n" "from tiktok_uploader.Config import Config\n" "from tiktok_uploader.basics import eprint\n" ) if old_imports in text: text = text.replace(old_imports, new_imports) changed = True login_marker = '\tbrowser = Browser.get()\n\tbrowser.driver.get(os.getenv("TIKTOK_LOGIN_URL"))' login_patch = ( '\tfrom tiktok_uploader.Browser import Browser\n\n' '\tbrowser = Browser.get()\n\tbrowser.driver.get(os.getenv("TIKTOK_LOGIN_URL"))' ) if login_marker in text and login_patch not in text: text = text.replace(login_marker, login_patch) changed = True old_cookies = ( '\tsession.cookies.set("sessionid", session_id, domain=".tiktok.com")\n' '\tsession.cookies.set("tt-target-idc", dc_id, domain=".tiktok.com")\n' ) new_cookies = ( '\tsession.cookies.set("sessionid", session_id, domain=".tiktok.com")\n' '\tsession.cookies.set("sessionid_ss", session_id, domain=".tiktok.com")\n' '\tsession.cookies.set("tt-target-idc", dc_id, domain=".tiktok.com")\n' ) if old_cookies in text: text = text.replace(old_cookies, new_cookies) changed = True old_project = ( '\tif not assert_success(project_url, r):\n' '\t\treturn False\n\n' '\t# get project_id\n' '\tproject_id = r.json()["project"]["project_id"]\n' ) new_project = ( '\tif not assert_success(project_url, r):\n' '\t\treturn False\n\n' '\t# get project_id\n' '\ttry:\n' '\t\tpayload = r.json()\n' '\texcept Exception:\n' '\t\traise RuntimeError("TikTok 创建项目失败:返回不是 JSON")\n' '\tproject = payload.get("project") if isinstance(payload, dict) else None\n' '\tif not isinstance(project, dict) or not project.get("project_id"):\n' '\t\tmsg = str((payload or {}).get("status_msg") or "").strip()\n' '\t\tcode = (payload or {}).get("status_code")\n' '\t\traise RuntimeError(\n' '\t\t\tf"TikTok 会话无效或机房不匹配(status_code={code} {msg})。请重新粘贴 Cookie 并填写 tt-target-idc"\n' '\t\t)\n' '\tproject_id = project["project_id"]\n' ) if old_project in text: text = text.replace(old_project, new_project) changed = True old = ( '\t\t\t\t"privacy_setting_info": {\n' '\t\t\t\t\t"visibility_type": 0,\n' '\t\t\t\t\t"allow_duet": 1,\n' '\t\t\t\t\t"allow_stitch": 1,\n' '\t\t\t\t\t"allow_comment": 1\n' "\t\t\t\t}" ) new = ( '\t\t\t\t"privacy_setting_info": {\n' '\t\t\t\t\t"visibility_type": visibility_type,\n' '\t\t\t\t\t"allow_duet": allow_duet,\n' '\t\t\t\t\t"allow_stitch": allow_stitch,\n' '\t\t\t\t\t"allow_comment": allow_comment,\n' "\t\t\t\t}" ) if old in text and new not in text: text = text.replace(old, new) changed = True old_ret = ( '\tif not uploaded:\n' '\t\tprint("[-] Could not upload video")\n' '\t\treturn False\n' ) new_ret = old_ret + '\treturn True\n' if old_ret in text and '\treturn True\n' not in text[text.find(old_ret): text.find(old_ret) + 200]: text = text.replace(old_ret, new_ret, 1) changed = True if changed: path.write_text(text, encoding="utf-8") print("patched:", path) init_path = VENDOR / "tiktok_uploader" / "__init__.py" if init_path.is_file(): slim = ( "# Cookie/API upload path must not import Browser (selenium) or Video (moviepy) at package load.\n" "from .basics import *\n" "from .Config import *\n" "from .cookies import *\n" ) cur = init_path.read_text(encoding="utf-8") if cur.strip() != slim.strip(): init_path.write_text(slim, encoding="utf-8") print("patched:", init_path) if __name__ == "__main__": sys.exit(main())