def _load_ebook(self, everything, filename): if "path" not in everything[filename].keys(): return False, None eb = Epub( everything[filename]["path"], self.config["library_dir"], self.config["author_aliases"], self.ebook_filename_template, ) return eb.load_from_database_json(everything[filename], filename), eb
def _return_or_create_new_ebook(self, full_path, known_ebooks): is_already_in_db = False for eb in known_ebooks: if eb.path == full_path: is_already_in_db = True eb.open_ebook_metadata() return eb if not is_already_in_db: eb = Epub( full_path, self.config["library_dir"], self.config["author_aliases"], self.ebook_filename_template ) eb.open_ebook_metadata() print(" -> NEW EBOOK: ", eb) return eb return None
def import_new_ebooks(self): # multithreaded conversion to epub before import, if necessary cpt = 1 all_mobis = [ os.path.join(self.config["import_dir"], el) for el in os.listdir(self.config["import_dir"]) if el.endswith(".mobi") ] with ThreadPoolExecutor(max_workers=cpu_count()) as executor: future_epubs = {executor.submit(self._convert_to_epub_before_importing, mobi): mobi for mobi in all_mobis} for future in as_completed(future_epubs): if future.result() == 0: print(" %.2f%%" % (100 * cpt / len(all_mobis)), end="\r", flush=True) cpt += 1 else: raise Exception("Error converting to epub!") all_ebooks = [el for el in os.listdir(self.config["import_dir"]) if el.endswith(".epub")] if len(all_ebooks) == 0: print("Nothing new to import.") return False else: print("Importing.") all_already_imported_ebooks = [el for el in os.listdir(self.config["imported_dir"]) if el.endswith(".epub")] already_imported_hashes = [] for eb in all_already_imported_ebooks: already_imported_hashes.append( hashlib.sha1(open(os.path.join(self.config["imported_dir"], eb), "rb").read()).hexdigest() ) start = time.perf_counter() imported_count = 0 for ebook in all_ebooks: ebook_candidate_full_path = os.path.join(self.config["import_dir"], ebook) # check for duplicate hash new_hash = hashlib.sha1(open(ebook_candidate_full_path, "rb").read()).hexdigest() if new_hash in already_imported_hashes: print(" -> skipping already imported: ", ebook) continue # check for complete metadata temp_ebook = Epub( ebook_candidate_full_path, self.config["library_dir"], self.config["author_aliases"], self.ebook_filename_template, ) temp_ebook.open_ebook_metadata() if not temp_ebook.librarian_metadata.is_complete: print(" -> skipping ebook with incomplete metadata: ", ebook) continue # check if book not already in library already_in_db = False for eb in self.ebooks: same_authors = eb.librarian_metadata.get_values("author") == temp_ebook.librarian_metadata.get_values( "author" ) same_title = eb.librarian_metadata.get_values("title") == temp_ebook.librarian_metadata.get_values( "title" ) if same_authors and same_title: already_in_db = True break if already_in_db: print( " -> library already contains an entry for: ", temp_ebook.librarian_metadata.get_values("author")[0], " - ", temp_ebook.librarian_metadata.get_values("title")[0], ": ", ebook, ) continue if self.config.get("interactive", True): print("About to import: %s" % str(temp_ebook)) answer = input("Confirm? \ny/n? ") if answer.lower() == "n": print(" -> skipping ebook ", ebook) continue # if all checks are ok, importing print(" ->", ebook) # backup if self.config["backup_imported_ebooks"]: # backup original mobi version if it exists mobi_full_path = ebook_candidate_full_path.replace(".epub", ".mobi") if os.path.exists(mobi_full_path): shutil.move( mobi_full_path, os.path.join(self.config["imported_dir"], ebook.replace(".epub", ".mobi")) ) shutil.copyfile(ebook_candidate_full_path, os.path.join(self.config["imported_dir"], ebook)) # import shutil.move(ebook_candidate_full_path, os.path.join(self.config["library_dir"], ebook)) imported_count += 1 print("Imported ebooks in %.2fs." % (time.perf_counter() - start)) if imported_count != 0: return True else: return False