Changing string concatenation to .format(). Other small fixes.

This commit is contained in:
ClaytonWWilson 2018-07-13 08:47:47 -04:00
parent 841d567c78
commit 07d3f1f960
5 changed files with 30 additions and 26 deletions

View File

@ -11,6 +11,8 @@ from checks import checkTimestamp
from checks import checkStatus from checks import checkStatus
from checks import getToken from checks import getToken
# TODO add counters for number of images downloaded and deleted
def wait(): def wait():
input("Press enter to continue.") input("Press enter to continue.")
@ -71,28 +73,30 @@ def clearLogin():
def clearFolders(): # TODO implement this def clearFolders(): # TODO implement this
folders = ["banner", "fanart", "poster"] folders = ["banner", "fanart", "poster"]
del_count = 0
for folder in folders: for folder in folders:
if os.path.exists(folder): if os.path.exists(folder):
imageList = os.listdir(folder) imageList = os.listdir(folder)
if len(imageList) != 0: if len(imageList) != 0:
print("Clearing " + folder + "/") print("Clearing " + folder + "/")
for x in imageList: # TODO check if folder is empty for x in imageList: # TODO check if folder is empty
print("Deleting " + x) print("Deleting {}/{}".format(folder, x))
delPath = os.path.join(folder + "\\" + x) delPath = os.path.join(folder + "\\" + x)
os.remove(delPath) os.remove(delPath)
del_count += 1
print() print()
else: else:
print("'" + folder + "'" + " is already empty") print("'{}' is already empty".format(folder))
else: else:
createFolder(folder) createFolder(folder)
print("") print("Deleted {} images.\n".format(del_count))
def createFolder(folder): # TODO remove this def createFolder(folder): # TODO remove this
os.makedirs(folder) os.makedirs(folder)
def searchImages(idNum, keyType, authHeaders): # This is getting a list of file info for images in json format def searchImages(idNum, keyType, authHeaders): # This is getting a list of file info for images in json format
queryUrl = "https://api.thetvdb.com/series/" + str(idNum) + "/images/query" + keyType # TODO change this to string formatting queryUrl = "https://api.thetvdb.com/series/{}/images/query{}".format(str(idNum), keyType)
response = requests.get(queryUrl, headers=authHeaders) response = requests.get(queryUrl, headers=authHeaders)
if (checkStatus(response, True)): if (checkStatus(response, True)):
return response return response
@ -120,7 +124,7 @@ def searchRemainder(imageType, saveNameList, idNum):#Finds any images missing fr
filenum = hyphenSuffix.replace(".jpg", "") filenum = hyphenSuffix.replace(".jpg", "")
numbers.append(int(filenum)) numbers.append(int(filenum))
else: else:
print("I couldn't find a hyphen in: %s" % name) # Error checking print("I couldn't find a hyphen in: {}".format(name)) # Error checking
numbers.sort numbers.sort
missingList = findMissing(numbers) missingList = findMissing(numbers)
minNum = min(numbers) minNum = min(numbers)
@ -139,16 +143,16 @@ def tryMissing(missingNums, minNum, maxNum, idNum, imageType):
startDirectory = "posters/" startDirectory = "posters/"
for num in missingNums: for num in missingNums:
fileName = startDirectory + str(idNum) + "-" + str(num) + ".jpg" fileName = "{}{}-{}.jpg".format(startDirectory, str(idNum), str(num))
# fileName = "%s%s-%d.jpg" % startDirectory, idNum, missingNums[num] # fileName = "%s%s-%d.jpg" % startDirectory, idNum, missingNums[num]
# try: # try:
print("Trying... " + fileName) print("Trying... {}".format(fileName))
dlUrl = "https://www.thetvdb.com/banners/" + fileName dlUrl = "https://www.thetvdb.com/banners/{}".format(fileName)
# print("url is: " + dlUrl) # print("url is: " + dlUrl)
response = requests.get(dlUrl) response = requests.get(dlUrl)
# print(response.status_code) # print(response.status_code)
if (checkStatus(response, True) == True): # TODO there is an error occurring here when checking fanart if (checkStatus(response, True) == True): # TODO there is an error occurring here when checking fanart
path = os.path.join(imageType + "\\" + str(idNum) + "-" + str(num) + ".jpg") # TODO string formatting path = os.path.join("{}\\{}-{}.jpg".format(imageType, str(idNum), str(num)))
obj = open(path, "wb") obj = open(path, "wb")
obj.write(response.content) obj.write(response.content)
obj.close() obj.close()
@ -176,8 +180,8 @@ def download(imageType, parsed_respObj):
saveName = fileName[slashIndex + 1:] # For example 'https://thetvdb.com/banners/fanart/original/32451-3.jpg' --> '32451.jpg' saveName = fileName[slashIndex + 1:] # For example 'https://thetvdb.com/banners/fanart/original/32451-3.jpg' --> '32451.jpg'
saveNameList.append(saveName) saveNameList.append(saveName)
print("Downloading... " + fileName) print("Downloading... {}".format(fileName))
dlUrl = "https://www.thetvdb.com/banners/" + fileName dlUrl = "https://www.thetvdb.com/banners/{}".format(fileName)
response = requests.get(dlUrl) # TODO getting errors when checking 'new game'. Check to see if those images actually exist response = requests.get(dlUrl) # TODO getting errors when checking 'new game'. Check to see if those images actually exist
if (checkStatus(response, True)): if (checkStatus(response, True)):
@ -210,7 +214,7 @@ def update():
"https://github.com/ClaytonWWilson/Image-fetcher-for-theTVDB.com") "https://github.com/ClaytonWWilson/Image-fetcher-for-theTVDB.com")
return return
if code == 0: if code == 0:
print("\nThe program has been updated.\n") print("\nUpdating complete.\n")
else: else:
print("\nThere was an error while updating. This may be caused by edits " print("\nThere was an error while updating. This may be caused by edits "
"you have made to the code.") "you have made to the code.")

View File

@ -25,7 +25,7 @@ def checkStatus(response, v):
if (response.status_code != 200): if (response.status_code != 200):
if (v == True): if (v == True):
print("\nAn error occurred.") print("\nAn error occurred.")
print("HTTP Code: " + str(response.status_code)) print("HTTP Code: {}".format(str(response.status_code)))
# error = json.loads(response.content) # TODO move this somewhere else # error = json.loads(response.content) # TODO move this somewhere else
# print("Response : " + error["Error"]) # print("Response : " + error["Error"])
return False return False

View File

@ -23,9 +23,9 @@ while True:
print("1. Search theTVDB.com") print("1. Search theTVDB.com")
print("2. Clear download folders") print("2. Clear download folders")
print("3. Login/Change login") print("3. Login/Change login")
print("4. Refresh API Token") # print("4. Refresh API Token")
print("5. Install Requirements") print("4. Install Requirements")
print("6. Check for updates\n") print("5. Check for updates\n")
print("0. Exit\n") print("0. Exit\n")
choice = user_choice() choice = user_choice()
@ -42,14 +42,14 @@ while True:
clearLogin() clearLogin()
login() login()
wait() wait()
elif choice == "4": # TODO need to perform this option automatically # elif choice == "4": # TODO need to perform this option automatically
clear_screen() # clear_screen()
refreshToken() # refreshToken()
wait() # wait()
elif choice == "5": elif choice == "4":
print("install requirements not implemented yet") print("install requirements not implemented yet")
wait() wait()
elif choice == "6": elif choice == "5":
update() update()
wait() wait()
elif choice == "0": elif choice == "0":

View File

@ -57,7 +57,7 @@ def login():
obj.close() obj.close()
print("\nLogin successful!\n") 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 file that is damaged/modified
login = json.load(json_data) login = json.load(json_data)
json_data.close() json_data.close()
saveTime = dateutil.parser.parse(login["TIMESTAMP"]) saveTime = dateutil.parser.parse(login["TIMESTAMP"])

View File

@ -57,7 +57,7 @@ def search():
sKeyword = sKeyword.replace("/", "%2F") sKeyword = sKeyword.replace("/", "%2F")
sKeyword = sKeyword.replace("%7E", "~") sKeyword = sKeyword.replace("%7E", "~")
searchUrl = "https://api.thetvdb.com/search/series?name=" + sKeyword searchUrl = "https://api.thetvdb.com/search/series?name={}".format(sKeyword)
response = requests.get(searchUrl, headers=authHeaders) response = requests.get(searchUrl, headers=authHeaders)
if (checkStatus(response, True) == False): if (checkStatus(response, True) == False):
@ -72,11 +72,11 @@ def search():
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"]:
print("\n%s)\nSeries Name: " % str(count), str(result["seriesName"])) print("\n{})\nSeries Name: {}".format(str(count), str(result["seriesName"])))
print() print()
desc = result["overview"] desc = result["overview"]
desc = str(desc).replace("\r\n\r\n", " ") # Removing format characters desc = str(desc).replace("\r\n\r\n", " ") # Removing format characters
print("Description: \n%s" % desc) print("Description: \n{}".format(desc))
print() print()
count = count + 1 count = count + 1
print() print()