diff --git a/actions.py b/actions.py index 506ceba..dcf02fa 100644 --- a/actions.py +++ b/actions.py @@ -8,6 +8,7 @@ import requests import dateutil from checks import checkTimestamp +from checks import checkStatus from checks import getToken def wait(): @@ -104,6 +105,56 @@ def downloadImages(imageType, respObj, idNum): # TODO some images arent grabbed searchRemainder(imageType, saveNameList, idNum) +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 + if (imageType is "banner"): # TODO check upper and lower bounds + print("this is a banner") + #TODO deal with banners + else: + for name in saveNameList: + if (name.rfind("-") != -1): + hyphenIndex = name.rfind("-") + hyphenSuffix = name[hyphenIndex + 1:] + value = hyphenSuffix.replace(".jpg", "") + numbers.append(int(value)) + else: + print("I couldn't find a hyphen in: %s" % name)#Error checking + numbers.sort + missingList = findMissing(numbers) + minNum = min(numbers) + maxNum = max(numbers) + tryMissing(missingList, minNum, maxNum, idNum, imageType) + +def findMissing(numbers): # TODO test this + start, end = numbers[0], numbers[-1] + return sorted(set(range(start, end + 1)).difference(numbers)) + +def tryMissing(missingNums, min, max, idNum, imageType): + if (imageType is "fanart"): + startDirectory = "fanart/original/" + elif (imageType is "poster"): + startDirectory = "posters/" + + for num in missingNums: + fileName = startDirectory + str(idNum) + "-" + str(num) + ".jpg" + # fileName = "%s%s-%d.jpg" % startDirectory, idNum, missingNums[num] + print("This is missing: " + fileName) + try: + print("Trying... " + fileName) + dlUrl = "https://www.thetvdb.com/banners/" + fileName + response = requests.get(dlUrl) + if (checkStatus(response, False) == True): + path = os.path.join(imageType + "\\" + str(idNum) + str(num) + ".jpg") + obj = open(path, "wb") + obj.write(response.content) + obj.close() + + except Exception as e: + print("repsonse code: " + str(response.status_code)) + print("Check: " + dlUrl) + print(fileName + " doesn't exist") + def download(imageType, parsed_respObj): counter = 0 saveNameList = [] diff --git a/launcher.py b/launcher.py index 838d5f9..441d2e4 100644 --- a/launcher.py +++ b/launcher.py @@ -35,7 +35,7 @@ while True: clear_screen() clearFolders() wait() - elif choice == "3": + elif choice == "3": # TODO if already logged in, ask 'are you sure?' clear_screen() clearLogin() login() diff --git a/login.py b/login.py index 0a36d3f..05f65c6 100644 --- a/login.py +++ b/login.py @@ -55,7 +55,7 @@ def login(): obj = open("login.json", "w") obj.write(json.dumps(login)) obj.close() - print("\nLogin successful!") + print("\nLogin successful!\n") else:# if login.json already exists with open("login.json") as json_data:# TODO add a check for a login that is damaged/modified login = json.load(json_data) diff --git a/search.py b/search.py index 8bb1534..176b1e7 100644 --- a/search.py +++ b/search.py @@ -1,63 +1,18 @@ import os.path import json +import datetime import requests import urllib.parse +import dateutil -from actions import wait +from actions import clearFolders +from actions import downloadImages +from actions import searchImages +from checks import checkTimestamp +from checks import checkStatus -# TODO Move this to actions -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 - if (imageType is "banner"): # TODO check upper and lower bounds - print("this is a banner") - #TODO deal with banners - else: - for name in saveNameList: - if (name.rfind("-") != -1): - hyphenIndex = name.rfind("-") - hyphenSuffix = name[hyphenIndex + 1:] - value = hyphenSuffix.replace(".jpg", "") - numbers.append(int(value)) - else: - print("I couldn't find a hyphen in: %s" % name)#Error checking - numbers.sort - missingList = findMissing(numbers) - minNum = min(numbers) - maxNum = max(numbers) - tryMissing(missingList, minNum, maxNum, idNum, imageType) - -def findMissing(numbers): # TODO test this - start, end = numbers[0], numbers[-1] - return sorted(set(range(start, end + 1)).difference(numbers)) - -def tryMissing(missingNums, min, max, idNum, imageType): - if (imageType is "fanart"): - startDirectory = "fanart/original/" - elif (imageType is "poster"): - startDirectory = "posters/" - - for num in missingNums: - fileName = startDirectory + str(idNum) + "-" + str(num) + ".jpg" - # fileName = "%s%s-%d.jpg" % startDirectory, idNum, missingNums[num] - print("This is missing: " + fileName) - try: - print("Trying... " + fileName) - dlUrl = "https://www.thetvdb.com/banners/" + fileName - response = requests.get(dlUrl) - if (checkStatus(response, False) == True): - path = os.path.join(imageType + "\\" + str(idNum) + str(num) + ".jpg") - obj = open(path, "wb") - obj.write(response.content) - obj.close() - - except Exception as e: - print("repsonse code: " + str(response.status_code)) - print("Check: " + dlUrl) - print(fileName + " doesn't exist") - def search(): try: with open("login.json") as json_data: # TODO add a check for a login that is damaged/modified @@ -72,7 +27,8 @@ def search(): if checkTimestamp(saveTime, curTime) == False: print("Your token has expired. Get a new one by choosing Refresh Token.") return None - except: + except Exception as ex: + print(ex) print("There was an error checking your login. Try logging in again with 'Login/Change login'.") return None @@ -109,6 +65,7 @@ def search(): title = -1 print() while title < 0 or title > len(searchResults["data"]) - 1: # Looping until the user chooses + print("==================================") print("Results:") # a series from the printed list count = 1 # or they input '0' to cancel for result in searchResults["data"]: