def verify_image(pyconfig): message = \ """ USAGE: pycomic.py verify-image COMCINAME FILETAG NOTE: Use 'pycomic.py list-books' command to get FILETAG value """ try: comic_name = sys.argv[2] request_tag = int(sys.argv[3]) except IndexError: print(message) sys.exit(1) # Check directory structure pylib.check_structure(pyconfig, SECTION) # Find comic from menu csv file try: eng_name, ch_name, number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name, number) comic.file_path(pyconfig.origin(SECTION), 'books-dir') # Get request tag's filename try: request_file = pylib.index_data(comic.path['books-dir'], request_tag, file=False) except pycomic_err.CSVError as err: logger.warning(err) logger.info('Failed to list directory {}'.format( comic.path['books-dir'])) sys.exit(16) except pycomic_err.DataIndexError: logger.info('File Tag {} not found'.format(request_tag)) sys.exit(18) comic.file_path(pyconfig.origin(SECTION), 'book', name=request_file.split('.')[0]) # Verification try: truncated = pylib.verify_images(comic.path['book']) except FileNotFoundError: logger.warning('Directory {} not exist'.format(comic.path['books'])) sys.exit(13) for image in truncated: logger.info('Image file {} in {} {} is truncated'.format( image, comic_name, request_tag)) logger.info('{} {} verification completed'.format(comic_name, request_tag))
def convert(pyconfig): message = \ """ USAGE: pycomic.py convert COMICNAME """ try: comic_name = sys.argv[2] except IndexError: print(message) sys.exit(1) # Chekc config file integrity try: pyconfig.config_test(SECTION, output=True) except pycomic_err.NoSectionError: sys.exit(102) except pycomic_err.NoOptionError: sys.exit(103) # Check directory structure pylib.check_structure(pyconfig, SECTION) _check(pyconfig, SECTION) # Find comic from menu csv file try: eng_name, ch_name, _number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name) comic.file_path(pyconfig.origin(SECTION), 'origin') comic.file_path(pyconfig.formatted(SECTION), 'format') # Convert images try: os.mkdir(comic.path['format']) except FileExistsError: logger.warning('Directory {} already exist'.format( comic.path['format'])) sys.exit(21) try: pylib.convert_images_jpg(comic.path['origin'], comic.path['format']) except IOError: logger.warning('Failed to convert {} to jpeg files'.format( comic.path['origin'])) shutil.rmtree(comic.path['format']) sys.exit(15) else: logger.info('Convert {} to jpeg files success'.format( comic.path['origin']))
def _check_comic_existence(config, comic_name): """ Find comic from menu csv file Exit program if no matching comic found """ try: # return eng_name, ch_name, number, status return pylib.find_menu_comic(config, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11)
def state_change(pyconfig): message = \ """ USAGE: pycomic.py state-change COMICNAME """ try: comic_name = sys.argv[2] except IndexError: print(message) sys.exit(1) # Chekc config file integrity try: pyconfig.config_test(SECTION, output=False) except pycomic_err.NoSectionError: sys.exit(102) except pycomic_err.NoOptionError: sys.exit(103) # Check directory structure pylib.check_structure(pyconfig, SECTION) _check(pyconfig, SECTION) # Find comic from menu csv file try: _eng_name, _ch_name, _number, status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Change comic state COMPLETE = 'complete' IN_PROGRESS = '--------' if status == IN_PROGRESS: status = COMPLETE else: status = IN_PROGRESS try: pylib.modify_menu(pyconfig.main_menu(SECTION), comic_name, status=status) except pycomic_err.UpdateError: logger.warning('Modify {} failed'.format(pyconfig.main_menu(SECTION))) sys.exit(12) # Success message if status == IN_PROGRESS: logger.info('{} marked as in progress'.format(comic_name)) else: logger.info('{} marked as process complete'.format(comic_name))
def make_pdf(pyconfig): message = \ """ USAGE: pycomic.py make-pdf COMICNAME """ try: comic_name = sys.argv[2] except IndexError: print(message) sys.exit(1) # Chekc config file integrity try: pyconfig.config_test(SECTION, output=True) except pycomic_err.NoSectionError: sys.exit(102) except pycomic_err.NoOptionError: sys.exit(103) # Check directory structure pylib.check_structure(pyconfig, SECTION) _check(pyconfig, SECTION) # Find comic from menu csv file try: eng_name, ch_name, _number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name) comic.file_path(pyconfig.formatted(SECTION), 'books') comic.file_path(pyconfig.comics(SECTION), 'pdf') # Make pdf try: pylib.make_pdf(comic.path['books'], comic.path['pdf']) except FileNotFoundError: logger.warning('Directory {} not exist'.format(comic.path['books'])) sys.exit(13) except pycomic_err.FileExistError: logger.info('PDF file {} already exist'.format(comic.path['pdf'])) sys.exit(14) except: logger.warning('Failed to make pdf file from {}'.format( comic.path['books'])) os.remove(comic.path['pdf']) sys.exit(22) else: logger.info('Make PDF {} success'.format(comic.path['pdf']))
def download(pyconfig): message = \ """ USAGE: pycomic.py download COMICNAME """ try: comic_name = sys.argv[2] except IndexError: print(message) sys.exit(1) # Chekc config file integrity try: pyconfig.config_test(SECTION, output=True) except pycomic_err.NoSectionError: sys.exit(102) except pycomic_err.NoOptionError: sys.exit(103) # Check directory structure pylib.check_structure(pyconfig, SECTION) _check(pyconfig, SECTION) # Find comic from menu csv file try: eng_name, ch_name, _number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name) comic.file_path(pyconfig.origin(SECTION), 'book') comic.file_path(pyconfig.refine(SECTION), 'refine') # Download images header = {'User-Agent': pyconfig.useragent(SECTION)} try: os.mkdir(comic.path['book']) except FileExistsError: logger.warning('Directory {} already exist'.format(comic.path['book'])) sys.exit(21) urls = url.extract_images(comic.path['refine']) print('Header: {}'.format(header)) errors = url.download_images(urls, comic.path['book'], header=header) # errors = url.download_images(urls, comic.path['book'], header='Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0') # Show download error messages for error_message in errors: logger.info(error_message)
def fetch_url(pyconfig): message = \ """ USAGE: pycomic.py fetch-url COMICNAME """ try: comic_name = sys.argv[2] except IndexError: print(message) sys.exit(1) # Chekc config file integrity try: pyconfig.config_test(SECTION, output=True) except pycomic_err.NoSectionError: sys.exit(102) except pycomic_err.NoOptionError: sys.exit(103) # Check directory structure pylib.check_structure(pyconfig, SECTION) _check(pyconfig, SECTION) # Find comic from menu csv file try: eng_name, ch_name, _number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name) comic.file_path(pyconfig.raw(SECTION), 'raw') comic.file_path(pyconfig.refine(SECTION), 'refine') # Extract links urls = url.extract_images(comic.path['raw']) # Save links to extract file try: pylib.write_csv(comic.path['refine'], urls) except pycomic_err.CSVError as err: logger.warning('Error: {}'.format(err)) logger.info('Failed to write to {}'.format(comic.path['refine'])) sys.exit(16) else: logger.info('Extract file {} success'.format(eng_name))
def verify(pyconfig): message = \ """ USAGE: pycomic.py verify COMICNAME """ try: comic_name = sys.argv[2] except IndexError: print(message) sys.exit(1) # Chekc config file integrity try: pyconfig.config_test(SECTION, output=True) except pycomic_err.NoSectionError: sys.exit(102) except pycomic_err.NoOptionError: sys.exit(103) # Check directory structure pylib.check_structure(pyconfig, SECTION) _check(pyconfig, SECTION) # Find comic from menu csv file try: eng_name, ch_name, _number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name) comic.file_path(pyconfig.origin(SECTION), 'books') # Verification try: truncated = pylib.verify_images(comic.path['books']) except FileNotFoundError: logger.warning('Directory {} not exist'.format(comic.path['books'])) sys.exit(13) for image in truncated: logger.info('Image file {} in {} is truncated'.format( image, comic_name)) logger.info('{} verification completed'.format(comic_name))
def make_pdf(pyconfig): message = \ """ USAGE: pycomic.py make-pdf COMICNAME FILETAG NOTE: Use 'pycomic.py list-books' command to get FILETAG value """ try: comic_name = sys.argv[2] request_tag = int(sys.argv[3]) except IndexError: print(message) sys.exit(1) # Check directory structure pylib.check_structure(pyconfig, SECTION) # Find comic from menu csv file # To make sure that comic is already added to data structure try: eng_name, ch_name, number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name, number) comic.file_path(pyconfig.formatted(SECTION), 'books-dir') # Get request tag's directory name try: request_file = pylib.index_data(comic.path['books-dir'], request_tag, file=False) except pycomic_err.DataIndexError: logger.info('File Tag {} not found'.format(request_tag)) sys.exit(18) comic.file_path(pyconfig.comics(SECTION), 'pdf-dir') comic.file_path(pyconfig.formatted(SECTION), 'book', name=request_file.split('.')[0]) comic.file_path(pyconfig.comics(SECTION), 'pdf', name=request_file.split('.')[0]) # Make pdf os.makedirs(comic.path['pdf-dir'], exist_ok=True) try: pylib.make_pdf(comic.path['book'], comic.path['pdf']) except FileNotFoundError: logger.warning('Directory {} not exist'.format(comic.path['book'])) sys.exit(13) except pycomic_err.FileExistError: logger.info('PDF file {} already exist'.format(comic.path['pdf'])) sys.exit(14) except Exception as err: logger.warning('Error: {}'.format(err)) logger.warning('Failed to make pdf file from {}'.format( comic.path['book'])) shutil.rmtree(comic.path['pdf']) sys.exit(22) else: logger.info('Make PDF {} success'.format(comic.path['pdf']))
def download(pyconfig): message = \ """ USAGE: pycomic.py download COMICNAME FILETAG NOTE: Use 'pycomic.py list-url' command to get FILETAG value """ try: comic_name = sys.argv[2] request_tag = int(sys.argv[3]) except IndexError: print(message) sys.exit(1) # Check directory structure pylib.check_structure(pyconfig, SECTION) # Find comic from menu csv file try: eng_name, ch_name, number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name, number) comic.file_path(pyconfig.links(SECTION), 'links-dir') # Get request tag's filename try: request_file = pylib.index_data(comic.path['links-dir'], request_tag, file=False) except pycomic_err.CSVError as err: logger.warning(err) logger.info('Failed to list directory {}'.format( comic.path['links-dir'])) sys.exit(16) except pycomic_err.DataIndexError: logger.info('File Tag {} not found'.format(request_tag)) sys.exit(18) comic.file_path(pyconfig.origin(SECTION), 'book', name=request_file.split('.')[0]) comic.file_path(pyconfig.links(SECTION), 'links', name=request_file) # Download images try: os.makedirs(comic.path['book']) except OSError: logger.warning('Directory {} already exist'.format(comic.path['book'])) sys.exit(10) urls = url.extract_images(comic.path['links'], duplicates=True) # Remove book directory if error occur when downloading images header = {'User-Agent': pyconfig.useragent(SECTION)} try: errors = url.download_images(urls, comic.path['book'], header=header) except Exception as err: logger.warning(err) shutil.rmtree(comic.path['book']) sys.exit(41) # Show download error messages for error_message in errors: logger.info(error_message) # Download success logger.info('Download {} {} complete'.format(comic.english, request_tag))
def convert_image(pyconfig): message = \ """ USAGE: pycomic.py convert COMICNAME FILETAG NOTE: Use 'pycomic.py list-books' command to get FILETAG value """ try: comic_name = sys.argv[2] request_tag = int(sys.argv[3]) except IndexError: print(message) sys.exit(1) # Check directory structure pylib.check_structure(pyconfig, SECTION) # Find comic from menu csv file try: eng_name, ch_name, number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Define comic object comic = pylib.Comic(eng_name, ch_name, number) comic.file_path(pyconfig.links(SECTION), 'links-dir') # Get request tag's filename try: request_file = pylib.index_data(comic.path['links-dir'], request_tag, file=False) except pycomic_err.CSVError as err: logger.warning(err) logger.info('Failed to list directory {}'.format( comic.path['links-dir'])) sys.exit(16) except pycomic_err.DataIndexError: logger.info('File Tag {} not found'.format(request_tag)) sys.exit(18) comic.file_path(pyconfig.origin(SECTION), 'origin', name=request_file.split('.')[0]) comic.file_path(pyconfig.formatted(SECTION), 'format', name=request_file.split('.')[0]) # Convert images try: os.makedirs(comic.path['format']) except FileExistsError: logger.warning('Directory {} already exist'.format( comic.path['format'])) sys.exit(21) try: pylib.convert_images_jpg(comic.path['origin'], comic.path['format']) except IOError: logger.warning('Failed to convert {} to jpeg files'.format( comic.path['origin'])) shutil.rmtree(comic.path['format']) sys.exit(15) else: logger.info('Convert {} to jpeg files success'.format( comic.path['origin']))
def eyny_download(pyconfig): message = \ """ USAGE: pycomic.py eyny-download URL """ _EYNY_LOGIN = '******' try: eyny_url = sys.argv[2] except IndexError: print(message) sys.exit(1) # Get user config - eyny user_config = pylib.UserConfig(['user_config.ini']) user_info = user_config.eyny_info() # Chekc config file integrity try: pyconfig.config_test(SECTION, output=False) except pycomic_err.NoSectionError: sys.exit(102) except pycomic_err.NoOptionError: sys.exit(103) # Check directory structure pylib.check_structure(pyconfig, SECTION) _check(pyconfig, SECTION) # Get comic information: ENGLISHNAME CHINESENAME eng_name = input('Comic english name: ') ch_name = input('Comic chinese name: ') # Check to avoid duplicated comic information pylib.check_menu_duplicate(pyconfig, SECTION, ch_name, eng_name) # Open login page driver = pytmp.EynyDriver() try: driver.get(_EYNY_LOGIN) except: logger.info('Driver failed to request {}'.format(_EYNY_LOGIN)) sys.exit(3) # Login driver.login(user_info) # Open URL webpage try: driver.get(eyny_url) except: logger.info('Driver failed to request {}'.format(eyny_url)) sys.exit(3) # Adult confirmation # TODO: Test for adult confirm page driver.adult_confirm() # Open source code inspect tab driver.inspect_source_code(eyny_url) # Add # Get file content from user print('Enter file content. Ctrl-D to save.') contents = [] while True: try: line = input() except EOFError: break contents.append(line) # Define Comic object comic = pylib.Comic(eng_name, ch_name) comic.file_path(pyconfig.raw(SECTION), 'raw') comic.file_path(pyconfig.refine(SECTION), 'refine') comic.file_path(pyconfig.origin(SECTION), 'book') # Add data to main menu book_number = '-----' process_state = '--------' data = [eng_name, ch_name, book_number, process_state] try: pylib.update_menu(pyconfig.main_menu(SECTION), data) except pycomic_err.UpdateError: logger.warning('Update {} failed'.format(pyconfig.main_menu(SECTION))) sys.exit(12) logger.info('Write {} to menu csv file success'.format(data)) # Write raw data file try: pylib.write_txt(comic.path['raw'], contents) except pycomic_err.TXTError as err: logger.warning('Error: {}'.format(err)) logger.info('Failed to write to {}'.format(comic.path['raw'])) # TODO: Delete data that was added to main menu sys.exit(17) else: logger.info('Write contents to {} success'.format(comic.path['raw'])) # Fetch-url # Find comic from menu csv file comic_name = eng_name try: eng_name, ch_name, _number, _status = pylib.find_menu_comic( pyconfig, SECTION, comic_name) except pycomic_err.ComicNotFoundError: logger.info('No match to {} found'.format(comic_name)) sys.exit(11) # Extract links urls = url.extract_images(comic.path['raw']) # Save links to extract file try: pylib.write_csv(comic.path['refine'], urls) except pycomic_err.CSVError as err: logger.warning('Error: {}'.format(err)) logger.info('Failed to write to {}'.format(comic.path['refine'])) sys.exit(16) else: logger.info('Extract file {} success'.format(eng_name)) # Download >> user logout >> close driver driver.download_images(urls) driver.logout() driver.close() # Copy temp images to book location try: shutil.copytree(driver.dir_path, comic.path['book']) except shutil.Error as err: # Directory are the same logger.warning('Error: {}'.format(err)) except OSError as err: logger.warning('Error: {}'.format(err)) else: logger.info('Save images to {} success'.format(comic.path['book']))