def compile_epub(title, author, cover_type, cover_bytes, chapters, images=[], path=None, metadata={}): """Compiles an ePub from the given arguments. The path is where the ePub should be saved to (or with a default name in the current direcory). The chapters should be an iterable of (title, filename, chapter_text) pairs. The images should be an iterable of (title, filename, bytes) pairs pairs""" if not path: path = title + " - " + author + ".epub" print("Compiling epub...") with Epub(title, author, path, cover_type, cover_bytes, metadata=metadata) as epub: epub.add_cover(cover_type, cover_bytes) for (local_name, filename, text) in chapters: epub.add_chapter(local_name, filename, text) for (local_name, filename, image_bytes) in images: epub.add_image(local_name, filename, image_bytes) print("Done!") print("Saved ePub to {!r}".format(path))
def start(urls, output_dir=None, cover_path=None, out_format='epub'): """ start the job using url Args: urls: A string represent the urls which was input by user output_dir: A string represent the path of the output EPUB file cover_path: A string represent the path of the EPUB cover out_format: A string represent the output file format """ for url in urls: for cls in [OldLinovel, Dxs, Wenku]: if cls.check_url(url): novel = cls(url, _SINGLE_THREAD) novel.extract_novel_information() books = novel.get_novel_information() for book in books: epub = Epub(output_dir=output_dir, cover_path=cover_path, out_format=out_format, **book) epub.generate_file() break else: print('URL "{}" is invalid'.format(url))
def save(self, *args, **kwargs): if not self.cover_img: if self.book_file.name.endswith('.epub'): # get the cover path from the epub file epub_file = Epub(self.book_file) cover_path = epub_file.get_cover_image_path() if cover_path is not None and os.path.exists(cover_path): cover_file = File(open(cover_path)) self.cover_img.save(os.path.basename(cover_path), cover_file) epub_file.close() super(Book, self).save(*args, **kwargs)
def write_to_epub(novelids, login, session): for novelid in novelids: e = Epub() e.summary = get_summary(novelid, session) e.intro = get_intro(novelid, session) e.author, e.name, contents = get_contents(novelid, login, session) e.init() pic_con = get_pic(novelid, e.filename, session) e.write_pic(pic_con) e.write_coverandintro() e.write_chapters(contents) for chapter in contents: if chapter[0]: text = get_all_text(chapter[2], session) print(chapter[1][0], " ", chapter[1][1]) e.write_text(chapter[1], text) e.packet()
def clean_epub_file(self): """Perform basic validation of the epub_file by making sure: - no other existing models have the same sha256 hash. - it is parseable by `Epub`. TODO: This method is called twice during the wizard (at step 0, and at done()), by Django design. Still, we should look for alternatives in order to make sure epub validation only happens once. https://code.djangoproject.com/ticket/10810 """ data = self.cleaned_data['epub_file'] # Validate sha256 hash. sha256sum = models.sha256_sum(data) if models.Book.objects.filter(file_sha256sum=sha256sum).exists(): raise forms.ValidationError('The file is already on the database') # Validate parseability. epub = None try: # Fetch information from the epub, and set it as attributes. epub = Epub(data) info_dict, cover_path, tags = epub.as_model_dict() # TODO: pass this info via a cleaner way. self.cleaned_data['original_path'] = data.name self.cleaned_data['info_dict'] = info_dict self.cleaned_data['cover_path'] = cover_path self.cleaned_data['file_sha256sum'] = sha256sum except Exception as e: raise forms.ValidationError(str(e)) finally: # Try to remove the temp extracted epub folder. try: epub.close() except: pass return data
def grab_volume(url, output_dir, cover_path): """ grab volume Args: url: A string represent the url which was input by user output_dir: A string represent the path of the output EPUB file cover_file: A string represent the path of the EPUB cover """ try: print_info('Getting:' + url) novel = Novel(url=url, single_thread=SINGLE_THREAD) novel.get_novel_information() epub = Epub(output_dir=output_dir, cover_path=cover_path, **novel.novel_information()) epub.generate_epub() except Exception as e: if HAS_QT: SENDER.sigWarningMessage.emit('错误', str(e) + '\nat:' + url) SENDER.sigButton.emit() print(url) raise e