douyinWEB/scripts/_backfill_avatars.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

70 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
"""Force-refresh owner avatars with systemd/API env (S3_ENABLED)."""
import asyncio
import os
import sys
from pathlib import Path
# Load EnvironmentFile used by douyinweb-api
for env_path in (
Path("/etc/douyinweb/api.env"),
Path("/etc/douyinweb.env"),
Path("/opt/douyinWEB/backend/.env"),
):
if not env_path.is_file():
continue
for line in env_path.read_text(encoding="utf-8", errors="replace").splitlines():
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
k, _, v = line.partition("=")
k = k.strip()
v = v.strip().strip('"').strip("'")
if k and k not in os.environ:
os.environ[k] = v
sys.path.insert(0, "/opt/douyinWEB/backend")
from app import db
from app.cover import ensure_room_avatar
from app.douyin import check_room
from app.db import get_settings_map
from app import s3_storage
from app.config import settings
async def main() -> None:
print("s3", settings.s3_enabled, s3_storage.s3_enabled(), settings.s3_public_base)
# clear bad local avatar paths
with db._lock:
conn = db._connect()
try:
conn.execute(
"UPDATE rooms SET avatar_url='' WHERE avatar_url NOT LIKE 'http%'"
)
conn.commit()
finally:
conn.close()
cfg = get_settings_map()
cookie = str(cfg.get("dy_cookie") or "")
rooms = db.list_rooms(limit=200, offset=0).get("rooms") or []
targets = [r for r in rooms if r.get("last_status") == "live"]
if not targets:
targets = rooms[:10]
print("targets", len(targets))
for r in targets:
url = str(r.get("url") or "")
rid = str(r.get("room_id") or "")
probe = await check_room(url, cookie=cookie)
avatar = str(probe.get("avatar_image") or "").strip()
print("probe", rid, probe.get("anchor_name"), "avatar", bool(avatar))
if not avatar:
continue
out = await ensure_room_avatar(rid, avatar, force=True)
print(" save", out)
if __name__ == "__main__":
asyncio.run(main())