cwa import

This commit is contained in:
2026-05-13 18:24:55 +02:00
parent bfa09976b7
commit c0e1cb0688
11 changed files with 60 additions and 886 deletions
+14 -65
View File
@@ -47,26 +47,16 @@ def init_db() -> None:
error_msg TEXT
);
CREATE TABLE IF NOT EXISTS uploaded_books (
id INTEGER PRIMARY KEY,
filename TEXT NOT NULL,
file_hash TEXT UNIQUE NOT NULL,
zip_source TEXT,
uploaded_at TEXT,
status TEXT
);
CREATE TABLE IF NOT EXISTS sync_runs (
id INTEGER PRIMARY KEY,
started_at TEXT NOT NULL,
finished_at TEXT,
zips_found INTEGER DEFAULT 0,
zips_new INTEGER DEFAULT 0,
books_uploaded INTEGER DEFAULT 0,
books_skipped INTEGER DEFAULT 0,
id INTEGER PRIMARY KEY,
started_at TEXT NOT NULL,
finished_at TEXT,
zips_found INTEGER DEFAULT 0,
zips_new INTEGER DEFAULT 0,
books_imported INTEGER DEFAULT 0,
books_errored INTEGER DEFAULT 0,
status TEXT DEFAULT 'running',
error_msg TEXT
status TEXT DEFAULT 'running',
error_msg TEXT
);
CREATE TABLE IF NOT EXISTS remote_zip_cache (
@@ -171,40 +161,6 @@ def get_recent_zips(limit: int = 50) -> list[sqlite3.Row]:
).fetchall()
# --- Uploaded books ---
def is_book_uploaded(file_hash: str) -> bool:
with get_db() as conn:
row = conn.execute(
"SELECT id FROM uploaded_books WHERE file_hash = ? AND status IN ('uploaded', 'skipped_duplicate')",
(file_hash,),
).fetchone()
return row is not None
def record_book(filename: str, file_hash: str, zip_source: str, status: str) -> None:
with get_db() as conn:
conn.execute(
"""INSERT INTO uploaded_books (filename, file_hash, zip_source, uploaded_at, status)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(file_hash) DO UPDATE SET status = excluded.status""",
(filename, file_hash, zip_source, _now(), status),
)
def get_books(limit: int = 200, offset: int = 0) -> list[sqlite3.Row]:
with get_db() as conn:
return conn.execute(
"SELECT * FROM uploaded_books ORDER BY uploaded_at DESC LIMIT ? OFFSET ?",
(limit, offset),
).fetchall()
def get_books_count() -> int:
with get_db() as conn:
return conn.execute("SELECT COUNT(*) FROM uploaded_books").fetchone()[0]
# --- Sync runs ---
def start_sync_run() -> int:
@@ -233,35 +189,28 @@ def get_recent_runs(limit: int = 10) -> list[sqlite3.Row]:
def get_stats() -> dict:
with get_db() as conn:
total_books = conn.execute("SELECT COUNT(*) FROM uploaded_books").fetchone()[0]
uploaded = conn.execute(
"SELECT COUNT(*) FROM uploaded_books WHERE status = 'uploaded'"
).fetchone()[0]
skipped = conn.execute(
"SELECT COUNT(*) FROM uploaded_books WHERE status = 'skipped_duplicate'"
).fetchone()[0]
total_zips = conn.execute("SELECT COUNT(*) FROM processed_zips").fetchone()[0]
total_imported = conn.execute(
"SELECT COALESCE(SUM(books_imported), 0) FROM sync_runs"
).fetchone()[0]
last_run = conn.execute(
"SELECT started_at, status FROM sync_runs ORDER BY started_at DESC LIMIT 1"
).fetchone()
return {
"total_books": total_books,
"uploaded": uploaded,
"skipped": skipped,
"total_zips": total_zips,
"total_imported": total_imported,
"last_run": dict(last_run) if last_run else None,
}
def clear_sync_data() -> dict:
"""Delete all processed_zips, uploaded_books, and sync_runs rows. Settings are kept.
"""Delete all processed_zips and sync_runs rows. Settings are kept.
Also resets the remote scan timestamp so the next sync does a full rescan."""
with get_db() as conn:
zips = conn.execute("DELETE FROM processed_zips").rowcount
books = conn.execute("DELETE FROM uploaded_books").rowcount
runs = conn.execute("DELETE FROM sync_runs").rowcount
conn.execute("DELETE FROM settings WHERE key = 'remote_cache_last_scan'")
return {"zips": zips, "books": books, "runs": runs}
return {"zips": zips, "runs": runs}
def _now() -> str: