aboutsummaryrefslogtreecommitdiff
path: root/code/utils/jellyfin_delete.py
blob: a0ff646df29e3631b374344b7ae251fa5cdf51ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import datetime
import requests

from utils.database import Session
from utils.models import JellyfinAccounts
from utils.config import LOG, JELLYFIN_URL, JELLYFIN_HEADERS


def delete_accounts():
    """
    Delete Jellyfin accounts that have passed their deletion time
    """
    # Get all expired Jellyfin accounts
    with Session() as session:
        jellyfin_user_ids = (
            session.query(JellyfinAccounts.jellyfin_user_id)
            .filter(JellyfinAccounts.deletion_time < datetime.datetime.now())
            .all()
        )

        # Delete each account
        for jellyfin_user_id in jellyfin_user_ids:
            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()