2023-03-19 02:40:18 -05:00
import re
import argparse
2023-03-19 05:50:48 -05:00
from tools import *
2023-03-19 02:40:18 -05:00
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 :
2023-04-12 16:08:51 -05:00
# TODO: Export to a txt file to free up ram once completed
2023-03-19 02:40:18 -05:00
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 )
2023-03-19 03:05:59 -05:00
if args . thread is not None :
2023-03-19 02:40:18 -05:00
thr = args . thread
for library in libraries :
printInfo ( f ' Counting books from library: { library } ' )
2023-04-12 16:08:51 -05:00
CalibreTools . downloadBooks ( ip_port , library , None , update , format , thr )
2023-03-19 02:40:18 -05:00
printSuccess ( " Download Complete! " )
exit ( 0 )
2023-04-12 16:08:51 -05:00
# TODO: Add support to specify what format you want to download.
# Ex. -f epub,pdf
# -f epub
2023-03-19 02:40:18 -05:00
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 ' )
2023-04-12 16:08:51 -05:00
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 ' )
2023-03-19 02:40:18 -05:00
parser . usage = '''
CALIBRE DUMPER
1. Download all books from all libraries of calibre server using - - calibre - host < IP : PORT > or - c < IP : PORT >
2. Specify library using - - library < LIBRARY NAME > or - l < LIBRARY NAME >
2023-04-12 16:08:51 -05:00
4. Specify what format youd like to download . Ex - f Ex . \\. epub | \\. mobi | \\. pdf
2023-03-19 02:40:18 -05:00
3. Specify how many download threads to use - - threads < NUMBER > or - t < NUMBER >
'''
args = parser . parse_args ( )
validateArgs ( parser , args )
if args . ip_port is not None :
calibre_implementation ( args . ip_port )