feat: fechas aproximadas gratis en discovery (approximate_date) con upgrade a real al extraer

This commit is contained in:
urieljareth
2026-08-22 14:51:03 -06:00
parent 8940c325ea
commit e219cfcbfa
5 changed files with 1087 additions and 75 deletions
+93
View File
@@ -0,0 +1,93 @@
"""Fechas aproximadas de discovery: bandera upload_date_approx.
Matriz de precedencia de upsert_videos + upgrade approx->real de
set_upload_date + conversion timestamp -> YYYYMMDD en discover.
Spec: docs/superpowers/specs/2026-08-22-approximate-upload-dates-design.md
"""
from yt_scraper.store import Store, VideoRef
def _ref(vid, date=None, approx=0, ch="ch1"):
return VideoRef(
video_id=vid,
channel_id=ch,
title="t " + vid,
url=f"https://youtu.be/{vid}",
upload_date=date,
date_approx=approx,
)
def _store(tmp_path):
s = Store(tmp_path / "test.db")
s.upsert_channel("ch1", None, "Canal de prueba")
return s
def test_approx_into_empty(tmp_path):
s = _store(tmp_path)
s.upsert_videos([_ref("v1", date="20260820", approx=1)])
row = s.get_video("v1")
assert row.upload_date == "20260820"
assert row.upload_date_approx == 1
def test_real_not_downgraded_by_approx(tmp_path):
s = _store(tmp_path)
s.upsert_videos([_ref("v1", date="20240101", approx=0)])
s.upsert_videos([_ref("v1", date="20260820", approx=1)])
row = s.get_video("v1")
assert row.upload_date == "20240101"
assert row.upload_date_approx == 0
def test_approx_refreshed_by_newer_approx(tmp_path):
s = _store(tmp_path)
s.upsert_videos([_ref("v1", date="20260101", approx=1)])
s.upsert_videos([_ref("v1", date="20260820", approx=1)])
row = s.get_video("v1")
assert row.upload_date == "20260820"
assert row.upload_date_approx == 1
def test_extraction_upgrades_approx_to_real(tmp_path):
s = _store(tmp_path)
s.upsert_videos([_ref("v1", date="20260820", approx=1)])
s.set_upload_date("v1", "20260818")
row = s.get_video("v1")
assert row.upload_date == "20260818"
assert row.upload_date_approx == 0
def test_set_upload_date_still_skips_existing_real(tmp_path):
s = _store(tmp_path)
s.upsert_videos([_ref("v1", date="20240101", approx=0)])
s.set_upload_date("v1", "20260818")
assert s.get_video("v1").upload_date == "20240101"
def test_migration_defaults_flag_to_zero(tmp_path):
# Filas creadas por la via vieja (INSERT sin la columna) quedan con 0.
s = _store(tmp_path)
s.upsert_videos([_ref("v2", date=None, approx=0)])
row = s.get_video("v2")
assert not row.upload_date_approx
# --- conversion en discover -------------------------------------------------
from yt_scraper.discover import _entry_upload_date
def test_entry_upload_date_prefers_exact():
assert _entry_upload_date({"upload_date": "20240101"}) == ("20240101", 0)
def test_entry_upload_date_from_timestamp():
ts = 1716115200 # 2024-05-19 12:00 UTC
assert _entry_upload_date({"timestamp": ts}) == ("20240519", 1)
def test_entry_upload_date_none():
assert _entry_upload_date({}) == (None, 0)