daily_recommend_store.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """每日推荐持久化 —— 推荐结果存储、去重与历史查询."""
  2. from __future__ import annotations
  3. import sqlite3
  4. import time
  5. from collections.abc import Iterable
  6. from ...utils.common import normalize_arxiv_id as _norm_arxiv_id
  7. from ...utils.common import exec_sql
  8. def ensure_tables(db_path: str) -> None:
  9. exec_sql(db_path,
  10. """CREATE TABLE IF NOT EXISTS daily_recommendations (
  11. id INTEGER PRIMARY KEY AUTOINCREMENT,
  12. date_key TEXT NOT NULL,
  13. source TEXT NOT NULL,
  14. arxiv_id TEXT,
  15. title TEXT,
  16. created_at INTEGER NOT NULL
  17. )""",
  18. "CREATE INDEX IF NOT EXISTS idx_daily_reco_date ON daily_recommendations(date_key, created_at)",
  19. "CREATE INDEX IF NOT EXISTS idx_daily_reco_arxiv ON daily_recommendations(source, arxiv_id)",
  20. )
  21. def record_arxiv_recommendations(
  22. db_path: str,
  23. *,
  24. date_key: str,
  25. items: Iterable[tuple[str | None, str]],
  26. ) -> int:
  27. ensure_tables(db_path)
  28. now = int(time.time())
  29. conn = sqlite3.connect(db_path)
  30. try:
  31. cur = conn.cursor()
  32. n = 0
  33. for arxiv_id, title in items:
  34. aid = _norm_arxiv_id(arxiv_id)
  35. t = (title or "").strip()
  36. cur.execute(
  37. """
  38. INSERT INTO daily_recommendations(date_key, source, arxiv_id, title, created_at)
  39. VALUES(?,?,?,?,?)
  40. """,
  41. (str(date_key), "arxiv", aid, t[:400] if t else None, int(now)),
  42. )
  43. n += 1
  44. conn.commit()
  45. return n
  46. finally:
  47. conn.close()