def save_entry_to_cbz(self, uri: str): """Save a chapter to a local \".cbz\" file.""" logger = self.logger chapters = self.parse(uri) for ch_uri, ch_title in chapters: logger.info('-' * DRAW_LINE_LENGTH) logger.info('{} ({})'.format(ch_title, ch_uri)) try: chapter_dl = self.download_chapter_gen(ch_uri) dl_counter = 0 print('Pages: [{}/{}]'.format(dl_counter, self.__page_sum), end='\r') if chapter_dl: dl_file = 'dl.txt' complete = False try: with zipfile.ZipFile('{}.cbz'.format(rectify_basename(ch_title)), 'r') as cbz: if dl_file in cbz.namelist(): complete = True cbz.close() except (zipfile.BadZipFile, FileNotFoundError): pass if complete: logger.info('(skip already downloaded)') else: with zipfile.ZipFile('{}.cbz'.format(rectify_basename(ch_title)), 'w') as cbz: for image, name, size in chapter_dl: dl_counter += 1 logger.debug('{}: {} ({})'.format(ch_title, name, size)) print('Pages: [{}/{}] ({}x)'.format(dl_counter, self.__page_sum, self.max_dl), end='\r') cbz.writestr(name, image) cbz.writestr(dl_file, datetime.datetime.utcnow().replace( tzinfo=datetime.timezone.utc).astimezone().replace(microsecond=0).isoformat()) cbz.close() print() else: print('(skip empty)') except HentaiParseError: logger.warning('ERROR WHEN PARSING THE MANGA!')
def save_entry_to_cbz(self, uri: str): """Save a chapter to a local \".cbz\" file.""" logger = self.logger chapters = self.parse(uri) for ch_uri, ch_title in chapters: logger.debug((ch_uri, ch_title)) logger.info('{} ({})'.format(ch_title, ch_uri)) chapter_dl = self.download_chapter_gen(ch_uri) if chapter_dl: with zipfile.ZipFile( '{}.cbz'.format(rectify_basename(ch_title)), 'w') as cbz: logger.debug(cbz) for image, name, size in chapter_dl: logger.info('{}: {} ({})'.format(ch_title, name, size)) cbz.writestr(name, image) cbz.close()
def save_gallery_to_cbz(self, gallery_src: tuple, dl_dir: str = ''): if dl_dir: self.logger.info('Download to dir: {}'.format(dl_dir)) title, all_pic_url_list = gallery_src cbz_name = rectify_basename(title) + '.cbz' self.logger.info('Save to file: {}'.format(cbz_name)) cbz_path = os.path.join(dl_dir, cbz_name) err_log_path = cbz_path + '.error.log' with zipfile.ZipFile(cbz_path, 'w') as cbz: self.logger.debug(cbz) for pic_name, pic_data in self.dl_pool.imap_unordered(self.download_picture, all_pic_url_list): try: cbz.writestr(pic_name, pic_data) except HentaiDownloadError as e: with open(err_log_path, 'a') as error_log: error_log.write(str(e)) error_log.write('\n') error_log.close() cbz.close()
def download(pid: str, purl: str, name: str): global server dl_dir = stat['dd'] pdb = db.get(pid) image_from_photo(pid, purl, pdb) pdb = db[pid] if len(name.encode()) > 240: name = name[:50].rstrip() + ' ... ' + name[-25:].lstrip() pdb['name'] = rectify_basename(name) url = pdb['url'] file = os.path.join(dl_dir, '{} - {}{}'.format(pdb['name'], pid, pdb['ext'])) _logger.debug('{} <-> {}'.format(pdb, file)) while True: try: if os.path.exists(file): local_size = os.stat(file).st_size pdb_remote_size = pdb['size'] if pdb_remote_size: remote_size = pdb_remote_size else: remote_size = content_length(head(url)) pdb['size'] = remote_size if local_size == remote_size: _logger.info('Good: {}'.format(file)) break else: _logger.warning('Bad: {}'.format(file)) r = get(url) if r.status_code == 200: with open(file, 'wb') as f: f.write(r.content) pdb['size'] = content_length(r) _logger.info('OK: {} -> {}'.format(url, file)) break else: _logger.warning('Fail: {}'.format(url)) except Exception: raise