From 517c0b328ca864242b4c950f9030f853ea414e69 Mon Sep 17 00:00:00 2001 From: Parker Date: Tue, 30 Apr 2024 21:22:19 -0500 Subject: Remove 24 hour accounts Remove the additional will you be watching a show or movie question as well as 24 hour accounts. All accounts are now just available for 4 hours. --- app/commands/create_jellyfin_account.py | 52 +++++++++++++++++++ app/commands/movie_show_response_newaccount.py | 72 -------------------------- app/initialize_variables.py | 4 +- app/messagearr.py | 26 ++++------ 4 files changed, 65 insertions(+), 89 deletions(-) create mode 100644 app/commands/create_jellyfin_account.py delete mode 100644 app/commands/movie_show_response_newaccount.py diff --git a/app/commands/create_jellyfin_account.py b/app/commands/create_jellyfin_account.py new file mode 100644 index 0000000..e618eba --- /dev/null +++ b/app/commands/create_jellyfin_account.py @@ -0,0 +1,52 @@ +import datetime +import requests +import random +import string +import sqlite3 + +import initialize_variables +from create_message import create_message + + +def create_jellyfin_account(from_number): + # Otherwise, all checks have been completed + username = ''.join(random.choices(string.ascii_lowercase + string.digits, k=5)) + password = ''.join(random.choices(string.ascii_lowercase + string.digits, k=15)) + + deletion_time = datetime.datetime.now() + datetime.timedelta(hours=4) + # Create new Jellyfin account + request_1 = requests.post(f'{initialize_variables.jellyfin_url}/Users/New', headers=initialize_variables.jellyfin_headers, json={'Name': username, 'Password': password}) + if request_1.status_code != 200: + create_message(from_number, "Error creating Jellyfin account. Please try again. If the error persists, contact Parker.") + return + + user_id = request_1.json()['Id'] + # Get account policy and make edits + request_2 = requests.get(f'{initialize_variables.jellyfin_url}/Users/{user_id}', headers=initialize_variables.jellyfin_headers) + if request_2.status_code != 200: + create_message(from_number, "Error creating Jellyfin account. Please try again. If the error persists, contact Parker.") + return + + policy = request_2.json()['Policy'] + policy['SyncPlayAccess'] = 'JoinGroups' + policy['EnableContentDownloading'] = False + policy['InvalidLoginAttemptCount'] = 3 + policy['MaxActiveSessions'] = 1 + # Update user with new policy + request_3 = requests.post(f'{initialize_variables.jellyfin_url}/Users/{user_id}/Policy', headers=initialize_variables.jellyfin_headers, json=policy) + if request_3.status_code != 204: + create_message(from_number, "Error creating Jellyfin account. Please try again. If the error persists, contact Parker.") + return + + # Add information to the database + db = sqlite3.connect(initialize_variables.db_path) + cursor = db.cursor() + cursor.execute(''' + INSERT INTO jellyfin_accounts (user_id, deletion_time) + VALUES(?, ?) + ''', (user_id, deletion_time)) + db.commit() + db.close() + + create_message(from_number, f"Username: {username}\nPassword: {password}\n\nYour account will expire in 4 hours.") + return diff --git a/app/commands/movie_show_response_newaccount.py b/app/commands/movie_show_response_newaccount.py deleted file mode 100644 index 6922560..0000000 --- a/app/commands/movie_show_response_newaccount.py +++ /dev/null @@ -1,72 +0,0 @@ -import datetime -import requests -import random -import string -import sqlite3 - -import initialize_variables -from create_message import create_message - - -def movie_show_response_newaccount(from_number, message): - if from_number not in initialize_variables.temp_new_account_requests.keys(): - create_message(from_number, "There is no current request that you can decide on. It might be that your /newaccount command timed out due since you took too long to response. Please try again. If this issue persists, please contact Parker.") - return - - # If its been 5 minutes since prompt was sent, alert user of timed out request - if (datetime.datetime.now() - initialize_variables.temp_new_account_requests[from_number]).total_seconds() / 60 > 5: - del initialize_variables.temp_new_account_requests[from_number] - create_message(from_number, "You waited too long and therefore your request has timed out.\n\nPlease try again by re-running the /newaccount command. If this issue persists, please contact Parker.") - return - - if message.strip().lower() == "show": - active_time = 24 - - elif message.strip().lower() == "movie": - active_time = 4 - - else: - create_message(from_number, "You did not enter a valid response. Please re-send the /newaccount command and try again. If you believe this is an error, please contact Parker.") - return - - # Otherwise, all checks have been completed - username = ''.join(random.choices(string.ascii_lowercase + string.digits, k=5)) - password = ''.join(random.choices(string.ascii_lowercase + string.digits, k=15)) - - deletion_time = datetime.datetime.now() + datetime.timedelta(hours=active_time) - # Create new Jellyfin account - request_1 = requests.post(f'{initialize_variables.jellyfin_url}/Users/New', headers=initialize_variables.jellyfin_headers, json={'Name': username, 'Password': password}) - if request_1.status_code != 200: - create_message(from_number, "Error creating Jellyfin account. Please try again. If the error persists, contact Parker.") - return - - user_id = request_1.json()['Id'] - # Get account policy and make edits - request_2 = requests.get(f'{initialize_variables.jellyfin_url}/Users/{user_id}', headers=initialize_variables.jellyfin_headers) - if request_2.status_code != 200: - create_message(from_number, "Error creating Jellyfin account. Please try again. If the error persists, contact Parker.") - return - - policy = request_2.json()['Policy'] - policy['SyncPlayAccess'] = 'JoinGroups' - policy['EnableContentDownloading'] = False - policy['InvalidLoginAttemptCount'] = 3 - policy['MaxActiveSessions'] = 1 - # Update user with new policy - request_3 = requests.post(f'{initialize_variables.jellyfin_url}/Users/{user_id}/Policy', headers=initialize_variables.jellyfin_headers, json=policy) - if request_3.status_code != 204: - create_message(from_number, "Error creating Jellyfin account. Please try again. If the error persists, contact Parker.") - return - - # Add information to the database - db = sqlite3.connect(initialize_variables.db_path) - cursor = db.cursor() - cursor.execute(''' - INSERT INTO jellyfin_accounts (user_id, deletion_time) - VALUES(?, ?) - ''', (user_id, deletion_time)) - db.commit() - db.close() - - create_message(from_number, f"Username: {username}\nPassword: {password}\n\nYour account will expire in {active_time} hours.") - return diff --git a/app/initialize_variables.py b/app/initialize_variables.py index 09221f7..8b726e5 100644 --- a/app/initialize_variables.py +++ b/app/initialize_variables.py @@ -52,8 +52,8 @@ def init(): } } """ - global temp_new_account_requests - temp_new_account_requests = {} + global jellyfin_active_accounts + jellyfin_active_accounts = {} """ { 'from_number': 'time' diff --git a/app/messagearr.py b/app/messagearr.py index 8e6b8e4..cf80e02 100644 --- a/app/messagearr.py +++ b/app/messagearr.py @@ -5,7 +5,7 @@ from create_message import create_message from commands.request import request from commands.status import status from commands.number_response_request import number_response_request -from commands.movie_show_response_newaccount import movie_show_response_newaccount +from commands.create_jellyfin_account import create_jellyfin_account import initialize_variables app = flask.Flask(__name__) @@ -58,34 +58,30 @@ def incoming(): if from_number not in initialize_variables.valid_senders: return 'OK' - if message.startswith('/request'): + if message.strip().lower().startswith('/request'): request(from_number, message) return 'OK' # If a user responded with a number, they are responding to # the 'request' command prompt - elif message.strip() in initialize_variables.numbers_responses.keys(): + elif message.strip().lower() in initialize_variables.numbers_responses.keys(): number_response_request(from_number, message) return 'OK' - elif message.startswith('/status'): + elif message.strip().lower().startswith('/status'): status(from_number) return 'OK' - elif message.startswith('/newaccount'): + elif message.strip().lower().startswith('/newaccount'): if initialize_variables.enable_jellyfin_temp_accounts.lower() == 'true': # If number is already in the temp dict, delete it so that they can redo the request - if from_number in initialize_variables.temp_new_account_requests.keys(): - del initialize_variables.temp_new_account_requests[from_number] + if from_number in initialize_variables.jellyfin_active_accounts.keys(): + if datetime.datetime.now() - initialize_variables.jellyfin_active_accounts[from_number] < datetime.timedelta(hours=4): + create_message(from_number, "You already have an active account. Please wait until it expires to create a new one.") + return 'OK' - create_message(from_number, "Will you be watching a TV show or a movie?\n\nRespond with 'show' for TV show, 'movie' for movies") - initialize_variables.temp_new_account_requests[from_number] = datetime.datetime.now() - return 'OK' - - # User must be responding to above prompt - elif message.strip().lower() in ['show', 'movie']: - if initialize_variables.enable_jellyfin_temp_accounts.lower() == 'true': - movie_show_response_newaccount(from_number, message) + create_jellyfin_account(from_number) + initialize_variables.jellyfin_active_accounts[from_number] = datetime.datetime.now() return 'OK' # No valid commands were found, so just return -- cgit v1.2.3-70-g09d2