1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-25 02:57:22 +00:00
calibre-web/cps/book_formats.py

100 lines
3.2 KiB
Python
Raw Normal View History

__author__ = 'lemmsh'
2016-06-05 16:42:18 +00:00
import logging
logger = logging.getLogger("book_formats")
import uploader
import os
try:
from wand.image import Image
use_generic_pdf_cover = False
except ImportError, e:
2016-06-05 19:28:30 +00:00
logger.warning('cannot import Image, generating pdf covers for pdf uploads will not work: %s', e)
use_generic_pdf_cover = True
2016-06-05 16:42:18 +00:00
try:
from PyPDF2 import PdfFileReader
use_pdf_meta = True
except ImportError, e:
2016-06-05 19:28:30 +00:00
logger.warning('cannot import PyPDF2, extracting pdf metadata will not work: %s', e)
2016-06-05 16:42:18 +00:00
use_pdf_meta = False
2016-06-05 19:28:30 +00:00
try:
import epub
use_epub_meta = True
except ImportError, e:
2016-08-07 16:46:38 +00:00
logger.warning('cannot import epub, extracting epub metadata will not work: %s', e)
2016-06-05 19:28:30 +00:00
use_epub_meta = False
2016-06-18 13:50:32 +00:00
try:
import fb2
use_fb2_meta = True
except ImportError, e:
2016-08-07 16:46:38 +00:00
logger.warning('cannot import fb2, extracting fb2 metadata will not work: %s', e)
2016-06-18 13:50:32 +00:00
use_fb2_meta = False
2016-06-05 19:28:30 +00:00
def process(tmp_file_path, original_file_name, original_file_extension):
2016-06-05 19:28:30 +00:00
try:
if ".PDF" == original_file_extension.upper():
return pdf_meta(tmp_file_path, original_file_name, original_file_extension)
2016-06-18 13:50:32 +00:00
if ".EPUB" == original_file_extension.upper() and use_epub_meta == True:
2016-06-05 19:28:30 +00:00
return epub.get_epub_info(tmp_file_path, original_file_name, original_file_extension)
2016-06-18 13:50:32 +00:00
if ".FB2" == original_file_extension.upper() and use_fb2_meta == True:
return fb2.get_fb2_info(tmp_file_path, original_file_name, original_file_extension)
2016-06-05 19:28:30 +00:00
except Exception, e:
logger.warning('cannot parse metadata, using default: %s', e)
return default_meta(tmp_file_path, original_file_name, original_file_extension)
2016-06-05 16:52:28 +00:00
def default_meta(tmp_file_path, original_file_name, original_file_extension):
return uploader.BookMeta(
file_path = tmp_file_path,
extension = original_file_extension,
title = original_file_name,
author = "Unknown",
cover = None,
description = "",
tags = "",
series = "",
series_id="")
def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
2016-06-05 16:42:18 +00:00
if (use_pdf_meta):
pdf = PdfFileReader(open(tmp_file_path, 'rb'))
doc_info = pdf.getDocumentInfo()
else:
doc_info = None
if (doc_info is not None):
2016-08-07 21:32:55 +00:00
author = doc_info.author if doc_info.author is not None else "Unknown"
title = doc_info.title if doc_info.title is not None else original_file_name
subject = doc_info.subject
else:
author = "Unknown"
title = original_file_name
subject = ""
return uploader.BookMeta(
file_path = tmp_file_path,
extension = original_file_extension,
title = title,
author = author,
cover = pdf_preview(tmp_file_path, original_file_name),
description = subject,
tags = "",
series = "",
series_id="")
def pdf_preview(tmp_file_path, tmp_dir):
if use_generic_pdf_cover:
return None
else:
cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg"
with Image(filename=tmp_file_path + "[0]", resolution=150) as img:
img.compression_quality = 88
img.save(filename=os.path.join(tmp_dir, cover_file_name))
return cover_file_name