2017-01-29 12:06:08 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-12-23 00:53:39 -08:00
|
|
|
import logging
|
|
|
|
import uploader
|
|
|
|
import os
|
|
|
|
from flask_babel import gettext as _
|
|
|
|
|
2016-06-05 08:41:47 -07:00
|
|
|
__author__ = 'lemmsh'
|
|
|
|
|
2016-06-05 09:42:18 -07:00
|
|
|
logger = logging.getLogger("book_formats")
|
|
|
|
|
2016-06-05 08:41:47 -07:00
|
|
|
try:
|
|
|
|
from wand.image import Image
|
2016-12-23 00:53:39 -08:00
|
|
|
from wand import version as ImageVersion
|
2016-06-05 08:41:47 -07:00
|
|
|
use_generic_pdf_cover = False
|
2017-03-05 01:40:39 -08:00
|
|
|
except ImportError as e:
|
2016-06-05 12:28:30 -07:00
|
|
|
logger.warning('cannot import Image, generating pdf covers for pdf uploads will not work: %s', e)
|
2016-06-05 08:41:47 -07:00
|
|
|
use_generic_pdf_cover = True
|
2016-06-05 09:42:18 -07:00
|
|
|
try:
|
|
|
|
from PyPDF2 import PdfFileReader
|
2016-12-23 00:53:39 -08:00
|
|
|
from PyPDF2 import __version__ as PyPdfVersion
|
2016-06-05 09:42:18 -07:00
|
|
|
use_pdf_meta = True
|
2017-03-05 01:40:39 -08:00
|
|
|
except ImportError as e:
|
2016-06-05 12:28:30 -07:00
|
|
|
logger.warning('cannot import PyPDF2, extracting pdf metadata will not work: %s', e)
|
2016-06-05 09:42:18 -07:00
|
|
|
use_pdf_meta = False
|
2016-06-05 08:41:47 -07:00
|
|
|
|
2016-06-05 12:28:30 -07:00
|
|
|
try:
|
|
|
|
import epub
|
|
|
|
use_epub_meta = True
|
2017-03-05 01:40:39 -08:00
|
|
|
except ImportError as e:
|
2016-08-07 09:46:38 -07:00
|
|
|
logger.warning('cannot import epub, extracting epub metadata will not work: %s', e)
|
2016-06-05 12:28:30 -07:00
|
|
|
use_epub_meta = False
|
|
|
|
|
2016-06-18 06:50:32 -07:00
|
|
|
try:
|
|
|
|
import fb2
|
|
|
|
use_fb2_meta = True
|
2017-03-05 01:40:39 -08:00
|
|
|
except ImportError as e:
|
2016-08-07 09:46:38 -07:00
|
|
|
logger.warning('cannot import fb2, extracting fb2 metadata will not work: %s', e)
|
2016-06-18 06:50:32 -07:00
|
|
|
use_fb2_meta = False
|
|
|
|
|
2016-06-05 12:28:30 -07:00
|
|
|
|
2016-06-05 08:41:47 -07:00
|
|
|
def process(tmp_file_path, original_file_name, original_file_extension):
|
2017-04-22 23:22:10 -07:00
|
|
|
meta = None
|
2016-06-05 12:28:30 -07:00
|
|
|
try:
|
|
|
|
if ".PDF" == original_file_extension.upper():
|
2017-04-22 23:22:10 -07:00
|
|
|
meta = pdf_meta(tmp_file_path, original_file_name, original_file_extension)
|
2016-12-23 00:53:39 -08:00
|
|
|
if ".EPUB" == original_file_extension.upper() and use_epub_meta is True:
|
2017-04-22 23:22:10 -07:00
|
|
|
meta = epub.get_epub_info(tmp_file_path, original_file_name, original_file_extension)
|
2016-12-23 00:53:39 -08:00
|
|
|
if ".FB2" == original_file_extension.upper() and use_fb2_meta is True:
|
2017-04-22 23:22:10 -07:00
|
|
|
meta = fb2.get_fb2_info(tmp_file_path, original_file_extension)
|
2017-03-05 01:40:39 -08:00
|
|
|
except Exception as e:
|
2016-06-05 12:28:30 -07:00
|
|
|
logger.warning('cannot parse metadata, using default: %s', e)
|
2017-04-22 23:22:10 -07:00
|
|
|
|
|
|
|
if meta and meta.title.strip() and meta.author.strip():
|
|
|
|
return meta
|
|
|
|
else:
|
|
|
|
return default_meta(tmp_file_path, original_file_name, original_file_extension)
|
2016-06-05 09:52:28 -07:00
|
|
|
|
|
|
|
|
|
|
|
def default_meta(tmp_file_path, original_file_name, original_file_extension):
|
|
|
|
return uploader.BookMeta(
|
2016-12-23 00:53:39 -08:00
|
|
|
file_path=tmp_file_path,
|
|
|
|
extension=original_file_extension,
|
|
|
|
title=original_file_name,
|
2017-02-04 05:28:18 -08:00
|
|
|
author=u"Unknown",
|
2016-12-23 00:53:39 -08:00
|
|
|
cover=None,
|
|
|
|
description="",
|
|
|
|
tags="",
|
|
|
|
series="",
|
2017-03-02 03:59:35 -08:00
|
|
|
series_id="",
|
2017-03-02 06:57:02 -08:00
|
|
|
languages="")
|
2016-06-05 08:41:47 -07:00
|
|
|
|
|
|
|
|
|
|
|
def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
|
2016-06-05 09:42:18 -07:00
|
|
|
|
2016-12-23 00:53:39 -08:00
|
|
|
if use_pdf_meta:
|
2016-06-05 09:42:18 -07:00
|
|
|
pdf = PdfFileReader(open(tmp_file_path, 'rb'))
|
|
|
|
doc_info = pdf.getDocumentInfo()
|
|
|
|
else:
|
|
|
|
doc_info = None
|
|
|
|
|
2016-12-23 00:53:39 -08:00
|
|
|
if doc_info is not None:
|
2017-04-22 23:22:10 -07:00
|
|
|
author = doc_info.author if doc_info.author else u"Unknown"
|
|
|
|
title = doc_info.title if doc_info.title else original_file_name
|
2016-06-05 08:41:47 -07:00
|
|
|
subject = doc_info.subject
|
|
|
|
else:
|
2017-02-04 05:28:18 -08:00
|
|
|
author = u"Unknown"
|
2016-06-05 08:41:47 -07:00
|
|
|
title = original_file_name
|
|
|
|
subject = ""
|
|
|
|
return uploader.BookMeta(
|
2016-12-23 00:53:39 -08:00
|
|
|
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="",
|
2017-03-02 03:59:35 -08:00
|
|
|
series_id="",
|
2017-03-02 06:57:02 -08:00
|
|
|
languages="")
|
2016-06-05 08:41:47 -07:00
|
|
|
|
2016-12-23 00:53:39 -08:00
|
|
|
|
2016-06-05 08:41:47 -07:00
|
|
|
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"
|
2017-02-15 09:09:17 -08:00
|
|
|
with Image(filename=tmp_file_path + "[0]", resolution=150) as img:
|
2016-06-05 08:41:47 -07:00
|
|
|
img.compression_quality = 88
|
|
|
|
img.save(filename=os.path.join(tmp_dir, cover_file_name))
|
|
|
|
return cover_file_name
|
2016-12-23 00:53:39 -08:00
|
|
|
|
|
|
|
def get_versions():
|
|
|
|
if not use_generic_pdf_cover:
|
|
|
|
IVersion=ImageVersion.MAGICK_VERSION
|
|
|
|
else:
|
2017-02-04 05:28:18 -08:00
|
|
|
IVersion=_(u'not installed')
|
2016-12-23 00:53:39 -08:00
|
|
|
if use_pdf_meta:
|
|
|
|
PVersion=PyPdfVersion
|
|
|
|
else:
|
2017-02-04 05:28:18 -08:00
|
|
|
PVersion=_(u'not installed')
|
2017-04-22 23:22:10 -07:00
|
|
|
return {'ImageVersion':IVersion,'PyPdfVersion':PVersion}
|