aboutsummaryrefslogtreecommitdiff
path: root/code/utils/jellyfin_create.py
blob: 43ad6a63acd882510166a3b0eef0476685e0fcde (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import datetime
import requests
import random
from wonderwords import RandomWord
from string import ascii_lowercase, digits

from utils.database import Session
from utils.models import JellyfinAccounts
from utils.config import (
    JELLYFIN_URL,
    JELLYFIN_HEADERS,
    ACCOUNT_TIME,
    SIMPLE_PASSWORDS,
)


def create_jellyfin_account(user_id):
    """
    Create a new Jellyfin account for the user and return the username and password

    Args:
        user_id (int): Discord user ID to create the account for

    Returns:
        tuple: The username and password of the new Jellyfin account
    """
    # Create username/password
    username = RandomWord().word(word_min_length=5, word_max_length=5)
    if SIMPLE_PASSWORDS:
        password = RandomWord().word(word_min_length=5, word_max_length=10)
    else:
        password = "".join(random.choices(ascii_lowercase + digits, k=15))

    deletion_time = datetime.datetime.now() + datetime.timedelta(
        minutes=ACCOUNT_TIME * 60
    )
    # Create the new Jellyfin account
    request_1 = requests.post(
        f"{JELLYFIN_URL}/Users/New",
        headers=JELLYFIN_HEADERS,
        json={"Name": username, "Password": password},
    )

    if request_1.status_code != 200:
        return False

    # Get the user ID of the new account
    jellyfin_user_id = request_1.json()["Id"]
    # Get the account policy and make edits
    request_2 = requests.get(
        f"{JELLYFIN_URL}/Users/{jellyfin_user_id}", headers=JELLYFIN_HEADERS
    )
    if request_2.status_code != 200:
        return False

    account_policy = request_2.json()["Policy"]
    account_policy["EnableSharedDeviceControl"] = False
    account_policy["EnableRemoteAccess"] = False
    account_policy["EnableLiveTvManagement"] = False
    account_policy["EnableContentDownloading"] = False
    account_policy["LoginAttemptsBeforeLockout"] = 3
    account_policy["MaxActiveSessions"] = 1
    account_policy["EnablePublicSharing"] = False
    account_policy["RemoteClientBitrateLimit"] = 15000000
    account_policy["SyncPlayAccess"] = "JoinGroups"
    # Update the user with the newly edited policy
    request_3 = requests.post(
        f"{JELLYFIN_URL}/Users/{jellyfin_user_id}/Policy",
        headers=JELLYFIN_HEADERS,
        json=account_policy,
    )
    if request_3.status_code != 204:
        return False

    # Add the information to the database
    with Session() as session:
        session.add(
            JellyfinAccounts(
                user_id=user_id,
                jellyfin_user_id=jellyfin_user_id,
                deletion_time=deletion_time,
            )
        )
        session.commit()

    return username, password