main
Aplikace pro stahování souborů z internetu. Ze stránek jako:
1""" 2Aplikace pro stahování souborů z internetu. 3Ze stránek jako: 4- https://sdilej.cz 5- https://datoid.cz 6- https://prehrajto.cz 7""" 8 9import os 10import sys 11import queue 12import argparse 13import threading 14from time import sleep 15from src.link_to_file import * 16from src.downloader.sdilej import Sdilej_downloader 17from src.downloader.datoid import Datoid_downloader 18from src.downloader.prehrajto import Prehrajto_downloader 19from src.downloader.page_search import Download_page_search 20 21JSON_FILE = "files.json" 22FAILED_FILES = "failed_files.json" 23DEBUG = False 24VERBOSE = True 25 26download_folder = "download" 27log_folder = "logs" 28 29prompt = "karel capek" 30file_type = "audio" 31 32os.makedirs(log_folder, exist_ok=True) 33 34def read_input(input_queue): 35 while True: 36 input_queue.put(sys.stdin.read(1)) 37 38if __name__ == "__main__": 39 # parse arguments 40 parser = argparse.ArgumentParser(description="Download files from internet.") 41 parser.add_argument("-s", "--search", type=str, help="Search for files.") 42 parser.add_argument("-t", "--file-type", type=str, choices=Download_page_search.file_types.keys(), help="Type of files to search for.") 43 parser.add_argument("-T", "--search-type", type=str, choices=Download_page_search.search_types.keys(), help="Search format.") 44 parser.add_argument("-d", "--download", action="store_true", help="Download the found files.") 45 parser.add_argument("-f", "--file", type=str, help="File to download.") 46 parser.add_argument("-F", "--folder", type=str, help="Folder to download to.") 47 parser.add_argument("-n", "--number", type=int, help="Max number of files to search.") 48 parser.add_argument("-v", "--verbose", action="store_true", help="Verbose mode.") 49 parser.add_argument("-D", "--debug", action="store_true", help="Debug mode.") 50 parser.add_argument("-g", "--tui", action="store_true", help="Start TUI.") 51 parser.add_argument("-G", "--gui", action="store_true", help="Start GUI.") 52 parser.add_argument("-r", "--remove", action="store_true", help="Remove downloaded files from the list.") 53 args = parser.parse_args() 54 55 if args.verbose: 56 VERBOSE = True 57 set_verbose(True) 58 59 if args.debug: 60 DEBUG = True 61 62 if args.folder: 63 # folder exists? 64 if not os.path.exists(args.folder): 65 raise ValueError(f"Directory {args.folder} does not exist!") 66 download_folder = args.folder 67 68 if args.file: 69 JSON_FILE = args.file 70 71 # start TUI 72 if args.tui: 73 import tui 74 tui.main() 75 exit(0) 76 77 # start GUI 78 if args.gui: 79 import gui 80 gui.main() 81 exit(0) 82 83 input_queue = queue.Queue() 84 input_thread = threading.Thread(target=read_input, args=(input_queue,)) 85 input_thread.daemon = True 86 input_thread.start() 87 88 # search for files 89 if args.search: 90 prompt = args.search 91 file_type = args.file_type if args.file_type else "all" 92 search_type = args.search_type if args.search_type else "relevance" 93 94 link_2_files = [] 95 for i, link_2_file in enumerate(Prehrajto_downloader().search(prompt, file_type, search_type)): 96 if args.number and i >= args.number: 97 break 98 if DEBUG: 99 print("") 100 print(link_2_file.colorize()) 101 link_2_files.append(link_2_file) 102 103 print_info(f"Number of files: {len(link_2_files)}") 104 add_links_to_file(link_2_files, JSON_FILE) 105 106 if args.download: 107 link_2_files = load_links_from_file(JSON_FILE) 108 successfull_files = [] 109 for link_2_file in link_2_files: 110 # Check for 'q' key press to exit 111 if not input_queue.empty() and input_queue.get() == 'q': 112 print_info("Exiting download loop.") 113 break 114 115 # test if file exists 116 if os.path.exists(f"{download_folder}/{link_2_file.title}"): 117 print_info(f"File {link_2_file.title} already exists.") 118 successfull_files.append(link_2_file) 119 continue 120 121 # download file 122 if VERBOSE: 123 print_info(f"Downloading file: {Blue}{link_2_file.title}{NC} of size {Blue}{link_2_file.size}{NC}...") 124 link_2_file.download(download_folder) 125 126 # test file size > 1kb 127 file_size = os.path.getsize(f"{download_folder}/{link_2_file.title}") 128 if (not compare_sizes(file_size, link_2_file.size, 20/100) and link_2_file.size != None) or (link_2_file.size == None and file_size < 1024): 129 print_warning(f"File {Blue}{link_2_file.title}{NC} was not downloaded correctly.") 130 print_info(f"File size: {Blue}{file_size}{NC} expected: {Blue}{link_2_file.size}{NC}") 131 if not DEBUG: 132 os.remove(f"{download_folder}/{link_2_file.title}") 133 print_info(f"File {Blue}{link_2_file.title}{NC} was removed.") 134 save_links_to_file([link_2_file], FAILED_FILES, append=True) 135 else: 136 successfull_files.append(link_2_file) 137 if VERBOSE: 138 print_success(f"File {Blue}{link_2_file.title}{NC} of size {Blue}{size_int_2_string(file_size)}{NC} was downloaded.") 139 140 # wait 141 sleep_time = 60 142 print_info(f"Wating {sleep_time}s...") 143 sleep(sleep_time) 144 145 if args.remove and len(successfull_files) > 0: 146 if VERBOSE: 147 print_info("Removing downloaded files from the list...") 148 remove_links_from_file(successfull_files, JSON_FILE)
JSON_FILE =
'files.json'
FAILED_FILES =
'failed_files.json'
DEBUG =
False
VERBOSE =
True
download_folder =
'download'
log_folder =
'logs'
prompt =
'karel capek'
file_type =
'audio'
def
read_input(input_queue):