diff --git a/app/commands/movie_show_response_newaccount.py b/app/commands/create_jellyfin_account.py similarity index 61% rename from app/commands/movie_show_response_newaccount.py rename to app/commands/create_jellyfin_account.py index 6922560..e618eba 100644 --- a/app/commands/movie_show_response_newaccount.py +++ b/app/commands/create_jellyfin_account.py @@ -8,32 +8,12 @@ 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 - +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=active_time) + 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: @@ -68,5 +48,5 @@ def movie_show_response_newaccount(from_number, message): db.commit() db.close() - create_message(from_number, f"Username: {username}\nPassword: {password}\n\nYour account will expire in {active_time} hours.") + create_message(from_number, f"Username: {username}\nPassword: {password}\n\nYour account will expire in 4 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