def get_list_of_filtered_books(languages, formats, only_books=[]): if len(formats): qs = (Book.select().join(BookFormat).join(Format).where( Format.mime << [FORMAT_MATRIX.get(f) for f in formats]).group_by(Book.id)) else: qs = Book.select() if len(only_books): # print(only_books) qs = qs.where(Book.id << only_books) if len(languages): qs = qs.where(Book.language << languages) return qs
def download_cover(book, book_dir, s3_storage, optimizer_version): has_cover = Book.select(Book.cover_page).where(Book.id == book.id) if has_cover: # try to download optimized cover from cache if s3_storage url = "{}{}/pg{}.cover.medium.jpg".format(IMAGE_BASE, book.id, book.id) etag = get_etag_from_url(url) downloaded_from_cache = False cover = "{}_cover_image.jpg".format(book.id) if (book_dir.joinpath("optimized").joinpath(cover).exists() or book_dir.joinpath("unoptimized").joinpath(cover).exists()): logger.debug(f"Cover already exists for book #{book.id}") return if s3_storage: logger.info( f"Trying to download cover for {book.id} from optimization cache" ) downloaded_from_cache = download_from_cache( book=book, etag=etag, book_format="cover", dest_dir=book_dir.joinpath("optimized"), s3_storage=s3_storage, optimizer_version=optimizer_version, ) if not downloaded_from_cache: logger.debug("Downloading {}".format(url)) if download_file(url, book_dir.joinpath("unoptimized").joinpath(cover)): book.cover_etag = etag book.save() else: logger.debug("No Book Cover found for Book #{}".format(book.id))
def get_list_of_filtered_books(languages, formats, only_books=[]): if len(formats): qs = Book.select().join(BookFormat) \ .join(Format) \ .where(Format.mime << [FORMAT_MATRIX.get(f) for f in formats]) \ .group_by(Book.id) else: qs = Book.select() if len(only_books): # print(only_books) qs = qs.where(Book.id << only_books) if len(languages): qs = qs.where(Book.language << languages) return qs
def download_covers(book, download_cache): cover = "{}_cover.jpg".format(book.id) fpath = os.path.join(download_cache, cover) has_cover = Book.select(Book.cover_page).where(Book.id == book.id) if has_cover: title = "{}{}/pg{}.cover.medium.jpg".format(IMAGE_BASE, book.id, book.id) logger.debug("Downloading {}".format(title)) download_file(title, fpath) else: logger.debug("No Book Cover found for Book #{}".format(book.id)) return True
def get_list_of_all_languages(): return list(set(list([b.language for b in Book.select(Book.language)])))