Refactored code for search and changed imports on login and launcher

This commit is contained in:
Clayton Wilson 2017-11-06 13:30:45 -05:00
parent da958cce55
commit ec12a6da1e
3 changed files with 71 additions and 69 deletions

View File

@ -1,6 +1,10 @@
import os # TODO: change the order of all import statements to 1. standard library
from login import * # TODO: 2. related 3rd party
from actions import * # TODO: 3. local application. with blank lines between and remove wilcard symbols
from login import login # TODO: 2. related 3rd party
from actions import wait # TODO: 3. local application. with blank lines between and remove wilcard symbols
from actions import clear_screen
from actions import clearLogin
from actions import refreshToken
from actions import update
from search import search
def user_choice():

View File

@ -64,5 +64,4 @@ def login():
print("token is good")
else:
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

131
search.py
View File

@ -26,9 +26,6 @@ def searchRemainder(imageType, saveNameList, idNum):#Finds any images missing fr
minNum = min(numbers)
maxNum = max(numbers)
tryMissing(missingList, minNum, maxNum, idNum, imageType)
# print("min: %d" % int(min(numbers)))
# print("max: %d" % int(max(numbers)))
# quit()
def findMissing(numbers): # TODO test this
start, end = numbers[0], numbers[-1]
@ -73,69 +70,71 @@ def search():
if checkTimestamp(saveTime, curTime) == False:
print("Your token has expired. Get a new one by choosing Refresh Token.")
return None
else: # All login checks pass and search starts # TODO move everything below out of the try except
FAN_KEY_TYPE = "?keyType=fanart" # These are used in the search strings
POS_KEY_TYPE = "?keyType=poster"
BAN_KEY_TYPE = "?keyType=series"
authHeaders = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + login["TOKEN"]
}
keyword = input("Enter series to search: ") # Getting the search name and fixing
sKeyword = urllib.parse.quote(keyword) # the url parse mistakes
sKeyword = sKeyword.replace("%21", "!") # TODO find a better way of doing this
sKeyword = sKeyword.replace("%2A", "*")
sKeyword = sKeyword.replace("%28", "(")
sKeyword = sKeyword.replace("%29", ")")
sKeyword = sKeyword.replace("%27", "'")
sKeyword = sKeyword.replace("/", "%2F")
sKeyword = sKeyword.replace("%7E", "~")
searchUrl = "https://api.thetvdb.com/search/series?name=" + sKeyword
response = requests.get(searchUrl, headers=authHeaders)
if (checkStatus(response, True) == False):
return None
searchResults = json.loads(response.content)
title = -1
print()
while title < 0 or title > len(searchResults["data"]) - 1: # Looping until the user chooses
print("Results:") # a series from the printed list
count = 1 # or they input '0' to cancel
for result in searchResults["data"]:
print("\n%s)\nSeries Name: " % str(count), str(result["seriesName"]))
print()
desc = result["overview"]
desc = str(desc).replace("\r\n\r\n", " ") # Removing format characters
print("Description: \n%s" % desc)
print()
count = count + 1
print()
title = int(input("Choose one by number or '0' to exit: ")) - 1 # Subtracting 1 so that the
print() # index can start from 0
if title < -1 or title > len(searchResults["data"]) - 1:
print("Error: Choose the number of an item listed. Or '0' to exit.")
if (title == -1): # If the user inputs 0
return None
print()
idNum = searchResults["data"][title]["id"] # Setting up the request urls
fanart = searchImages(idNum, FAN_KEY_TYPE, authHeaders) # for banners, fanart, and posters
poster = searchImages(idNum, POS_KEY_TYPE, authHeaders)
banner = searchImages(idNum, BAN_KEY_TYPE, authHeaders)
downloadImages("fanart", fanart, idNum) # TODO find a better way to pass these variables. Constructor?
downloadImages("poster", poster, idNum)
downloadImages("banner", banner, idNum)
return None
except:
print("There was an error checking your login. Try logging in again with 'Login/Change login'.")
return None
# All login checks pass and search starts
FAN_KEY_TYPE = "?keyType=fanart" # These are used in the search strings
POS_KEY_TYPE = "?keyType=poster"
BAN_KEY_TYPE = "?keyType=series"
authHeaders = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + login["TOKEN"]
}
keyword = input("Enter series to search: ") # Getting the search name and fixing
sKeyword = urllib.parse.quote(keyword) # the url parse mistakes
sKeyword = sKeyword.replace("%21", "!") # TODO find a better way of doing this
sKeyword = sKeyword.replace("%2A", "*")
sKeyword = sKeyword.replace("%28", "(")
sKeyword = sKeyword.replace("%29", ")")
sKeyword = sKeyword.replace("%27", "'")
sKeyword = sKeyword.replace("/", "%2F")
sKeyword = sKeyword.replace("%7E", "~")
searchUrl = "https://api.thetvdb.com/search/series?name=" + sKeyword
response = requests.get(searchUrl, headers=authHeaders)
if (checkStatus(response, True) == False):
return None
searchResults = json.loads(response.content)
title = -1
print()
while title < 0 or title > len(searchResults["data"]) - 1: # Looping until the user chooses
print("Results:") # a series from the printed list
count = 1 # or they input '0' to cancel
for result in searchResults["data"]:
print("\n%s)\nSeries Name: " % str(count), str(result["seriesName"]))
print()
desc = result["overview"]
desc = str(desc).replace("\r\n\r\n", " ") # Removing format characters
print("Description: \n%s" % desc)
print()
count = count + 1
print()
title = int(input("Choose one by number or '0' to exit: ")) - 1 # Subtracting 1 so that the
print() # index can start from 0
if title < -1 or title > len(searchResults["data"]) - 1:
print("Error: Choose the number of an item listed. Or '0' to exit.")
if (title == -1): # If the user inputs 0
return None
print()
idNum = searchResults["data"][title]["id"] # Setting up the request urls
fanart = searchImages(idNum, FAN_KEY_TYPE, authHeaders) # for banners, fanart, and posters
poster = searchImages(idNum, POS_KEY_TYPE, authHeaders)
banner = searchImages(idNum, BAN_KEY_TYPE, authHeaders)
downloadImages("fanart", fanart, idNum) # TODO find a better way to pass these variables. Constructor?
downloadImages("poster", poster, idNum)
downloadImages("banner", banner, idNum)
print("All downloads finished!")
return None