feat: M2 (part 2) — RSS poller, backfill, enrichment, scheduler
- Free per-channel RSS reader for quota-less fresh-video detection
- Recent-first backfill (configurable: 100 videos / 1 year) plus resumable deep
backfill from the uploads playlist
- Enrichment via videos.list: duration, view/like counts, category, topics,
language, Shorts heuristic and livestream/premiere classification
- Reusable sync runners + APScheduler jobs (rss / enrich / backfill), all
quota-aware with a reserve so backfill never starves fresh enrichment
- Manual triggers: POST /api/sync/{rss,backfill,enrich}
- Exact insert counting via RETURNING with in-batch de-duplication
2026-06-11 01:36:41 +02:00
|
|
|
"""Background scheduler: free RSS detection, enrichment of new videos, and quota-aware
|
|
|
|
|
recent-first / deep backfill. Runs inside the API process (single worker)."""
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
|
|
|
|
|
|
from app.config import settings
|
|
|
|
|
from app.db import SessionLocal
|
2026-06-11 01:57:19 +02:00
|
|
|
from app.sync.autotag import run_autotag_all
|
feat: M2 (part 2) — RSS poller, backfill, enrichment, scheduler
- Free per-channel RSS reader for quota-less fresh-video detection
- Recent-first backfill (configurable: 100 videos / 1 year) plus resumable deep
backfill from the uploads playlist
- Enrichment via videos.list: duration, view/like counts, category, topics,
language, Shorts heuristic and livestream/premiere classification
- Reusable sync runners + APScheduler jobs (rss / enrich / backfill), all
quota-aware with a reserve so backfill never starves fresh enrichment
- Manual triggers: POST /api/sync/{rss,backfill,enrich}
- Exact insert counting via RETURNING with in-batch de-duplication
2026-06-11 01:36:41 +02:00
|
|
|
from app.sync.runner import (
|
|
|
|
|
run_deep_backfill,
|
|
|
|
|
run_enrich,
|
|
|
|
|
run_recent_backfill,
|
|
|
|
|
run_rss_poll,
|
fix: address reader-UI feedback (shorts, search, tags, login, UX)
- Shorts: confirm via youtube.com/shorts/<id> probe (SOCS cookie bypasses the
consent redirect) instead of a 60s heuristic; concurrent probing, shorts_probed
flag, scheduled refinement (migration 0005)
- Search: match title + channel name only (descriptions caused noisy results)
- Faceted tag filtering: AND across categories (language AND topic narrows),
OR within a category; any/all toggle applies to topics
- Language detection: majority vote over individual titles (fixes misdetections
like multipoleguy -> English; drops bogus Polish/Romanian)
- Login: drop forced consent so returning sign-in is quick (select_account)
- Feed cards: clickable channel name (opens channel), persistent saved badge,
undo toast on hide, Hidden view to restore; tag chips show counts in tooltip
2026-06-11 03:07:49 +02:00
|
|
|
run_shorts,
|
2026-06-11 03:28:45 +02:00
|
|
|
run_subscription_resync,
|
feat: M2 (part 2) — RSS poller, backfill, enrichment, scheduler
- Free per-channel RSS reader for quota-less fresh-video detection
- Recent-first backfill (configurable: 100 videos / 1 year) plus resumable deep
backfill from the uploads playlist
- Enrichment via videos.list: duration, view/like counts, category, topics,
language, Shorts heuristic and livestream/premiere classification
- Reusable sync runners + APScheduler jobs (rss / enrich / backfill), all
quota-aware with a reserve so backfill never starves fresh enrichment
- Manual triggers: POST /api/sync/{rss,backfill,enrich}
- Exact insert counting via RETURNING with in-batch de-duplication
2026-06-11 01:36:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger("subfeed.scheduler")
|
|
|
|
|
|
|
|
|
|
_scheduler: BackgroundScheduler | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _job(name: str, fn) -> None:
|
|
|
|
|
db = SessionLocal()
|
|
|
|
|
try:
|
|
|
|
|
result = fn(db)
|
|
|
|
|
logger.info("job %s -> %s", name, result)
|
|
|
|
|
except Exception:
|
|
|
|
|
db.rollback()
|
|
|
|
|
logger.exception("job %s failed", name)
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _rss_job() -> None:
|
|
|
|
|
_job("rss_poll", lambda db: run_rss_poll(db))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _enrich_job() -> None:
|
|
|
|
|
_job("enrich", lambda db: run_enrich(db))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _backfill_job() -> None:
|
|
|
|
|
# Recent-first for not-yet-synced channels, then deep backfill for the rest.
|
|
|
|
|
def work(db):
|
|
|
|
|
recent = run_recent_backfill(db, max_channels=25)
|
|
|
|
|
deep = run_deep_backfill(db, max_channels=10)
|
|
|
|
|
return {"recent": recent, "deep": deep}
|
|
|
|
|
|
|
|
|
|
_job("backfill", work)
|
|
|
|
|
|
|
|
|
|
|
2026-06-11 01:57:19 +02:00
|
|
|
def _autotag_job() -> None:
|
|
|
|
|
_job("autotag", lambda db: run_autotag_all(db, only_missing=True))
|
|
|
|
|
|
|
|
|
|
|
fix: address reader-UI feedback (shorts, search, tags, login, UX)
- Shorts: confirm via youtube.com/shorts/<id> probe (SOCS cookie bypasses the
consent redirect) instead of a 60s heuristic; concurrent probing, shorts_probed
flag, scheduled refinement (migration 0005)
- Search: match title + channel name only (descriptions caused noisy results)
- Faceted tag filtering: AND across categories (language AND topic narrows),
OR within a category; any/all toggle applies to topics
- Language detection: majority vote over individual titles (fixes misdetections
like multipoleguy -> English; drops bogus Polish/Romanian)
- Login: drop forced consent so returning sign-in is quick (select_account)
- Feed cards: clickable channel name (opens channel), persistent saved badge,
undo toast on hide, Hidden view to restore; tag chips show counts in tooltip
2026-06-11 03:07:49 +02:00
|
|
|
def _shorts_job() -> None:
|
|
|
|
|
_job("shorts", run_shorts)
|
|
|
|
|
|
|
|
|
|
|
2026-06-11 03:28:45 +02:00
|
|
|
def _subscriptions_job() -> None:
|
|
|
|
|
_job("subscriptions", run_subscription_resync)
|
|
|
|
|
|
|
|
|
|
|
feat: M2 (part 2) — RSS poller, backfill, enrichment, scheduler
- Free per-channel RSS reader for quota-less fresh-video detection
- Recent-first backfill (configurable: 100 videos / 1 year) plus resumable deep
backfill from the uploads playlist
- Enrichment via videos.list: duration, view/like counts, category, topics,
language, Shorts heuristic and livestream/premiere classification
- Reusable sync runners + APScheduler jobs (rss / enrich / backfill), all
quota-aware with a reserve so backfill never starves fresh enrichment
- Manual triggers: POST /api/sync/{rss,backfill,enrich}
- Exact insert counting via RETURNING with in-batch de-duplication
2026-06-11 01:36:41 +02:00
|
|
|
def start_scheduler() -> None:
|
|
|
|
|
global _scheduler
|
|
|
|
|
if not settings.scheduler_enabled or _scheduler is not None:
|
|
|
|
|
return
|
|
|
|
|
scheduler = BackgroundScheduler(timezone="UTC")
|
|
|
|
|
scheduler.add_job(
|
|
|
|
|
_rss_job, "interval", minutes=settings.rss_poll_minutes, id="rss_poll"
|
|
|
|
|
)
|
|
|
|
|
scheduler.add_job(
|
|
|
|
|
_enrich_job, "interval", minutes=settings.enrich_interval_minutes, id="enrich"
|
|
|
|
|
)
|
|
|
|
|
scheduler.add_job(
|
|
|
|
|
_backfill_job,
|
|
|
|
|
"interval",
|
|
|
|
|
minutes=settings.backfill_interval_minutes,
|
|
|
|
|
id="backfill",
|
|
|
|
|
)
|
2026-06-11 01:57:19 +02:00
|
|
|
scheduler.add_job(
|
|
|
|
|
_autotag_job,
|
|
|
|
|
"interval",
|
|
|
|
|
minutes=settings.autotag_interval_minutes,
|
|
|
|
|
id="autotag",
|
|
|
|
|
)
|
fix: address reader-UI feedback (shorts, search, tags, login, UX)
- Shorts: confirm via youtube.com/shorts/<id> probe (SOCS cookie bypasses the
consent redirect) instead of a 60s heuristic; concurrent probing, shorts_probed
flag, scheduled refinement (migration 0005)
- Search: match title + channel name only (descriptions caused noisy results)
- Faceted tag filtering: AND across categories (language AND topic narrows),
OR within a category; any/all toggle applies to topics
- Language detection: majority vote over individual titles (fixes misdetections
like multipoleguy -> English; drops bogus Polish/Romanian)
- Login: drop forced consent so returning sign-in is quick (select_account)
- Feed cards: clickable channel name (opens channel), persistent saved badge,
undo toast on hide, Hidden view to restore; tag chips show counts in tooltip
2026-06-11 03:07:49 +02:00
|
|
|
scheduler.add_job(
|
|
|
|
|
_shorts_job,
|
|
|
|
|
"interval",
|
|
|
|
|
minutes=settings.shorts_probe_interval_minutes,
|
|
|
|
|
id="shorts",
|
|
|
|
|
)
|
2026-06-11 03:28:45 +02:00
|
|
|
scheduler.add_job(
|
|
|
|
|
_subscriptions_job,
|
|
|
|
|
"interval",
|
|
|
|
|
minutes=settings.subscriptions_resync_minutes,
|
|
|
|
|
id="subscriptions",
|
|
|
|
|
)
|
feat: M2 (part 2) — RSS poller, backfill, enrichment, scheduler
- Free per-channel RSS reader for quota-less fresh-video detection
- Recent-first backfill (configurable: 100 videos / 1 year) plus resumable deep
backfill from the uploads playlist
- Enrichment via videos.list: duration, view/like counts, category, topics,
language, Shorts heuristic and livestream/premiere classification
- Reusable sync runners + APScheduler jobs (rss / enrich / backfill), all
quota-aware with a reserve so backfill never starves fresh enrichment
- Manual triggers: POST /api/sync/{rss,backfill,enrich}
- Exact insert counting via RETURNING with in-batch de-duplication
2026-06-11 01:36:41 +02:00
|
|
|
scheduler.start()
|
|
|
|
|
_scheduler = scheduler
|
|
|
|
|
logger.info("scheduler started")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def shutdown_scheduler() -> None:
|
|
|
|
|
global _scheduler
|
|
|
|
|
if _scheduler is not None:
|
|
|
|
|
_scheduler.shutdown(wait=False)
|
|
|
|
|
_scheduler = None
|