From 96870d4198384d9d4283aeb0f5805477e90e4c53 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Wed, 1 Nov 2017 12:51:23 -0400 Subject: [PATCH] Moved some methods to action.py and created refreshToken() --- TVDBSearch.py | 45 --------------------------------------------- actions.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ login.py | 3 ++- 3 files changed, 49 insertions(+), 46 deletions(-) create mode 100644 actions.py diff --git a/TVDBSearch.py b/TVDBSearch.py index 428765e..bff4bea 100644 --- a/TVDBSearch.py +++ b/TVDBSearch.py @@ -5,51 +5,6 @@ import os.path import shutil # import login -def getImages(idNum, keyType, authHeaders): - imageUrl = "https://api.thetvdb.com/series/" + str(idNum) + "/images/query" + keyType - response = requests.get(imageUrl, headers=authHeaders) - if (checkStatus(response, True)): - return response - else: - quit() - -def downloadImages(imageType, respObj, idNum):# TODO some images arent grabbed through the api. save the image number and make a try catch to get any missing images - if (os.path.exists(imageType)):#TODO add try catch here - print("\nClearing /%s/" % imageType) - shutil.rmtree(imageType) - os.makedirs(imageType) - - parsed_respObj = json.loads(respObj.content) - - saveNameList = download(imageType, parsed_respObj) - - searchRemainder(imageType, saveNameList, idNum) - -def download(imageType, parsed_respObj): - counter = 0 - saveNameList = [] - for imageObj in parsed_respObj["data"]: - fileName = parsed_respObj["data"][counter]["fileName"]#TODO the download method should start here, move everything else up to downloadImages - counter = counter + 1 - - slashIndex = fileName.rfind("/") - saveName = fileName[slashIndex + 1:] - - saveNameList.append(saveName) - - print("Downloading... " + fileName) - dlUrl = "https://www.thetvdb.com/banners/" + fileName - response = requests.get(dlUrl)# TODO getting errors when checking 'new game'. Check to see if those images actually exist - - if (checkStatus(response, True)): - path = os.path.join(imageType + "\\", saveName) - obj = open(path, "wb") - obj.write(response.content) - obj.close() - else: - quit() - return saveNameList - def searchRemainder(imageType, saveNameList, idNum):#Finds any images missing from the api call in getImages numbers = [] print("Checking for missing images...")#TODO implement this method diff --git a/actions.py b/actions.py new file mode 100644 index 0000000..9c52e71 --- /dev/null +++ b/actions.py @@ -0,0 +1,47 @@ +def refreshToken(): + print("Not implemented yet") + +def getImages(idNum, keyType, authHeaders): + imageUrl = "https://api.thetvdb.com/series/" + str(idNum) + "/images/query" + keyType + response = requests.get(imageUrl, headers=authHeaders) + if (checkStatus(response, True)): + return response + else: + quit() + +def downloadImages(imageType, respObj, idNum):# TODO some images arent grabbed through the api. save the image number and make a try catch to get any missing images + if (os.path.exists(imageType)):#TODO add try catch here + print("\nClearing /%s/" % imageType) + shutil.rmtree(imageType) + os.makedirs(imageType) + + parsed_respObj = json.loads(respObj.content) + + saveNameList = download(imageType, parsed_respObj) + + searchRemainder(imageType, saveNameList, idNum) + +def download(imageType, parsed_respObj): + counter = 0 + saveNameList = [] + for imageObj in parsed_respObj["data"]: + fileName = parsed_respObj["data"][counter]["fileName"]#TODO the download method should start here, move everything else up to downloadImages + counter = counter + 1 + + slashIndex = fileName.rfind("/") + saveName = fileName[slashIndex + 1:] + + saveNameList.append(saveName) + + print("Downloading... " + fileName) + dlUrl = "https://www.thetvdb.com/banners/" + fileName + response = requests.get(dlUrl)# TODO getting errors when checking 'new game'. Check to see if those images actually exist + + if (checkStatus(response, True)): + path = os.path.join(imageType + "\\", saveName) + obj = open(path, "wb") + obj.write(response.content) + obj.close() + else: + quit() + return saveNameList diff --git a/login.py b/login.py index 6471b13..d16c5e3 100644 --- a/login.py +++ b/login.py @@ -1,6 +1,7 @@ import json import os.path from checks import * +from actions import * import datetime import dateutil.parser @@ -60,6 +61,6 @@ def login(): if checkTimestamp(saveTime, curTime):# token does not need refreshed print("token is good") else: - print("token needs refreshed") + refreshToken() # 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