Refactoring - Updating some old functions.

This commit is contained in:
ClaytonWWilson 2019-05-24 15:40:11 -04:00
parent 2edfc5be14
commit c876291100
3 changed files with 29 additions and 21 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@ downloads
banner banner
fanart fanart
poster poster
.vscode

View File

@ -65,15 +65,15 @@ def login():
# TODO at startup, check token for validity and remove it if it is expired # TODO at startup, check token for validity and remove it if it is expired
def getToken(data):#TODO add a timeout and try catch to all requests def getToken(login_data): #TODO add a timeout and try catch to all requests
url = "https://api.thetvdb.com/login" url = "https://api.thetvdb.com/login"
headers = { headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"Accept": "application/json" "Accept": "application/json"
} }
try: try:
response = requests.post(url, data=json.dumps(data), headers=headers) response = requests.post(url, data=json.dumps(login_data), headers=headers)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError:
print("An error occurred. Please check your internet and try again.") print("An error occurred. Please check your internet and try again.")
quit() quit()
@ -99,7 +99,7 @@ def refreshToken():
"username": login["USER_NAME"] "username": login["USER_NAME"]
} }
if checkTimestamp(save_time, cur_time): if check_timestamp(save_time, cur_time):
while True: while True:
print("Your current token is still valid. Are you sure you want to grab a different one?") print("Your current token is still valid. Are you sure you want to grab a different one?")
choice = input("(y/n) ") choice = input("(y/n) ")
@ -137,8 +137,9 @@ def checkStatus(response, v):
else: else:
return True return True
# Returns true if the token is still valid # Returns true if the token is still valid.
def checkTimestamp(save_time, cur_time): # Tokens expire after 24 hours
def check_timestamp(save_time, cur_time):
if cur_time - save_time < datetime.timedelta(0, 86100, 0): if cur_time - save_time < datetime.timedelta(0, 86100, 0):
return True return True
else: else:

View File

@ -7,10 +7,11 @@ import re
import requests import requests
import urllib.parse import urllib.parse
from utils import APIConnector
from utils import clearFolders from utils import clearFolders
from utils import clearScreen from utils import clearScreen
from utils import create_file_name from utils import create_file_name
from authentication import checkTimestamp from authentication import check_timestamp
from authentication import checkStatus from authentication import checkStatus
from authentication import refreshToken from authentication import refreshToken
@ -23,27 +24,29 @@ class Series:
def search(): def search():
try: try:
with open("login.json") as json_data: # TODO add a check for a login that is damaged/modified with open("login.json") as json_data:
login = json.load(json_data) login = json.load(json_data)
json_data.close() # json_data.close()
if login["TIMESTAMP"] == "": if login["TIMESTAMP"] == "":
print("There was an error checking your login. Try logging in again with 'Login/Change login'.") print("There was an error checking your login. Try logging in again with 'Login/Change login'.")
return None return None
else: else:
save_time = dateutil.parser.parse(login["TIMESTAMP"]) save_time = dateutil.parser.parse(login["TIMESTAMP"])
cur_time = datetime.datetime.now().replace(tzinfo=None) # TODO use UTC time? cur_time = datetime.datetime.now().replace(tzinfo=None) # TODO use UTC time?
if checkTimestamp(save_time, cur_time) == False: if check_timestamp(save_time, cur_time) == False:
refreshToken() refreshToken()
except Exception as ex: except Exception as ex:
# print(ex) # print(ex)
print("There was an error checking your login. Try logging in again with 'Login/Change login'.") print("There was an error checking your login. Try logging in again with 'Login/Change login'.")
return None return None
authHeaders = { api_con = APIConnector()
"Content-Type": "application/json",
"Accept": "application/json", # authHeaders = {
"Authorization": "Bearer " + login["TOKEN"] # "Content-Type": "application/json",
} # "Accept": "application/json",
# "Authorization": "Bearer " + login["TOKEN"]
# }
keyword = input("Enter series name to search: ") # Getting the search name and fixing keyword = input("Enter series name to search: ") # Getting the search name and fixing
s_keyword = urllib.parse.quote(keyword) # the url parse mistakes s_keyword = urllib.parse.quote(keyword) # the url parse mistakes
@ -56,13 +59,16 @@ def search():
s_keyword = s_keyword.replace("/", "%2F") s_keyword = s_keyword.replace("/", "%2F")
s_keyword = s_keyword.replace("%7E", "~") s_keyword = s_keyword.replace("%7E", "~")
search_url = "https://api.thetvdb.com/search/series?name={}".format(s_keyword) # search_url = "https://api.thetvdb.com/search/series?name={}".format(s_keyword)
response = requests.get(search_url, headers=authHeaders) # response = requests.get(search_url, headers=authHeaders)
if (checkStatus(response, True) == False): res = api_con.send_http_req(
return None "https://api.thetvdb.com/search/series?name={}".format(s_keyword))
search_results = json.loads(response.content) # if (checkStatus(response, True) == False):
# return None
search_results = json.loads(res.content)
title = -1 title = -1
print() print()