"""Remplissage de la base de test (chaînes, vidéos, relevés, commentaires).""" from __future__ import annotations from datetime import datetime, timedelta, timezone import psycopg from .site import CH, COMMENT_TEXTS, OTHER def add_channel(url: str, channel_id: str, active: bool = True, checked: bool = False, scope: str = "top") -> None: """Ajoute une chaîne suivie (comme la page « Chaînes » du site).""" with psycopg.connect(url, autocommit=True) as conn: conn.execute( """INSERT INTO shortstats.channel (channel_id, active, checked_at, youtube_title, comment_scope) VALUES (%s, %s, CASE WHEN %s THEN now() END, CASE WHEN %s THEN 'Chaîne' END, %s)""", (channel_id, active, checked, checked, scope), ) def seed_snapshots(owner_url, n_videos=12): """Deux chaînes, des vidéos à différents âges, des relevés sur les premières étapes.""" base = datetime(2026, 9, 1, 17, 0, tzinfo=timezone.utc) with psycopg.connect(owner_url, autocommit=True) as conn: for cid, name in ((CH, "Ma chaîne"), (OTHER, "Autre chaîne")): conn.execute("""INSERT INTO shortstats.channel (channel_id, youtube_title) VALUES (%s, %s) ON CONFLICT DO NOTHING""", (cid, name)) steps = conn.execute("SELECT step_id, nominal_hours FROM shortstats.snapshot_step " "WHERE step_id <= 30 ORDER BY step_id").fetchall() for i in range(n_videos): vid = f"evo{i:08d}" published = base + timedelta(days=i, hours=i % 7) conn.execute( """INSERT INTO shortstats.video (video_id, channel_id, published_at, status, title, duration_s) VALUES (%s, %s, %s, 'pending', %s, %s)""", (vid, CH if i % 2 else OTHER, published, f"Évolution {i}", 20 + i)) for sid, hours in steps[: 6 + i * 2]: views = int(100 * (i + 1) * (float(hours) ** 0.6)) # commentaires relevés et comptes par thème (analyse thématique, 1.3.0) read = views // 300 + 3 conn.execute( """INSERT INTO shortstats.video_snapshot (video_id, step_id, taken_at, age_hours, view_count, like_count, comment_count, comments_read) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""", (vid, sid, published + timedelta(hours=float(hours) + 0.1), float(hours) + 0.1, views, views // 20, views // 300, read)) conn.execute( """INSERT INTO shortstats.snapshot_theme (video_id, step_id, theme_id, comments) SELECT %s, %s, theme_id, CASE WHEN name = 'applepie' THEN %s ELSE %s END FROM shortstats.theme WHERE CASE WHEN name = 'applepie' THEN %s ELSE %s END > 0""", (vid, sid, read // 3, i % 3, read // 3, i % 3)) def add_comments(owner_url, comments): """Une vidéo et ses commentaires relevés (du plus ancien au plus récent), classés comme par le collecteur.""" from shortstats import comment_themes with psycopg.connect(owner_url, autocommit=True) as conn: conn.execute("""INSERT INTO shortstats.channel (channel_id, youtube_title) VALUES (%s, 'Ma chaîne') ON CONFLICT DO NOTHING""", (CH,)) conn.execute("""INSERT INTO shortstats.video (video_id, channel_id, published_at, status, title) VALUES ('rgxvideo001', %s, %s, 'pending', 'Short des commentaires') ON CONFLICT DO NOTHING""", (CH, datetime(2026, 9, 1, tzinfo=timezone.utc))) ids = [] for i, text in enumerate(comments): conn.execute("""INSERT INTO shortstats.comment (comment_id, video_id, published_at, text) VALUES (%s, 'rgxvideo001', %s, %s)""", (f"rgx{i:08d}", datetime(2026, 9, 2 + i, tzinfo=timezone.utc), text)) ids.append(f"rgx{i:08d}") comment_themes.classify(conn, ids) def add_pinned(owner_url, comments): """Avant la 1.3.0 (migrations 008 et 009) : une vidéo relevée par commentaire épinglé.""" with psycopg.connect(owner_url, autocommit=True) as conn: conn.execute("""INSERT INTO shortstats.channel (channel_id, author_channel_id, youtube_title) VALUES (%s, %s, 'Ma chaîne') ON CONFLICT DO NOTHING""", (CH, CH)) for i, text in enumerate(comments): conn.execute( """INSERT INTO shortstats.video (video_id, channel_id, published_at, status, collected_at, title, view_count, author_comment_status, author_comment_text) VALUES (%s, %s, %s, 'collected', now(), %s, 100, 'found', %s)""", (f"rgx{i:08d}", CH, datetime(2026, 9, 1 + i, tzinfo=timezone.utc), f"Short {i}", text)) def seed_videos(owner_url, n=30): base = datetime(2026, 9, 1, 17, 0, tzinfo=timezone.utc) with psycopg.connect(owner_url, autocommit=True) as conn: conn.execute("""INSERT INTO shortstats.channel (channel_id, youtube_title) VALUES (%s, 'Ma chaîne') ON CONFLICT DO NOTHING""", (CH,)) for i in range(n): conn.execute( """INSERT INTO shortstats.video (video_id, channel_id, published_at, status, collected_at, title, duration_s, view_count, like_count, comment_count) VALUES (%s, %s, %s, 'collected', now(), %s, %s, %s, %s, %s)""", (f"vid{i:08d}", CH, base + timedelta(days=i, hours=i % 5), f"Short {i}", 30 + i, 1000 * (i + 1), 40 * (i + 1), 5 + i), ) def seed_comments(owner_url, n=30): """Pour les vidéos de seed_videos : un relevé à 7 j, et 2 + (i % 4) commentaires relevés (publiés avant le relevé), classés par thème comme le ferait le collecteur.""" from shortstats import comment_themes base = datetime(2026, 9, 1, 17, 0, tzinfo=timezone.utc) vids, ids = [], [] with psycopg.connect(owner_url, autocommit=True) as conn: for i in range(n): vid = f"vid{i:08d}" published = base + timedelta(days=i, hours=i % 5) taken = published + timedelta(hours=168) conn.execute( """INSERT INTO shortstats.video_snapshot (video_id, step_id, taken_at, age_hours, view_count, like_count, comment_count) VALUES (%s, 56, %s, 168, %s, %s, %s)""", (vid, taken, 1000 * (i + 1), 40 * (i + 1), 5 + i)) for k in range(2 + i % 4): cid = f"{vid}-c{k}" conn.execute( """INSERT INTO shortstats.comment (comment_id, video_id, published_at, text) VALUES (%s, %s, %s, %s)""", (cid, vid, published + timedelta(hours=k + 1), COMMENT_TEXTS[(i + k) % 4])) ids.append(cid) conn.execute("""UPDATE shortstats.video SET comments_read_at = %s, comment_count_read = %s, comment_count_latest = %s, comment_scope_read = 'top' WHERE video_id = %s""", (taken, 5 + i, 5 + i, vid)) vids.append(vid) comment_themes.classify(conn, ids) comment_themes.refresh_videos(conn, vids) def channels_in_db(owner_url): with psycopg.connect(owner_url) as conn: return {r[0]: r[1:] for r in conn.execute( "SELECT channel_id, comment_scope, active, youtube_title, checked_at FROM shortstats.channel")}