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