46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
"""History from streamrip sqlite DBs (read-only)."""
|
|
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
from fastapi import APIRouter
|
|
|
|
from app.core import settings
|
|
|
|
router = APIRouter(prefix="/api/history", tags=["history"])
|
|
|
|
|
|
def _read_ids(db_path: str, table: str) -> list:
|
|
p = Path(db_path)
|
|
if not p.exists():
|
|
return []
|
|
try:
|
|
conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True)
|
|
try:
|
|
if table == "downloads":
|
|
rows = conn.execute("SELECT id FROM downloads").fetchall()
|
|
return [{"id": r[0]} for r in rows]
|
|
rows = conn.execute("SELECT source, media_type, id FROM failed_downloads").fetchall()
|
|
return [{"source": r[0], "media_type": r[1], "id": r[2]} for r in rows]
|
|
finally:
|
|
conn.close()
|
|
except Exception:
|
|
return []
|
|
|
|
|
|
def _default_db_paths() -> tuple[str, str]:
|
|
cfg_dir = Path(settings.CONFIG_PATH).parent
|
|
return (
|
|
str(cfg_dir / "downloads.db"),
|
|
str(cfg_dir / "failed_downloads.db"),
|
|
)
|
|
|
|
|
|
@router.get("")
|
|
def get_history():
|
|
dl_path, failed_path = _default_db_paths()
|
|
return {
|
|
"downloads": _read_ids(dl_path, "downloads"),
|
|
"failed": _read_ids(failed_path, "failed_downloads"),
|
|
}
|