From d559916c6492a1d1d7f41d975b4c3980a613c908 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Wed, 1 Nov 2017 08:10:12 -0400 Subject: [PATCH] Added some code to login and moved some code from TVDBSearch to checks --- .gitignore | 1 + TVDBSearch.py | 15 --------------- checks.py | 11 +++++++++-- login.py | 53 ++++++++++++++++++++++++++++++--------------------- 4 files changed, 41 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 694093f..6f812cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.jpg login.json api info.txt +__pycache__ diff --git a/TVDBSearch.py b/TVDBSearch.py index a1f9778..b2b3d7d 100644 --- a/TVDBSearch.py +++ b/TVDBSearch.py @@ -108,21 +108,6 @@ def tryMissing(missingNums, min, max, idNum, imageType): FAN_KEY_TYPE = "?keyType=fanart"# TODO check upper and lower bounds POS_KEY_TYPE = "?keyType=poster" BAN_KEY_TYPE = "?keyType=series" - -url = "https://api.thetvdb.com/login"#TODO change this to LOGIN_URL - -data = { - "apikey": API_KEY, - "userkey": USER_KEY, - "username": USER_NAME -} - -headers = {# TODO uppercase this - "Content-Type": "application/json", - "Accept": "application/json" -} - - token = getToken(url, data, headers)# TODO uppercase these diff --git a/checks.py b/checks.py index e373bee..21e0168 100644 --- a/checks.py +++ b/checks.py @@ -1,13 +1,20 @@ import json +import requests + +def getToken(data):#TODO add a timeout and try catch to all requests + url = "https://api.thetvdb.com/login" + headers = { + "Content-Type": "application/json", + "Accept": "application/json" + } -def getToken(url, data, headers):#TODO add a timeout and try catch to all requests response = requests.post(url, data=json.dumps(data), headers=headers) if (checkStatus(response, True)): parsed_token = json.loads(response.content) token = parsed_token["token"] return token else: - quit() + return "" def checkStatus(response, v): if (response.status_code != 200): diff --git a/login.py b/login.py index b7a8b66..d3722a1 100644 --- a/login.py +++ b/login.py @@ -1,6 +1,7 @@ import json import os.path -import checks +from checks import * +import time def login(): if (os.path.exists("login.json") == False): @@ -16,28 +17,36 @@ def login(): "TIMESTAMP": "" } + tmp_api_key = login["API_KEY"] + tmp_user_key = login["USER_KEY"] + tmp_user_name = login["USER_NAME"] + + while tmp_api_key is "": + tmp_api_key = input("Please enter your api key: ") + while tmp_user_key is "": + tmp_user_key = input("Please enter your user key: ") + while tmp_user_name is "": + tmp_user_name = input("Please enter your username: ") + + LOGIN_DATA = { + "apikey": tmp_api_key, + "userkey": tmp_user_key, + "username": tmp_user_name + } + + tmp_token = getToken(LOGIN_DATA) + if tmp_token is "": + print("Authentication failed. Please try again.") + with open("login.json") as json_data: - # login = json.load(json_data) - - tmp_api_key = login["API_KEY"] - tmp_user_key = login["USER_KEY"] - tmp_user_name = login["USER_NAME"] - - while tmp_api_key is "": - tmp_api_key = input("Please enter your api key: ") - while tmp_user_key is "": - tmp_user_key = input("Please enter your user key: ") - while tmp_user_name is "": - tmp_user_name = input("Please enter your username: ") - - # TODO check token here - login["API_KEY"] = tmp_api_key - login["USER_KEY"] = tmp_user_key - login["USER_NAME"] = tmp_user_name - 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 + obj = open("login.json", "w") + obj.write(json.dumps(login)) + obj.close() +def getCredentials(): + print() # 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