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>
148 lines
4.6 KiB
Python
148 lines
4.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Unified Douyin live-URL ingest → rooms (直播池).
|
|
|
|
All UI surfaces that accept Douyin live URLs should call ensure_in_live_pool
|
|
so overview / monitor / live-map stay in sync with assign pools.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
import uuid
|
|
from typing import Any, Optional
|
|
|
|
from .db import _connect, _is_blacklisted_conn, _lock, _row_to_dict
|
|
from .models import extract_room_id, normalize_room_url
|
|
|
|
|
|
def parse_douyin_line(raw: str) -> dict[str, str]:
|
|
"""Parse paste line → {url, title, room_id}. Reuses assign pool parser."""
|
|
from .yt_assign_db import parse_pool_line
|
|
|
|
return parse_pool_line(raw)
|
|
|
|
|
|
def ensure_in_live_pool_conn(
|
|
conn,
|
|
url: str,
|
|
*,
|
|
note: str = "",
|
|
title: str = "",
|
|
enabled: bool = True,
|
|
tags: str = "",
|
|
) -> dict[str, Any]:
|
|
"""Insert or soft-update a rooms row. Caller holds db._lock / open conn.
|
|
|
|
Returns {ok, created, skipped_blacklist, room, reason}.
|
|
"""
|
|
normalized = normalize_room_url(str(url or "").strip()) or str(url or "").strip()
|
|
rid = extract_room_id(normalized)
|
|
if not rid:
|
|
return {"ok": False, "created": False, "reason": "无法解析抖音直播间 URL / 房间号"}
|
|
|
|
blocked = _is_blacklisted_conn(conn, rid, normalized)
|
|
if blocked:
|
|
return {
|
|
"ok": False,
|
|
"created": False,
|
|
"skipped_blacklist": True,
|
|
"reason": f"黑名单:{blocked.get('reason') or blocked.get('value') or ''}",
|
|
}
|
|
|
|
label = (str(title or "").strip() or str(note or "").strip())[:120]
|
|
now = time.time()
|
|
existing = conn.execute(
|
|
"SELECT * FROM rooms WHERE room_id = ? OR url = ? LIMIT 1",
|
|
(rid, normalized),
|
|
).fetchone()
|
|
if existing:
|
|
row = _row_to_dict(existing) or {}
|
|
pk = str(row.get("id") or "")
|
|
cur_note = str(row.get("note") or "").strip()
|
|
cur_anchor = str(row.get("last_anchor") or "").strip()
|
|
new_note = cur_note or label
|
|
new_anchor = cur_anchor or label
|
|
# Kick monitor sooner so overview/live-map refresh.
|
|
conn.execute(
|
|
"""
|
|
UPDATE rooms SET
|
|
note = CASE WHEN TRIM(COALESCE(note, '')) = '' AND ? != '' THEN ? ELSE note END,
|
|
last_anchor = CASE WHEN TRIM(COALESCE(last_anchor, '')) = '' AND ? != '' THEN ? ELSE last_anchor END,
|
|
next_check_at = MIN(COALESCE(next_check_at, ?), ?),
|
|
updated_at = ?
|
|
WHERE id = ?
|
|
""",
|
|
(label, new_note, label, new_anchor, now, now, now, pk),
|
|
)
|
|
fresh = conn.execute("SELECT * FROM rooms WHERE id = ?", (pk,)).fetchone()
|
|
return {"ok": True, "created": False, "room": _row_to_dict(fresh), "reason": "exists"}
|
|
|
|
pk = str(uuid.uuid4())
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO rooms(
|
|
id, url, room_id, note, enabled, last_status, last_anchor, tags,
|
|
created_at, updated_at, next_check_at
|
|
) VALUES (?, ?, ?, ?, ?, 'unknown', ?, ?, ?, ?, ?)
|
|
""",
|
|
(
|
|
pk,
|
|
normalized,
|
|
rid,
|
|
label,
|
|
1 if enabled else 0,
|
|
label,
|
|
tags or "",
|
|
now,
|
|
now,
|
|
now,
|
|
),
|
|
)
|
|
fresh = conn.execute("SELECT * FROM rooms WHERE id = ?", (pk,)).fetchone()
|
|
return {"ok": True, "created": True, "room": _row_to_dict(fresh), "reason": "created"}
|
|
|
|
|
|
def ensure_in_live_pool(
|
|
url: str,
|
|
*,
|
|
note: str = "",
|
|
title: str = "",
|
|
enabled: bool = True,
|
|
tags: Any = None,
|
|
) -> dict[str, Any]:
|
|
"""Public wrapper with lock — for API handlers outside assign txn."""
|
|
from .db import _normalize_tags
|
|
|
|
tags_s = _normalize_tags(tags) if tags is not None else ""
|
|
with _lock:
|
|
conn = _connect()
|
|
try:
|
|
result = ensure_in_live_pool_conn(
|
|
conn,
|
|
url,
|
|
note=note,
|
|
title=title,
|
|
enabled=enabled,
|
|
tags=tags_s,
|
|
)
|
|
conn.commit()
|
|
return result
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def sync_pool_titles_from_anchor_conn(conn, room_id: str, anchor: str) -> int:
|
|
"""Fill empty yt_assign_pool_urls.title from rooms.last_anchor."""
|
|
rid = str(room_id or "").strip()
|
|
name = str(anchor or "").strip()[:120]
|
|
if not rid or not name:
|
|
return 0
|
|
cur = conn.execute(
|
|
"""
|
|
UPDATE yt_assign_pool_urls
|
|
SET title = ?, updated_at = ?
|
|
WHERE room_id = ? AND TRIM(COALESCE(title, '')) = ''
|
|
""",
|
|
(name, time.time(), rid),
|
|
)
|
|
return int(cur.rowcount or 0)
|