Moved methods from search.py to actions.py

This commit is contained in:
Clayton Wilson 2017-11-07 13:57:54 -05:00
parent 9aab258350
commit cc3c89c5d6
4 changed files with 63 additions and 55 deletions

View File

@ -8,6 +8,7 @@ import requests
import dateutil import dateutil
from checks import checkTimestamp from checks import checkTimestamp
from checks import checkStatus
from checks import getToken from checks import getToken
def wait(): def wait():
@ -104,6 +105,56 @@ def downloadImages(imageType, respObj, idNum): # TODO some images arent grabbed
searchRemainder(imageType, saveNameList, idNum) 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): def download(imageType, parsed_respObj):
counter = 0 counter = 0
saveNameList = [] saveNameList = []

View File

@ -35,7 +35,7 @@ while True:
clear_screen() clear_screen()
clearFolders() clearFolders()
wait() wait()
elif choice == "3": elif choice == "3": # TODO if already logged in, ask 'are you sure?'
clear_screen() clear_screen()
clearLogin() clearLogin()
login() login()

View File

@ -55,7 +55,7 @@ def login():
obj = open("login.json", "w") obj = open("login.json", "w")
obj.write(json.dumps(login)) obj.write(json.dumps(login))
obj.close() obj.close()
print("\nLogin successful!") print("\nLogin successful!\n")
else:# if login.json already exists else:# if login.json already exists
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:# TODO add a check for a login that is damaged/modified
login = json.load(json_data) login = json.load(json_data)

View File

@ -1,63 +1,18 @@
import os.path import os.path
import json import json
import datetime
import requests import requests
import urllib.parse 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(): 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: # TODO add a check for a login that is damaged/modified
@ -72,7 +27,8 @@ def search():
if checkTimestamp(saveTime, curTime) == False: if checkTimestamp(saveTime, curTime) == False:
print("Your token has expired. Get a new one by choosing Refresh Token.") print("Your token has expired. Get a new one by choosing Refresh Token.")
return None 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'.") print("There was an error checking your login. Try logging in again with 'Login/Change login'.")
return None return None
@ -109,6 +65,7 @@ def search():
title = -1 title = -1
print() print()
while title < 0 or title > len(searchResults["data"]) - 1: # Looping until the user chooses while title < 0 or title > len(searchResults["data"]) - 1: # Looping until the user chooses
print("==================================")
print("Results:") # a series from the printed list print("Results:") # a series from the printed list
count = 1 # or they input '0' to cancel count = 1 # or they input '0' to cancel
for result in searchResults["data"]: for result in searchResults["data"]: