diff options
author | Parker <contact@pkrm.dev> | 2025-01-22 16:07:47 -0600 |
---|---|---|
committer | Parker <contact@pkrm.dev> | 2025-01-22 16:07:47 -0600 |
commit | 557a646d65326065b60ea729034d4dbf4069749d (patch) | |
tree | c0618c25fff81257cc7645b1b761833202787431 /code/utils/jellyfin_delete.py | |
parent | 7092bd81c71c92296724bfcc5a6a54dac07727c1 (diff) | |
parent | b48f2a1eff20996901e8db40d68c880a5d7f053f (diff) |
Merge branch 'dev'
Diffstat (limited to 'code/utils/jellyfin_delete.py')
-rw-r--r-- | code/utils/jellyfin_delete.py | 60 |
1 files changed, 34 insertions, 26 deletions
diff --git a/code/utils/jellyfin_delete.py b/code/utils/jellyfin_delete.py index 66af00b..0e65201 100644 --- a/code/utils/jellyfin_delete.py +++ b/code/utils/jellyfin_delete.py @@ -1,8 +1,9 @@ import datetime -import sqlite3 import requests -from utils.config import JELLYFIN_URL, JELLYFIN_HEADERS +from utils.database import Session +from utils.models import JellyfinAccounts +from utils.config import LOG, JELLYFIN_URL, JELLYFIN_HEADERS def delete_accounts(): @@ -10,29 +11,36 @@ def delete_accounts(): Delete Jellyfin accounts that have passed their deletion time """ # Get all expired Jellyfin accounts - db = sqlite3.connect("data/cordarr.db") - cursor = db.cursor() - cursor.execute( - "SELECT jellyfin_user_id FROM jellyfin_accounts WHERE" - " deletion_time < ?", - (datetime.datetime.now(),), - ) - jellyfin_user_ids = cursor.fetchall() - - # Delete the Jellyfin accounts - for jellyfin_user_id in jellyfin_user_ids: - request = requests.delete( - f"{JELLYFIN_URL}/Users/{jellyfin_user_id[0]}", - headers=JELLYFIN_HEADERS, + with Session() as session: + jellyfin_user_ids = ( + session.query(JellyfinAccounts.jellyfin_user_id) + .filter(JellyfinAccounts.deletion_time < datetime.datetime.now()) + .all() ) - # If 204 - account deleted - # If 404 - account not found - # Either way, remove account from database - if request.status_code in (404, 204): - cursor.execute( - "DELETE FROM jellyfin_accounts WHERE jellyfin_user_id = ?", - (jellyfin_user_id,), - ) - db.commit() - db.close() + # Delete each account + for jellyfin_user_id in jellyfin_user_ids: + print(f"Deleting account {jellyfin_user_id[0]}") + try: + response = requests.delete( + f"{JELLYFIN_URL}/Users/{jellyfin_user_id[0]}", + headers=JELLYFIN_HEADERS, + ) + response.raise_for_status() + # Get the account and delete it + account = ( + session.query(JellyfinAccounts) + .filter( + JellyfinAccounts.jellyfin_user_id + == jellyfin_user_id[0] + ) + .first() + ) + session.delete(account) + except: + LOG.error( + "Failed deleting Jellyfin account w/ ID" + f" {jellyfin_user_id[0]}" + ) + # Commit changes + session.commit() |