From 6d3fdb1734a49a6c72ef3abd6079b882116122aa Mon Sep 17 00:00:00 2001 From: Jacob Stevens Date: Sun, 19 Mar 2023 05:50:48 -0500 Subject: [PATCH] Implemented a form of tui to make it easier to understand and watch. --- calibre_tools.py | 33 ++++++++++++++++---------- log_tools.py | 13 ---------- main.py | 2 +- tools.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 26 deletions(-) delete mode 100644 log_tools.py create mode 100644 tools.py diff --git a/calibre_tools.py b/calibre_tools.py index a790335..8edbfbe 100644 --- a/calibre_tools.py +++ b/calibre_tools.py @@ -3,7 +3,7 @@ import re import cgi import time import requests -from log_tools import * +from tools import * from bs4 import BeautifulSoup from _thread import start_new_thread from urllib.request import urlopen, urlretrieve @@ -62,13 +62,19 @@ class CalibreTools: pass printInfo(f'Found {len(download_links)} books') printInfo(f'Downloading Books. This can take a while') + TerminalInterface.clearTerminal() + TerminalInterface.appendInterface("Info", "Currently Downloading from " + ip_port, 1) while downloaded_books != len(download_links): if CalibreTools.active_threads != dlthread: - start_new_thread(CalibreTools.threadDownloadBook, (download_links[downloaded_books], directory_name, )) - CalibreTools.active_threads += 1 - downloaded_books += 1 + for x in range(3, 3+dlthread): + if CalibreTools.threads.count(x) == 0: + start_new_thread(CalibreTools.threadDownloadBook, (x, download_links[downloaded_books], directory_name, )) + CalibreTools.active_threads += 1 + downloaded_books += 1 + TerminalInterface.appendInterface("Info", "Downloading Books " + str(downloaded_books) + " out of " + str(len(download_links)), 2) else: - time.sleep(1) + TerminalInterface.runInterfaceSchedule() + time.sleep(0.2) @staticmethod def getBooksLink(ip_port, library_name, multiple, max=None): @@ -91,7 +97,8 @@ class CalibreTools: return books_links @staticmethod - def threadDownloadBook(link, directory_name): + def threadDownloadBook(line, link, directory_name): + CalibreTools.threads.append(line) filename = link try: remotefile = urlopen(link) @@ -101,16 +108,18 @@ class CalibreTools: filename = params["filename"] filesize = remoteinfo['Content-Length'] if not os.path.exists(directory_name + "/" + filename): - printInfo(f'Downloading {filename}') + TerminalInterface.appendInterface("Info", f"Thread-{line-3} Downloading {filename}", line) urlretrieve(link, directory_name + "/" + filename) - printInfo(f'Completed {filename}') + TerminalInterface.appendInterface("Info", f"Thread-{line-3} Completed", line) elif not int(filesize) == os.path.getsize(directory_name + "/" + filename): - printInfo(f'Filesize does not match... Downloading file. {filename}') + TerminalInterface.appendInterface("Warning", f'Thread-{line-3} Filesize does not match... Downloading file. {filename}', line) os.remove(directory_name + "/" + filename) urlretrieve(link, directory_name + "/" + filename) - printInfo(f'Completed {filename}') + TerminalInterface.appendInterface("Success", f'Thread-{line-3} Completed {filename}', line) else: - printInfo(f'File Exists... Skipping {filename}') + TerminalInterface.appendInterface("Success", f'Thread-{line-3} File Exists... Skipping {filename}', line) except: - printError(f'Encountered a issue downloading file. Skipping {filename}') + TerminalInterface.appendInterface("Error", f'Thread-{line-3} Encountered a issue downloading file. Skipping {filename}', line) + time.sleep(2) CalibreTools.active_threads -= 1 + CalibreTools.threads.remove(line) diff --git a/log_tools.py b/log_tools.py deleted file mode 100644 index 013f13b..0000000 --- a/log_tools.py +++ /dev/null @@ -1,13 +0,0 @@ -from colorama import Style, Fore - -def printError(message): - print(f'{Fore.RED}[Error] - {message}{Style.RESET_ALL}') - -def printWarning(message): - print(f'{Fore.YELLOW}[Warning] - {message}{Style.RESET_ALL}') - -def printInfo(message): - print(f'{Fore.BLUE}[Info] - {message}{Style.RESET_ALL}') - -def printSuccess(message): - print(f'{Fore.GREEN}[Success] - {message}{Style.RESET_ALL}') \ No newline at end of file diff --git a/main.py b/main.py index 69127e2..3857a43 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import re import argparse -from log_tools import * +from tools import * from calibre_tools import CalibreTools def validateArgs(parser, args): diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..345827a --- /dev/null +++ b/tools.py @@ -0,0 +1,62 @@ +import os +import sys +from colorama import Style, Fore + +def printError(message): + print(f'{Fore.RED}[Error] - {message}{Style.RESET_ALL}') + +def printWarning(message): + print(f'{Fore.YELLOW}[Warning] - {message}{Style.RESET_ALL}') + +def printInfo(message): + print(f'{Fore.BLUE}[Info] - {message}{Style.RESET_ALL}') + +def printSuccess(message): + print(f'{Fore.GREEN}[Success] - {message}{Style.RESET_ALL}') + +class TerminalInterface: + schedule = [] + @staticmethod + def clearTerminal(): + if os.name == 'nt': + _ = os.system('cls') + else: + _ = os.system('clear') + + @staticmethod + def setPos(x, y): + sys.stdout.write("\x1b[%d;%df%s" % (x, y, "")) + sys.stdout.flush() + + @staticmethod + def printPos(x, y, message): + terminal_width = os.get_terminal_size().columns + whitespace_count = terminal_width - len(message) - y + sys.stdout.write("\x1b[%d;%df%s" % (x, y, message + ' ' * whitespace_count)) + sys.stdout.flush() + + @staticmethod + def runInterfaceSchedule(): + if TerminalInterface.schedule: + for x in TerminalInterface.schedule: + TerminalInterface.setPos(x[1], 1) + if x[0] == "Error": + print(Fore.RED + "[Error] ") + TerminalInterface.printPos(x[1], 9, x[2]) + elif x[0] == "Warning": + print(Fore.YELLOW + "[Warning] ") + TerminalInterface.printPos(x[1], 11, x[2]) + elif x[0] == "Info": + print(Fore.BLUE + "[Info] ") + TerminalInterface.printPos(x[1], 8, x[2]) + elif x[0] == "Success": + print(Fore.GREEN + "[Success] ") + TerminalInterface.printPos(x[1], 11, x[2]) + print(Style.RESET_ALL) + sys.stdout.flush() + TerminalInterface.setPos(x[1]-1, 1) + TerminalInterface.schedule.remove(x) + + @staticmethod + def appendInterface(status, message, line): + TerminalInterface.schedule.append([status, line, message]) \ No newline at end of file