From 2c86a7ce8dd045c506bb16ea180b7cdb3475b0e7 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Wed, 1 Nov 2017 12:47:44 -0400 Subject: [PATCH] Saving login credentials to file --- checks.py | 13 ++++++++++++- login.py | 35 ++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/checks.py b/checks.py index 21e0168..a3d6aaa 100644 --- a/checks.py +++ b/checks.py @@ -1,5 +1,6 @@ import json import requests +import datetime def getToken(data):#TODO add a timeout and try catch to all requests url = "https://api.thetvdb.com/login" @@ -7,8 +8,12 @@ def getToken(data):#TODO add a timeout and try catch to all requests "Content-Type": "application/json", "Accept": "application/json" } + try: + response = requests.post(url, data=json.dumps(data), headers=headers) + except requests.exceptions.ConnectionError as e: + print("An error occurred. Please check your internet and try again.") + quit() - response = requests.post(url, data=json.dumps(data), headers=headers) if (checkStatus(response, True)): parsed_token = json.loads(response.content) token = parsed_token["token"] @@ -25,3 +30,9 @@ def checkStatus(response, v): return False else: return True + +def checkTimestamp(saveTime, curTime): + if curTime - saveTime < datetime.timedelta(0, 86100, 0):# if less than 23h 55m since last token grab + return True + else: + return False diff --git a/login.py b/login.py index 735a20b..6471b13 100644 --- a/login.py +++ b/login.py @@ -2,11 +2,12 @@ import json import os.path from checks import * import datetime +import dateutil.parser def login(): if os.path.exists("login.json") == False: - obj = open("login.json") - # obj.write() + obj = open("login.json", "w") + obj.write("") obj.close() if os.stat("login.json").st_size == 0:# Will only ask for credentials if the login file is empty @@ -36,17 +37,29 @@ def login(): } tmp_token = getToken(LOGIN_DATA) + if tmp_token is "": print("Authentication failed. Please try again.") else: - with open("login.json") as json_data: - login["API_KEY"] = tmp_api_key - login["USER_KEY"] = tmp_user_key - login["USER_NAME"] = tmp_user_name - login["TOKEN"] = tmp_token - login["TIMESTAMP"] = datetime.datetime.now() - obj = open("login.json", "w") - obj.write(json.dumps(login)) - obj.close() + 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() + else:# if login.json already exists + + with open("login.json") as json_data: + login = json.load(json_data) + json_data.close() + saveTime = dateutil.parser.parse(login["TIMESTAMP"]) + curTime = datetime.datetime.now().replace(tzinfo=None) + + if checkTimestamp(saveTime, curTime):# token does not need refreshed + print("token is good") + else: + print("token needs refreshed") # TODO try to get token, if token fails, ask for login info again, if it passes save login details to login.py and save token with timestamp. # TODO at startup, check token for validity and remove it if it is expired