import re import argparse from tools import * from calibre_tools import CalibreTools def validateArgs(parser, args): if args.ip_port is None and args.library is None: printError("Minimum one parameter required") exit(0) if args.ip_port is not None: if not re.match('^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d+$', args.ip_port) and not re.match('(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])):\\d+$', args.ip_port): printError("Invalid IP") exit(0) def calibre_implementation(ip_port): printInfo("Checking if server is accessible") if not CalibreTools.checkServer(ip_port): printError("Server requires authentication.") exit(0) printSuccess("Server is accessible") printInfo("Getting Libraries") libraries = [] thr = 1 if args.lib is None: # TODO: Export to a txt file to free up ram once completed libraries = CalibreTools.getLibraries(ip_port) if len(libraries) == 0: printError("Could not find any libraries") exit(0) printSuccess(f'Found {len(libraries)} libraries') else: libraries.append(args.lib) if args.thread is not None: thr = args.thread for library in libraries: printInfo(f'Counting books from library: {library}') CalibreTools.downloadBooks(ip_port, library, None, update, format, thr) printSuccess("Download Complete!") exit(0) # TODO: Add support to specify what format you want to download. # Ex. -f epub,pdf # -f epub if __name__ == '__main__': parser = argparse.ArgumentParser("Calibre Dumper") parser.add_argument('-c', '--calibre-host', type=str, dest='ip_port', action='store', help='Provide ip and port of calibre server, Format ip:port') parser.add_argument('-l', '--library', type=str, dest='lib', action='store', help='Specify which library to download') parser.add_argument('-t', '--threads', type=int, dest='thread', action='store', help='Specify how many download threads to use') parser.add_argument('-f', '--format', type=str, dest='format', action='store', help='Specify what format youd like to download. Ex. \\.epub|\\.mobi|\\.pdf') parser.add_argument('-u', '--update', type=bool, dest='update', action='store', help='Force Scraper to get updated list of books') parser.usage = ''' CALIBRE DUMPER 1. Download all books from all libraries of calibre server using --calibre-host or -c 2. Specify library using --library or -l 4. Specify what format youd like to download. Ex -f Ex. \\.epub|\\.mobi|\\.pdf 3. Specify how many download threads to use --threads or -t ''' args = parser.parse_args() validateArgs(parser, args) if args.ip_port is not None: calibre_implementation(args.ip_port)