From bcdcdd682e899add97f4b2db4e0235390f07500f Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Thu, 2 Nov 2017 11:50:57 -0400 Subject: [PATCH] Implemented the 'Change login' option --- actions.py | 24 ++++++++++++++++++++++++ checks.py | 5 +++-- launcher.py | 19 +++++++++++++------ login.py | 21 +++++++++++---------- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/actions.py b/actions.py index dd7acef..d4a71a6 100644 --- a/actions.py +++ b/actions.py @@ -1,10 +1,34 @@ import requests import shutil import json +import os def refreshToken(): print("Not implemented yet") +def clearLogin(): + try: + os.remove("login.json") + except Exception as e: + pass + +def clearFolders():# TODO implement this + if os.path.exists("banner"): + print("cleared") + else: + print("empty") + + if os.path.exists("fanart"): + print("cleared") + else: + print("empty") + + if os.path.exists("poster"): + print("cleared") + else: + print("empty") + + def getImages(idNum, keyType, authHeaders): imageUrl = "https://api.thetvdb.com/series/" + str(idNum) + "/images/query" + keyType response = requests.get(imageUrl, headers=authHeaders) diff --git a/checks.py b/checks.py index a3d6aaa..fccf1ab 100644 --- a/checks.py +++ b/checks.py @@ -24,9 +24,10 @@ def getToken(data):#TODO add a timeout and try catch to all requests def checkStatus(response, v): if (response.status_code != 200): if (v == True): - print("An error occurred.") + print("\nAn error occurred.") print("HTTP Code: " + str(response.status_code)) - print("Response : " + str(response.content)) + error = json.loads(response.content) + print("Response : " + error["Error"]) return False else: return True diff --git a/launcher.py b/launcher.py index abb11b3..3ecb0b2 100644 --- a/launcher.py +++ b/launcher.py @@ -1,4 +1,7 @@ import os +from login import * +from actions import * + def clear_screen(): IS_WINDOWS = os.name == "nt" if IS_WINDOWS: @@ -9,6 +12,8 @@ def clear_screen(): def user_choice(): return input("> ").lower().strip() +def wait(): + input("Press enter to continue.") while True: clear_screen() @@ -30,14 +35,16 @@ while True: print("Search") break elif choice == "2": - print("Clear download") - break + clearFolders() + wait() elif choice == "3": - print("Change login") - break + clear_screen() + clearLogin() + login() + wait() elif choice == "4": - print("Refresh token") - break + refreshToken()# TODO implement this + wait() elif choice == "5": print("install requirements") break diff --git a/login.py b/login.py index d16c5e3..5ffd798 100644 --- a/login.py +++ b/login.py @@ -20,16 +20,18 @@ def login(): "TIMESTAMP": "" } - tmp_api_key = login["API_KEY"] - tmp_user_key = login["USER_KEY"] - tmp_user_name = login["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") while tmp_api_key is "": - tmp_api_key = input("Please enter your api key: ") + tmp_api_key = input("Enter your api key: ") while tmp_user_key is "": - tmp_user_key = input("Please enter your user key: ") + tmp_user_key = input("Enter your user key: ") while tmp_user_name is "": - tmp_user_name = input("Please enter your username: ") + tmp_user_name = input("Enter your username: ") LOGIN_DATA = { "apikey": tmp_api_key, @@ -40,7 +42,7 @@ def login(): tmp_token = getToken(LOGIN_DATA) if tmp_token is "": - print("Authentication failed. Please try again.") + print("\nAuthentication failed. Please try again.") else: login["API_KEY"] = tmp_api_key login["USER_KEY"] = tmp_user_key @@ -51,12 +53,11 @@ def login(): obj.write(json.dumps(login)) obj.close() else:# if login.json already exists - - with open("login.json") as json_data: + with open("login.json") as json_data:# TODO add a check for a login 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) + curTime = datetime.datetime.now().replace(tzinfo=None)# TODO use UTC time? if checkTimestamp(saveTime, curTime):# token does not need refreshed print("token is good")