Fix watchlist download sync for resolved IDs

This commit is contained in:
Dymas
2026-05-17 21:35:49 +02:00
parent 9e918a50ab
commit 2a0da335b2
6 changed files with 106 additions and 36 deletions
+84 -34
View File
@@ -1001,6 +1001,29 @@ class WatchlistStore:
with self.lock, self._connect() as conn:
return self._exists_conn(conn, normalized_show_id)
def _find_download_title_match_conn(self, conn, title, exclude_show_id=""):
normalized_title_key = normalize_identity_title_key(title)
excluded_show_id = str(exclude_show_id or "").strip()
if not normalized_title_key:
return None
rows = conn.execute(
"""
SELECT *
FROM watchlist
ORDER BY updated_at DESC, created_at DESC, title COLLATE NOCASE ASC
"""
).fetchall()
matches = []
for row in rows:
row_show_id = str(row["show_id"] or "").strip()
if not row_show_id or row_show_id == excluded_show_id:
continue
if normalize_identity_title_key(row["title"]) == normalized_title_key:
matches.append(row)
if len(matches) > 1:
return None
return matches[0] if matches else None
def _init_db(self):
STATE_DB_PATH.parent.mkdir(parents=True, exist_ok=True)
with self._connect() as conn:
@@ -1460,47 +1483,74 @@ class WatchlistStore:
if not normalized_show_id:
raise ValueError("Missing show_id for watchlist download sync.")
created = False
try:
existing = self.get(normalized_show_id)
except KeyError:
created = True
now = now_iso()
existing = {
"show_id": normalized_show_id,
"title": str(title or "Unknown Anime").strip() or "Unknown Anime",
"category": "finished",
"downloaded": True,
"status": "queued",
"status_message": "Queued refresh after successful download.",
"created_at": now,
"updated_at": now,
"last_checked": None,
"sub_count": 0,
"dub_count": 0,
"expected_count": 0,
"airing_status": None,
"sub_latest_episode": None,
"dub_latest_episode": None,
"animeschedule_route": None,
"animeschedule_title": None,
"anidb_aid": None,
"anidb_title": None,
"thumbnail_path": None,
"thumbnail_checked_at": None,
}
with self.lock, self._connect() as conn:
self._upsert_conn(conn, existing)
else:
with self.lock, self._connect() as conn:
normalized_title = str(title or "").strip()
now = now_iso()
with self.lock, self._connect() as conn:
row = conn.execute("SELECT * FROM watchlist WHERE show_id = ?", (normalized_show_id,)).fetchone()
if row:
conn.execute(
"""
UPDATE watchlist
SET downloaded = 1, status = 'queued', status_message = ?, updated_at = ?
WHERE show_id = ?
""",
("Queued refresh after successful download.", now_iso(), normalized_show_id),
("Queued refresh after successful download.", now, normalized_show_id),
)
else:
matched_row = self._find_download_title_match_conn(conn, normalized_title, exclude_show_id=normalized_show_id)
if matched_row is not None:
previous_show_id = str(matched_row["show_id"]).strip()
resolved_title = normalized_title or str(matched_row["title"] or "").strip() or "Unknown Anime"
conn.execute(
"""
UPDATE watchlist
SET
show_id = ?,
title = ?,
downloaded = 1,
status = 'queued',
status_message = ?,
updated_at = ?
WHERE show_id = ?
""",
(
normalized_show_id,
resolved_title,
"Queued refresh after successful download.",
now,
previous_show_id,
),
)
if previous_show_id != normalized_show_id:
event = self.thumbnail_events.pop(previous_show_id, None)
if event is not None:
self.thumbnail_events[normalized_show_id] = event
self._drop_refresh_locked(previous_show_id)
else:
existing = {
"show_id": normalized_show_id,
"title": normalized_title or "Unknown Anime",
"category": "finished",
"downloaded": True,
"status": "queued",
"status_message": "Queued refresh after successful download.",
"created_at": now,
"updated_at": now,
"last_checked": None,
"sub_count": 0,
"dub_count": 0,
"expected_count": 0,
"airing_status": None,
"sub_latest_episode": None,
"dub_latest_episode": None,
"animeschedule_route": None,
"animeschedule_title": None,
"anidb_aid": None,
"anidb_title": None,
"thumbnail_path": None,
"thumbnail_checked_at": None,
}
self._upsert_conn(conn, existing)
self.schedule_refresh(normalized_show_id)
return self.get(normalized_show_id)