Add Discord webhook refresh notifications

This commit is contained in:
Dymas
2026-05-21 18:56:49 +02:00
parent a95b4c343f
commit 9648b4f6e4
9 changed files with 638 additions and 54 deletions
+20 -22
View File
@@ -34,9 +34,9 @@ from app_support import (
APP_NAME,
CONFIG_PATH,
DEFAULT_CONFIG,
LEGACY_THUMBNAIL_ROOT,
MAX_JSON_BODY_BYTES,
MODE_CHOICES,
send_discord_webhook_event,
STATE_DB_PATH,
THUMBNAIL_RETRY_SECONDS,
THUMBNAIL_ROOT,
@@ -54,6 +54,7 @@ from app_support import (
normalize_config,
now_iso,
sanitize_path_component,
thumbnail_file_path,
write_json,
)
from http_handler import HandlerContext, build_handler_class, server_host, server_port
@@ -134,26 +135,6 @@ def thumbnail_retry_due(checked_at):
return age.total_seconds() >= THUMBNAIL_RETRY_SECONDS
def thumbnail_file_path(name):
filename = Path(str(name or "")).name
if not filename:
return None
project_path = THUMBNAIL_ROOT / filename
if project_path.exists():
return project_path
legacy_path = LEGACY_THUMBNAIL_ROOT / filename
if legacy_path.exists():
THUMBNAIL_ROOT.mkdir(parents=True, exist_ok=True)
try:
shutil.move(str(legacy_path), str(project_path))
debug_log("thumbnail.cache.migrated", from_path=legacy_path, to_path=project_path)
return project_path
except OSError as exc:
debug_log("thumbnail.cache.migrate_failed", from_path=legacy_path, to_path=project_path, error=exc)
return legacy_path
return project_path
def cover_route(show_id):
return f"/api/watchlist/thumb/{quote(str(show_id).strip(), safe='')}"
@@ -1347,6 +1328,8 @@ class WatchlistStore:
def refresh(self, show_id, include_thumbnail=True):
existing = self.get(show_id)
normalized_show_id = existing["show_id"]
previous_sub_count = int(existing.get("sub_count") or 0)
previous_dub_count = int(existing.get("dub_count") or 0)
now = now_iso()
request_timeout = self._refresh_request_timeout()
try:
@@ -1448,7 +1431,13 @@ class WatchlistStore:
self.ensure_thumbnail(normalized_show_id)
except Exception:
pass
return self.get(normalized_show_id)
item = self.get(normalized_show_id)
item["previous_sub_count"] = previous_sub_count
item["previous_dub_count"] = previous_dub_count
item["sub_delta"] = max(0, int(item.get("sub_count") or 0) - previous_sub_count)
item["dub_delta"] = max(0, int(item.get("dub_count") or 0) - previous_dub_count)
item["had_new_episodes"] = bool(item["sub_delta"] or item["dub_delta"])
return item
def refresh_all(self):
refreshed = []
@@ -1842,6 +1831,14 @@ def save_runtime_config(config):
return dict(CONFIG)
def test_discord_webhook_config(config):
normalized = normalize_config(config)
if not normalized.get("discord_webhook_url"):
raise ValueError("Discord webhook URL is required for testing.")
send_discord_webhook_event(normalized, "test", force=True)
return {"message": "Discord webhook test notification sent."}
def runtime_state():
return {
"config": CONFIG,
@@ -1981,6 +1978,7 @@ Handler = build_handler_class(
runtime_state=runtime_state,
save_runtime_config=save_runtime_config,
search_anime=search_anime,
test_discord_webhook_config=test_discord_webhook_config,
thumbnail_file_path=thumbnail_file_path,
update_all_watchlist_statuses=update_all_watchlist_statuses,
update_watchlist_category=update_watchlist_category,