Moved to new category type, overhauled sending notifications #3
No files matched your search
+2
-1
@@ -13,7 +13,8 @@ httpx==0.28.1
|
|||||||
idna==3.20
|
idna==3.20
|
||||||
lib-db-module==0.4.0
|
lib-db-module==0.4.0
|
||||||
lib-secret-manager==0.3.6
|
lib-secret-manager==0.3.6
|
||||||
lib-send-notifications==0.2.2
|
lib-send-notifications==0.3.0
|
||||||
|
lib-shared-models==0.2.0
|
||||||
lib-service-auth==0.1.0
|
lib-service-auth==0.1.0
|
||||||
msgspec==0.22.0
|
msgspec==0.22.0
|
||||||
mysql-connector-python==26.10.0
|
mysql-connector-python==26.10.0
|
||||||
|
|||||||
+12
-35
@@ -1,13 +1,14 @@
|
|||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||||
from seadex import SeaDexEntry
|
from seadex import SeaDexEntry
|
||||||
|
from shared_models import Category, Message
|
||||||
|
|
||||||
from auth_config import Caller, auth
|
from auth_config import Caller, auth
|
||||||
from db_module import (
|
from db_module import (
|
||||||
get_db,
|
get_db,
|
||||||
)
|
)
|
||||||
from seadex_client import check_for_update
|
from seadex_client import check_for_update
|
||||||
from send_notifications import NotificationError, NotificationUnavailableError
|
from send_notifications import send_to_users
|
||||||
from simple_logger_handler import setup_logger
|
from simple_logger_handler import setup_logger
|
||||||
|
|
||||||
logger = setup_logger(__name__)
|
logger = setup_logger(__name__)
|
||||||
@@ -41,10 +42,7 @@ def check_seadex(request:Request, db=Depends(get_db)):
|
|||||||
if current == last_updated:
|
if current == last_updated:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
logger.info(
|
logger.info("[FastAPI] Anime: %s updated, Anilist ID: %s",anime_name,anilist_id,)
|
||||||
"[FastAPI] Anime: %s updated, Anilist ID: %s",
|
|
||||||
anime_name,anilist_id,
|
|
||||||
)
|
|
||||||
with db.cursor() as cursor:
|
with db.cursor() as cursor:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT user_id, custom_name "
|
"SELECT user_id, custom_name "
|
||||||
@@ -54,35 +52,18 @@ def check_seadex(request:Request, db=Depends(get_db)):
|
|||||||
)
|
)
|
||||||
subscribers = cursor.fetchall()
|
subscribers = cursor.fetchall()
|
||||||
|
|
||||||
sent = 0
|
notifications = []
|
||||||
failed = 0
|
|
||||||
for user_id, custom_name in subscribers:
|
for user_id, custom_name in subscribers:
|
||||||
try:
|
notifications.append((user_id,Message(
|
||||||
request.app.state.notifier.send(
|
|
||||||
title=custom_name or anime_name,
|
title=custom_name or anime_name,
|
||||||
body="SeaDex entry updated",
|
body="SeaDex entry updated",
|
||||||
link=f"https://seadex.moe/{anilist_id}/",
|
link=f"https://seadex.moe/{anilist_id}/",
|
||||||
user_id=user_id,
|
category=Category.WATCHING,
|
||||||
)
|
)))
|
||||||
sent += 1
|
_,failed = send_to_users(request.app.state.notifier,notifications)
|
||||||
except NotificationUnavailableError:
|
results["new"] += 1
|
||||||
failed = len(subscribers) - sent
|
results["errors"] += failed
|
||||||
logger.error("[FastAPI] Notifications backend unavailable, skipping "
|
if failed == 0:
|
||||||
"%s remaining user(s) for Anime: %s",
|
|
||||||
failed, anime_name,
|
|
||||||
)
|
|
||||||
break
|
|
||||||
except NotificationError as e:
|
|
||||||
failed += 1
|
|
||||||
logger.error(
|
|
||||||
"[FastAPI] Failed to send notification to user_id=%s for Anime: %s, status=%s",
|
|
||||||
user_id, anime_name, getattr(e, 'status_code',None),
|
|
||||||
)
|
|
||||||
if subscribers and sent == 0:
|
|
||||||
results["errors"] += 1
|
|
||||||
logger.error("[FastAPI] All notifications failed for Anime: %s, will retry later", anime_name)
|
|
||||||
continue
|
|
||||||
|
|
||||||
with db.cursor() as cursor:
|
with db.cursor() as cursor:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"UPDATE seadex_state SET last_updated = %s WHERE id = %s",
|
"UPDATE seadex_state SET last_updated = %s WHERE id = %s",
|
||||||
@@ -90,15 +71,11 @@ def check_seadex(request:Request, db=Depends(get_db)):
|
|||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
logger.debug("[FastAPI] Notification sent and database updated for Anime: %s", anime_name)
|
logger.debug("[FastAPI] Notification sent and database updated for Anime: %s", anime_name)
|
||||||
results["new"] += 1
|
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
results["errors"] += 1
|
results["errors"] += 1
|
||||||
logger.exception(
|
logger.exception("[FastAPI] Failed for id: %s Anilist: %s",seadex_state_id, anilist_id,)
|
||||||
"[FastAPI] Failed for id: %s Anilist: %s",
|
|
||||||
seadex_state_id, anilist_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
if results["errors"]:
|
if results["errors"]:
|
||||||
logger.warning("[FastAPI] SeaDex check finished with errors: %s", results)
|
logger.warning("[FastAPI] SeaDex check finished with errors: %s", results)
|
||||||
|
|||||||
Reference in new issue
Block a user