From 2fab5ae2bd453aef602ddffc4adf86bc6a4c0b0c Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Thu, 2 Nov 2017 15:38:08 -0400 Subject: [PATCH 1/6] Completed clearFolders() --- actions.py | 30 +++++++++++++++++------------- launcher.py | 2 ++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/actions.py b/actions.py index d4a71a6..ad92066 100644 --- a/actions.py +++ b/actions.py @@ -13,20 +13,24 @@ def clearLogin(): pass def clearFolders():# TODO implement this - if os.path.exists("banner"): - print("cleared") - else: - print("empty") + folders = ["banner", "fanart", "poster"] + for folder in folders: + if os.path.exists(folder): + imageList = os.listdir(folder) + if len(imageList) != 0: + for x in imageList: # TODO check if folder is empty + print("Deleting " + x) + delPath = os.path.join(folder + "\\" + x) + os.remove(delPath) + print(folder + " cleared\n") + else: + print(folder + " is already empty") + else: + createFolder(folder) + print("") - if os.path.exists("fanart"): - print("cleared") - else: - print("empty") - - if os.path.exists("poster"): - print("cleared") - else: - print("empty") +def createFolder(folder): + os.makedirs(folder) def getImages(idNum, keyType, authHeaders): diff --git a/launcher.py b/launcher.py index c00f8ce..a76a671 100644 --- a/launcher.py +++ b/launcher.py @@ -15,6 +15,7 @@ def user_choice(): def wait(): input("Press enter to continue.") + while True: clear_screen() print("=============================\n" @@ -35,6 +36,7 @@ while True: print("Search") break elif choice == "2": + clear_screen() clearFolders() wait() elif choice == "3": From 17fcc6cc1839a92534b8dfcdc81d9928c14b1535 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Thu, 2 Nov 2017 15:58:40 -0400 Subject: [PATCH 2/6] Added code to refreshToken() --- actions.py | 22 ++++++++++++++++++++++ checks.py | 4 ++-- launcher.py | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/actions.py b/actions.py index ad92066..662a3a9 100644 --- a/actions.py +++ b/actions.py @@ -1,10 +1,32 @@ import requests import shutil import json +import datetime +import dateutil import os +from checks import checkTimestamp def refreshToken(): print("Not implemented yet") + if os.path.exists("login.json"): + try: + with open("login.json") as json_data: + login = json.load(json_data) + saveTime = dateutil.parser.parse(login["TIMESTAMP"]) + curTime = datetime.datetime.now().replace(tzinfo=None)# TODO use UTC time? + json_data.close() + if checkTimestamp(saveTime, curTime): + While True: + print("The current token is still valid. Do you still want to grab a different one") + choice = input("(y/n) ") + if choice is "n": + break + elif choice is "y": + token = getToken() # TODO finish setting this up. Make it occur if the if statement above fails + except Exception as e: + print("You need to log in first. Select Login/Change login.\n") # TODO make a set of constants for error codes + else: + print("You need to log in first. Select Login/Change login.\n") def clearLogin(): try: diff --git a/checks.py b/checks.py index fccf1ab..9c8e6db 100644 --- a/checks.py +++ b/checks.py @@ -32,8 +32,8 @@ def checkStatus(response, v): else: return True -def checkTimestamp(saveTime, curTime): - if curTime - saveTime < datetime.timedelta(0, 86100, 0):# if less than 23h 55m since last token grab +def checkTimestamp(saveTime, curTime): # Returns true if the token is still valid + if curTime - saveTime < datetime.timedelta(0, 86100, 0): return True else: return False diff --git a/launcher.py b/launcher.py index a76a671..01b8a62 100644 --- a/launcher.py +++ b/launcher.py @@ -24,7 +24,7 @@ while True: print("1. Search theTVDB.com") print("2. Clear download folders") - print("3. Change login") + print("3. Login/Change login") print("4. Refresh API Token") print("5. Install Requirements") print("6. Check for updates\n") From 9507bcc26c8edc07185ee753752509d57e4eb97d Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Fri, 3 Nov 2017 07:59:41 -0400 Subject: [PATCH 3/6] Implemented the refreshToken() method --- actions.py | 36 ++++++++++++++++++++++++++++++++---- launcher.py | 10 ++-------- login.py | 1 + 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/actions.py b/actions.py index 662a3a9..3f531c5 100644 --- a/actions.py +++ b/actions.py @@ -5,9 +5,16 @@ import datetime import dateutil import os from checks import checkTimestamp +from checks import getToken + +def clear_screen(): + IS_WINDOWS = os.name == "nt" + if IS_WINDOWS: + os.system("cls") + else: + os.system("clear") def refreshToken(): - print("Not implemented yet") if os.path.exists("login.json"): try: with open("login.json") as json_data: @@ -15,14 +22,35 @@ def refreshToken(): saveTime = dateutil.parser.parse(login["TIMESTAMP"]) curTime = datetime.datetime.now().replace(tzinfo=None)# TODO use UTC time? json_data.close() + + LOGIN_DATA = { + "apikey": login["API_KEY"], + "userkey": login["USER_KEY"], + "username": login["USER_NAME"] + } + if checkTimestamp(saveTime, curTime): - While True: - print("The current token is still valid. Do you still want to grab a different one") + while True: + print("Your current token is still valid. Are you sure you want to grab a different one?") choice = input("(y/n) ") if choice is "n": break elif choice is "y": - token = getToken() # TODO finish setting this up. Make it occur if the if statement above fails + login["TOKEN"] = getToken(LOGIN_DATA) # TODO find a better way to run this on both paths + login["TIMESTAMP"] = str(datetime.datetime.now().replace(tzinfo=None)) + obj = open("login.json", "w") + obj.write(json.dumps(login)) + obj.close() + print("\nNew token acquired!\n") + break + clear_screen() + else: + login["TOKEN"] = getToken(LOGIN_DATA) + login["TIMESTAMP"] = str(datetime.datetime.now().replace(tzinfo=None)) + obj = open("login.json", "w") + obj.write(json.dumps(login)) + obj.close() + print("New token acquired!\n") except Exception as e: print("You need to log in first. Select Login/Change login.\n") # TODO make a set of constants for error codes else: diff --git a/launcher.py b/launcher.py index 01b8a62..0b18ad3 100644 --- a/launcher.py +++ b/launcher.py @@ -2,13 +2,6 @@ import os # TODO: change the order of all import statements to from login import * # TODO: 2. related 3rd party from actions import * # TODO: 3. local application with blank lines between -def clear_screen(): - IS_WINDOWS = os.name == "nt" - if IS_WINDOWS: - os.system("cls") - else: - os.system("clear") - def user_choice(): return input("> ").lower().strip() @@ -45,7 +38,8 @@ while True: login() wait() elif choice == "4": - refreshToken()# TODO implement this + clear_screen() + refreshToken() wait() elif choice == "5": print("install requirements") diff --git a/login.py b/login.py index 5ffd798..349363c 100644 --- a/login.py +++ b/login.py @@ -52,6 +52,7 @@ def login(): obj = open("login.json", "w") obj.write(json.dumps(login)) obj.close() + print("\nLogin successful!") 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) From 9bcebbce4852dc7f348eebb94d06f5775036c2d1 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Fri, 3 Nov 2017 12:56:25 -0400 Subject: [PATCH 4/6] Did some work on update() --- .gitignore | 1 - actions.py | 29 ++++++++++++++++++++++++++++- launcher.py | 4 ++-- test.py | 3 --- 4 files changed, 30 insertions(+), 7 deletions(-) delete mode 100644 test.py diff --git a/.gitignore b/.gitignore index 9c4e788..6f812cb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ login.json api info.txt __pycache__ -menuLayout.txt diff --git a/actions.py b/actions.py index 3f531c5..c758fb6 100644 --- a/actions.py +++ b/actions.py @@ -4,6 +4,7 @@ import json import datetime import dateutil import os +import subprocess from checks import checkTimestamp from checks import getToken @@ -62,7 +63,7 @@ def clearLogin(): except Exception as e: pass -def clearFolders():# TODO implement this +def clearFolders(): # TODO implement this folders = ["banner", "fanart", "poster"] for folder in folders: if os.path.exists(folder): @@ -127,3 +128,29 @@ def download(imageType, parsed_respObj): else: quit() return saveNameList + + # The following code is from Red-DiscordBot + # https://github.com/Cog-Creators/Red-DiscordBot +def is_git_installed(): + try: + subprocess.call(["git", "--version"], stdout=subprocess.DEVNULL, + stdin =subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + except FileNotFoundError: + return False + else: + return True + +def update(): + try: + code = subprocess.call(("git", "pull", "--ff-only")) + except FileNotFoundError: + print("\nError: Git not found. It's either not installed or you did " + "not clone this using git. Install instructions are on the GitHub: " + "https://github.com/ClaytonWWilson/Image-fetcher-for-theTVDB.com") + return + if code == 0: + print("\nProgram has been updated.") + else: + print("\nThere was an error while updating. This may be caused by edits " + "you have made to the code.") diff --git a/launcher.py b/launcher.py index 0b18ad3..58e34cb 100644 --- a/launcher.py +++ b/launcher.py @@ -45,7 +45,7 @@ while True: print("install requirements") break elif choice == "6": - print("update") - break + update() + wait() elif choice == "0": exit() diff --git a/test.py b/test.py deleted file mode 100644 index bb8b8a3..0000000 --- a/test.py +++ /dev/null @@ -1,3 +0,0 @@ -from login import login - -login() From f8d29565f35c4ad35dbfa69ecf7688d51d735b5c Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Fri, 3 Nov 2017 13:05:56 -0400 Subject: [PATCH 5/6] Edits in actions.py --- actions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actions.py b/actions.py index c758fb6..3f64ef1 100644 --- a/actions.py +++ b/actions.py @@ -143,14 +143,14 @@ def is_git_installed(): def update(): try: - code = subprocess.call(("git", "pull", "--ff-only")) + code = subprocess.call(("git", "pull", "--ff-only", "--quiet")) except FileNotFoundError: print("\nError: Git not found. It's either not installed or you did " "not clone this using git. Install instructions are on the GitHub: " "https://github.com/ClaytonWWilson/Image-fetcher-for-theTVDB.com") return if code == 0: - print("\nProgram has been updated.") + print("\nThe program has been updated.\n") else: print("\nThere was an error while updating. This may be caused by edits " "you have made to the code.") From f36d12a55b4e608f21c81feb8d2b9b4ef7848dc2 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Fri, 3 Nov 2017 13:07:08 -0400 Subject: [PATCH 6/6] Small edit in actions --- actions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actions.py b/actions.py index 3f64ef1..a7d72cd 100644 --- a/actions.py +++ b/actions.py @@ -143,7 +143,7 @@ def is_git_installed(): def update(): try: - code = subprocess.call(("git", "pull", "--ff-only", "--quiet")) + code = subprocess.call(("git", "pull", "--ff-only")) except FileNotFoundError: print("\nError: Git not found. It's either not installed or you did " "not clone this using git. Install instructions are on the GitHub: "