douyinWEB/scripts/setup_tiktok_uploader.py
eric 78a043b0de fix: slim tiktok_uploader __init__ to skip Browser/Video imports
Package __init__ was still loading undetected_chromedriver before tiktok.upload_video could be imported.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-14 11:00:15 -05:00

103 lines
3.4 KiB
Python

# -*- 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 = (
'\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
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())