From d31e4d563ea91d519c7a8cb7a6a6fc99c2675c05 Mon Sep 17 00:00:00 2001 From: ClaytonWWilson Date: Fri, 13 Jul 2018 09:15:59 -0400 Subject: [PATCH] Streamlining the login and token fetching process --- actions.py | 6 ---- launcher.py | 3 +- login.py | 82 +++++++++++++++++++++++------------------------------ search.py | 2 -- 4 files changed, 37 insertions(+), 56 deletions(-) diff --git a/actions.py b/actions.py index 083e1f5..1b3e2fb 100644 --- a/actions.py +++ b/actions.py @@ -65,12 +65,6 @@ def refreshToken(): else: print("You need to log in first. Select Login/Change login.\n") -def clearLogin(): - try: - os.remove("login.json") - except Exception as e: - pass - def clearFolders(): # TODO implement this folders = ["banner", "fanart", "poster"] del_count = 0 diff --git a/launcher.py b/launcher.py index 1d2fe1f..b66af58 100644 --- a/launcher.py +++ b/launcher.py @@ -4,7 +4,6 @@ from login import login from actions import wait from actions import clear_screen from actions import clearFolders -from actions import clearLogin from actions import refreshToken from actions import update from search import search @@ -39,7 +38,7 @@ while True: wait() elif choice == "3": # TODO if already logged in, ask 'are you sure?' clear_screen() # TODO wait to clear login and add a ctrl+c option to cancel - clearLogin() + # clearLogin() login() wait() # elif choice == "4": # TODO need to perform this option automatically diff --git a/login.py b/login.py index 7524021..3b14388 100644 --- a/login.py +++ b/login.py @@ -5,6 +5,7 @@ import datetime import dateutil.parser from actions import refreshToken +from checks import checkTimestamp from checks import getToken @@ -14,57 +15,46 @@ def login(): obj.write("") obj.close() - if os.stat("login.json").st_size == 0:# Will only ask for credentials if the login file is empty - login = { - "API_KEY": "", - "USER_KEY": "", - "USER_NAME": "", - "TOKEN": "", - "TIMESTAMP": "" - } + login = { + "API_KEY": "", + "USER_KEY": "", + "USER_NAME": "", + "TOKEN": "", + "TIMESTAMP": "" + } - tmp_api_key = "" - tmp_user_key = "" - tmp_user_name = "" + tmp_api_key = "" + tmp_user_key = "" + tmp_user_name = "" - print("You can find your user key & request an API key while logged in at:\nhttps://www.thetvdb.com/?tab=userinfo\n") + print("You can find your user key & request an API key while logged in at:\nhttps://www.thetvdb.com/?tab=userinfo\n") - while tmp_api_key is "": - tmp_api_key = input("Enter your api key: ") - while tmp_user_key is "": - tmp_user_key = input("Enter your user key: ") - while tmp_user_name is "": - tmp_user_name = input("Enter your username: ") + while tmp_api_key is "": + tmp_api_key = input("Enter your api key: ") + while tmp_user_key is "": + tmp_user_key = input("Enter your user key: ") + while tmp_user_name is "": + tmp_user_name = input("Enter your username: ") - LOGIN_DATA = { - "apikey": tmp_api_key, - "userkey": tmp_user_key, - "username": tmp_user_name - } + LOGIN_DATA = { + "apikey": tmp_api_key, + "userkey": tmp_user_key, + "username": tmp_user_name + } - tmp_token = getToken(LOGIN_DATA) + tmp_token = getToken(LOGIN_DATA) - if tmp_token is "": - print("\nAuthentication failed. Please try again.") - else: - login["API_KEY"] = tmp_api_key - login["USER_KEY"] = tmp_user_key - login["USER_NAME"] = tmp_user_name - login["TOKEN"] = tmp_token - login["TIMESTAMP"] = str(datetime.datetime.now().replace(tzinfo=None)) - obj = open("login.json", "w") - obj.write(json.dumps(login)) - obj.close() - print("\nLogin successful!\n") - else:# if login.json already exists - with open("login.json") as json_data:# TODO add a check for a login file that is damaged/modified - login = json.load(json_data) - json_data.close() - saveTime = dateutil.parser.parse(login["TIMESTAMP"]) - curTime = datetime.datetime.now().replace(tzinfo=None)# TODO use UTC time? + if tmp_token is "": + print("\nAuthentication failed. Please try again.") + else: + login["API_KEY"] = tmp_api_key + login["USER_KEY"] = tmp_user_key + login["USER_NAME"] = tmp_user_name + login["TOKEN"] = tmp_token + login["TIMESTAMP"] = str(datetime.datetime.now().replace(tzinfo=None)) + obj = open("login.json", "w") + obj.write(json.dumps(login)) + obj.close() + print("\nLogin successful!\n") - if checkTimestamp(saveTime, curTime):# token does not need refreshed - print("token is good") - else: - refreshToken() # TODO at startup, check token for validity and remove it if it is expired diff --git a/search.py b/search.py index d402ed6..cce9279 100644 --- a/search.py +++ b/search.py @@ -27,8 +27,6 @@ def search(): saveTime = dateutil.parser.parse(login["TIMESTAMP"]) curTime = datetime.datetime.now().replace(tzinfo=None) # TODO use UTC time? if checkTimestamp(saveTime, curTime) == False: - # print("Your token has expired. Get a new one by choosing Refresh Token.") - # return None refreshToken() except Exception as ex: print(ex)