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
fanart
poster
.vscode

View File

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

View File

@ -7,10 +7,11 @@ import re
import requests
import urllib.parse
from utils import APIConnector
from utils import clearFolders
from utils import clearScreen
from utils import create_file_name
from authentication import checkTimestamp
from authentication import check_timestamp
from authentication import checkStatus
from authentication import refreshToken
@ -23,27 +24,29 @@ class Series:
def search():
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)
json_data.close()
# json_data.close()
if login["TIMESTAMP"] == "":
print("There was an error checking your login. Try logging in again with 'Login/Change login'.")
return None
else:
save_time = dateutil.parser.parse(login["TIMESTAMP"])
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()
except Exception as ex:
# print(ex)
print("There was an error checking your login. Try logging in again with 'Login/Change login'.")
return None
authHeaders = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + login["TOKEN"]
}
api_con = APIConnector()
# authHeaders = {
# "Content-Type": "application/json",
# "Accept": "application/json",
# "Authorization": "Bearer " + login["TOKEN"]
# }
keyword = input("Enter series name to search: ") # Getting the search name and fixing
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("%7E", "~")
search_url = "https://api.thetvdb.com/search/series?name={}".format(s_keyword)
response = requests.get(search_url, headers=authHeaders)
# search_url = "https://api.thetvdb.com/search/series?name={}".format(s_keyword)
# response = requests.get(search_url, headers=authHeaders)
if (checkStatus(response, True) == False):
return None
res = api_con.send_http_req(
"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
print()
@ -92,4 +98,4 @@ def search():
print()
series = Series(create_file_name(search_results["data"][title]["seriesName"]), search_results["data"][title]["id"], "https://www.thetvdb.com/series/" + search_results["data"][title]["slug"])
return series
return series