Prefer location.assign for endscreen leave, and ship hub analytics/charts plus host deploy helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
254 lines
11 KiB
Python
254 lines
11 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from src.youtube_relay_log import (
|
|
close_active_relay_sessions,
|
|
end_relay_session,
|
|
import_relay_sessions_from_log,
|
|
list_relay_sessions,
|
|
on_relay_push_start,
|
|
start_relay_session,
|
|
summarize_relay_sessions,
|
|
sync_relay_from_recent_log,
|
|
)
|
|
|
|
|
|
class YoutubeRelayLogTests(unittest.TestCase):
|
|
def test_start_and_end_session(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
db = Path(td) / "hub_state.sqlite"
|
|
started = start_relay_session(
|
|
process="youtube",
|
|
anchor_name="又系_柠檬茶",
|
|
source_url="https://live.douyin.com/745277409780",
|
|
source_room_id="745277409780",
|
|
stream_title="又系_柠檬茶_2026-06-24_03-59-00",
|
|
channel_name="线路1",
|
|
db_path=db,
|
|
)
|
|
self.assertEqual(started["anchor_name"], "又系_柠檬茶")
|
|
self.assertEqual(started["source_room_id"], "745277409780")
|
|
self.assertEqual(started["status"], "active")
|
|
self.assertEqual(started["push_date"], "2026-06-24")
|
|
|
|
ended = end_relay_session("youtube", anchor_name="又系_柠檬茶", reason="push_stopped", db_path=db)
|
|
assert ended is not None
|
|
self.assertEqual(ended["status"], "ended")
|
|
self.assertIsNotNone(ended["ended_at"])
|
|
self.assertGreaterEqual(ended["duration_seconds"], 0)
|
|
|
|
def test_on_relay_push_start_dedupes_same_anchor(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
db = Path(td) / "hub_state.sqlite"
|
|
first = on_relay_push_start(
|
|
process="youtube",
|
|
anchor_name="樱桃炒饭(摆摊版)",
|
|
record_url="https://live.douyin.com/334176667239",
|
|
stream_title="樱桃炒饭(摆摊版)_2026-06-24_07-10-00",
|
|
real_url="https://pull-flv.example/live.flv",
|
|
db_path=db,
|
|
)
|
|
second = on_relay_push_start(
|
|
process="youtube",
|
|
anchor_name="樱桃炒饭(摆摊版)",
|
|
record_url="https://live.douyin.com/334176667239",
|
|
stream_title="樱桃炒饭(摆摊版)_2026-06-24_07-10-00",
|
|
real_url="https://pull-flv.example/live.flv",
|
|
db_path=db,
|
|
)
|
|
self.assertEqual(first["id"], second["id"])
|
|
listing = list_relay_sessions(process="youtube", db_path=db)
|
|
self.assertEqual(listing["total"], 1)
|
|
|
|
def test_source_switch_closes_previous_session(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
db = Path(td) / "hub_state.sqlite"
|
|
first = start_relay_session(
|
|
process="youtube",
|
|
anchor_name="主播A",
|
|
source_url="https://live.douyin.com/111",
|
|
db_path=db,
|
|
)
|
|
second = start_relay_session(
|
|
process="youtube",
|
|
anchor_name="主播B",
|
|
source_url="https://live.douyin.com/222",
|
|
db_path=db,
|
|
)
|
|
self.assertNotEqual(first["id"], second["id"])
|
|
listing = list_relay_sessions(process="youtube", status="ended", db_path=db)
|
|
self.assertEqual(listing["total"], 1)
|
|
active = list_relay_sessions(process="youtube", status="active", db_path=db)
|
|
self.assertEqual(active["total"], 1)
|
|
self.assertEqual(active["sessions"][0]["anchor_name"], "主播B")
|
|
|
|
def test_list_relay_sessions_search_and_date_filter(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
db = Path(td) / "hub_state.sqlite"
|
|
start_relay_session(
|
|
process="youtube",
|
|
anchor_name="音乐主播",
|
|
source_url="https://live.douyin.com/99887766",
|
|
source_room_id="99887766",
|
|
stream_title="音乐主播_2026-06-20_12-00-00",
|
|
db_path=db,
|
|
)
|
|
start_relay_session(
|
|
process="youtube2__lane1",
|
|
anchor_name="其他主播",
|
|
source_url="https://live.douyin.com/11223344",
|
|
db_path=db,
|
|
)
|
|
|
|
by_q = list_relay_sessions(q="99887766", db_path=db)
|
|
self.assertEqual(by_q["total"], 1)
|
|
self.assertEqual(by_q["sessions"][0]["anchor_name"], "音乐主播")
|
|
|
|
by_proc = list_relay_sessions(process="youtube2__lane1", db_path=db)
|
|
self.assertEqual(by_proc["total"], 1)
|
|
|
|
def test_sync_relay_from_recent_log(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
db = Path(td) / "hub_state.sqlite"
|
|
recent_log = "\n".join(
|
|
[
|
|
"2026-06-24T06:04:11: INFO | 06:04:11 | 正在录制 1 个: 又系_柠檬茶[原画 2:05:11]",
|
|
]
|
|
)
|
|
url_lines = "https://live.douyin.com/745277409780 又系_柠檬茶\n"
|
|
synced = sync_relay_from_recent_log(
|
|
"youtube",
|
|
recent_log,
|
|
channel_name="线路1",
|
|
url_lines=url_lines,
|
|
db_path=db,
|
|
)
|
|
assert synced is not None
|
|
self.assertEqual(synced["anchor_name"], "又系_柠檬茶")
|
|
self.assertIn("745277409780", synced["source_url"])
|
|
self.assertEqual(synced["duration_seconds"], 2 * 3600 + 5 * 60 + 11)
|
|
|
|
def test_import_relay_sessions_from_log(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
db = Path(td) / "hub_state.sqlite"
|
|
log_path = Path(td) / "youtube-out.log"
|
|
log_path.write_text(
|
|
"\n".join(
|
|
[
|
|
"2026-06-24T08:59:10: [INFO] 又系_柠檬茶 准备开始录制视频: /opt/live/d2ypp/downloads/抖音直播/又系_柠檬茶 开始推流到 YouTube: 又系_柠檬茶_2026-06-24_03-59-10",
|
|
"2026-06-24T11:04:11: 监测 29 | 并发 3 | 代理 开 | 错误 0 | 06:04:11 | 正在录制 1 个: 又系_柠檬茶[原画 2:05:01]",
|
|
"2026-06-24T11:05:00: [INFO] 推流已彻底停止并清理完毕",
|
|
"2026-06-24T12:10:00: [INFO] 樱桃炒饭(摆摊版) 开始推流到 YouTube: 樱桃炒饭(摆摊版)_2026-06-24_07-10-00",
|
|
"2026-06-24T14:15:30: 监测 29 | 并发 3 | 代理 开 | 错误 0 | 09:15:30 | 正在录制 1 个: 樱桃炒饭(摆摊版)[原画 2:05:30]",
|
|
]
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
url_lines = "\n".join(
|
|
[
|
|
"https://live.douyin.com/745277409780,主播: 又系_柠檬茶",
|
|
"https://live.douyin.com/334176667239,主播: 樱桃炒饭(摆摊版)",
|
|
]
|
|
)
|
|
result = import_relay_sessions_from_log(
|
|
log_path,
|
|
process="youtube",
|
|
url_lines=url_lines,
|
|
youtube_key_suffix="h-ijkl",
|
|
replace_imported=True,
|
|
db_path=db,
|
|
)
|
|
self.assertGreaterEqual(result["imported"], 1)
|
|
listing = list_relay_sessions(process="youtube", db_path=db)
|
|
anchors = {row["anchor_name"] for row in listing["sessions"]}
|
|
self.assertIn("又系_柠檬茶", anchors)
|
|
self.assertIn("樱桃炒饭(摆摊版)", anchors)
|
|
lemon = next(row for row in listing["sessions"] if row["anchor_name"] == "又系_柠檬茶")
|
|
self.assertIn("745277409780", lemon["source_url"])
|
|
self.assertGreaterEqual(lemon["duration_seconds"], 2 * 3600 + 5 * 60)
|
|
self.assertEqual(lemon["youtube_key_suffix"], "h-ijkl")
|
|
|
|
def test_close_active_relay_sessions(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
db = Path(td) / "hub_state.sqlite"
|
|
start_relay_session(process="youtube", anchor_name="待关闭", db_path=db)
|
|
closed = close_active_relay_sessions("youtube", reason="restart", db_path=db)
|
|
self.assertEqual(len(closed), 1)
|
|
self.assertEqual(closed[0]["end_reason"], "restart")
|
|
active = list_relay_sessions(process="youtube", status="active", db_path=db)
|
|
self.assertEqual(active["total"], 0)
|
|
|
|
def test_summarize_relay_sessions_by_anchor_and_hour(self) -> None:
|
|
with tempfile.TemporaryDirectory() as td:
|
|
db = Path(td) / "hub_state.sqlite"
|
|
start_relay_session(
|
|
process="youtube",
|
|
anchor_name="又系_柠檬茶",
|
|
source_url="https://live.douyin.com/111",
|
|
source_room_id="111",
|
|
stream_title="又系_柠檬茶_2026-08-01_10-00-00",
|
|
db_path=db,
|
|
)
|
|
end_relay_session("youtube", reason="done", db_path=db)
|
|
start_relay_session(
|
|
process="youtube",
|
|
anchor_name="又系_柠檬茶",
|
|
source_url="https://live.douyin.com/111",
|
|
source_room_id="111",
|
|
stream_title="又系_柠檬茶_2026-08-02_22-00-00",
|
|
db_path=db,
|
|
)
|
|
end_relay_session("youtube", reason="done", db_path=db)
|
|
start_relay_session(
|
|
process="youtube",
|
|
anchor_name="樱桃炒饭",
|
|
source_url="https://live.douyin.com/222",
|
|
source_room_id="222",
|
|
stream_title="樱桃炒饭_2026-08-03_15-00-00",
|
|
db_path=db,
|
|
)
|
|
end_relay_session("youtube", reason="done", db_path=db)
|
|
|
|
# Force deterministic start hours for heatmap assertions.
|
|
from src.youtube_live_store import _connect
|
|
|
|
conn = _connect(db)
|
|
rows = conn.execute(
|
|
"SELECT id, stream_title FROM youtube_relay_sessions ORDER BY id"
|
|
).fetchall()
|
|
for row in rows:
|
|
title = row["stream_title"]
|
|
stamp = title.rsplit("_", 2)
|
|
# title like name_YYYY-MM-DD_HH-MM-SS
|
|
day = stamp[-2]
|
|
hms = stamp[-1]
|
|
ts = f"{day} {hms.replace('-', ':')}"
|
|
from datetime import datetime
|
|
|
|
started = datetime.strptime(ts, "%Y-%m-%d %H:%M:%S").timestamp()
|
|
conn.execute(
|
|
"UPDATE youtube_relay_sessions SET started_at=?, ended_at=?, duration_seconds=?, push_date=? WHERE id=?",
|
|
(started, started + 3600, 3600, day, row["id"]),
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
summary = summarize_relay_sessions(process="youtube", db_path=db)
|
|
self.assertEqual(summary["overview"]["sessions"], 3)
|
|
self.assertEqual(summary["overview"]["unique_anchors"], 2)
|
|
self.assertAlmostEqual(summary["overview"]["total_hours"], 3.0, places=2)
|
|
top = summary["top_anchors"][0]
|
|
self.assertEqual(top["anchor_name"], "又系_柠檬茶")
|
|
self.assertEqual(top["sessions"], 2)
|
|
self.assertEqual(summary["by_hour"]["starts"][10], 1)
|
|
self.assertEqual(summary["by_hour"]["starts"][22], 1)
|
|
self.assertEqual(summary["by_hour"]["starts"][15], 1)
|
|
self.assertTrue(summary["insights"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|