Implemented a form of tui to make it easier to understand and watch.

This commit is contained in:
Jacob Stevens 2023-03-19 05:50:48 -05:00
parent 14cb9f5003
commit 6d3fdb1734
4 changed files with 84 additions and 26 deletions

View File

@ -3,7 +3,7 @@ import re
import cgi import cgi
import time import time
import requests import requests
from log_tools import * from tools import *
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from _thread import start_new_thread from _thread import start_new_thread
from urllib.request import urlopen, urlretrieve from urllib.request import urlopen, urlretrieve
@ -62,13 +62,19 @@ class CalibreTools:
pass pass
printInfo(f'Found {len(download_links)} books') printInfo(f'Found {len(download_links)} books')
printInfo(f'Downloading Books. This can take a while') 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): while downloaded_books != len(download_links):
if CalibreTools.active_threads != dlthread: if CalibreTools.active_threads != dlthread:
start_new_thread(CalibreTools.threadDownloadBook, (download_links[downloaded_books], directory_name, )) 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 CalibreTools.active_threads += 1
downloaded_books += 1 downloaded_books += 1
TerminalInterface.appendInterface("Info", "Downloading Books " + str(downloaded_books) + " out of " + str(len(download_links)), 2)
else: else:
time.sleep(1) TerminalInterface.runInterfaceSchedule()
time.sleep(0.2)
@staticmethod @staticmethod
def getBooksLink(ip_port, library_name, multiple, max=None): def getBooksLink(ip_port, library_name, multiple, max=None):
@ -91,7 +97,8 @@ class CalibreTools:
return books_links return books_links
@staticmethod @staticmethod
def threadDownloadBook(link, directory_name): def threadDownloadBook(line, link, directory_name):
CalibreTools.threads.append(line)
filename = link filename = link
try: try:
remotefile = urlopen(link) remotefile = urlopen(link)
@ -101,16 +108,18 @@ class CalibreTools:
filename = params["filename"] filename = params["filename"]
filesize = remoteinfo['Content-Length'] filesize = remoteinfo['Content-Length']
if not os.path.exists(directory_name + "/" + filename): 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) 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): 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) os.remove(directory_name + "/" + filename)
urlretrieve(link, directory_name + "/" + filename) urlretrieve(link, directory_name + "/" + filename)
printInfo(f'Completed {filename}') TerminalInterface.appendInterface("Success", f'Thread-{line-3} Completed {filename}', line)
else: else:
printInfo(f'File Exists... Skipping {filename}') TerminalInterface.appendInterface("Success", f'Thread-{line-3} File Exists... Skipping {filename}', line)
except: 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.active_threads -= 1
CalibreTools.threads.remove(line)

View File

@ -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}')

View File

@ -1,7 +1,7 @@
import re import re
import argparse import argparse
from log_tools import * from tools import *
from calibre_tools import CalibreTools from calibre_tools import CalibreTools
def validateArgs(parser, args): def validateArgs(parser, args):

62
tools.py Normal file
View File

@ -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])