Upload pdf fixes:

Handle no title
Handle no author
Fix import of more than one language
Add missing pdf upload publisher handling
This commit is contained in:
Ozzie Isaacs
2021-03-17 19:06:51 +01:00
parent 2be7b6480a
commit fcf9e7a1ef
9 changed files with 148 additions and 39 deletions
+116 -16
View File
@@ -44,12 +44,17 @@ except (ImportError, RuntimeError) as e:
use_generic_pdf_cover = True
try:
from PyPDF2 import PdfFileReader
from PyPDF2 import __version__ as PyPdfVersion
from PyPDF3 import PdfFileReader
from PyPDF3 import __version__ as PyPdfVersion
use_pdf_meta = True
except ImportError as e:
log.debug('Cannot import PyPDF2, extracting pdf metadata will not work: %s', e)
use_pdf_meta = False
except ImportError as ex:
try:
from PyPDF2 import PdfFileReader
from PyPDF2 import __version__ as PyPdfVersion
use_pdf_meta = True
except ImportError as e:
log.debug('Cannot import PyPDF3/PyPDF2, extracting pdf metadata will not work: %s / %s', e)
use_pdf_meta = False
try:
from . import epub
@@ -102,7 +107,98 @@ def default_meta(tmp_file_path, original_file_name, original_file_extension):
tags="",
series="",
series_id="",
languages="")
languages="",
publisher="")
def parse_xmp(pdf_file):
"""
Parse XMP Metadata and prepare for BookMeta object
"""
try:
xmp_info = pdf_file.getXmpMetadata()
except Exception as e:
log.debug('Can not read XMP metadata', e)
return None
if xmp_info:
try:
xmp_author = xmp_info.dc_creator # list
except AttributeError:
xmp_author = ['']
if xmp_info.dc_title:
xmp_title = xmp_info.dc_title['x-default']
else:
xmp_title = ''
if xmp_info.dc_description:
xmp_description = xmp_info.dc_description['x-default']
else:
xmp_description = ''
languages = []
try:
for i in xmp_info.dc_language:
#calibre-web currently only takes one language.
languages.append(isoLanguages.get_lang3(i))
except:
languages.append('')
xmp_tags = ', '.join(xmp_info.dc_subject)
xmp_publisher = ', '.join(xmp_info.dc_publisher)
return {'author': xmp_author,
'title': xmp_title,
'subject': xmp_description,
'tags': xmp_tags, 'languages': languages,
'publisher': xmp_publisher
}
def parse_xmp(pdf_file):
"""
Parse XMP Metadata and prepare for BookMeta object
"""
try:
xmp_info = pdf_file.getXmpMetadata()
except Exception as e:
log.debug('Can not read XMP metadata', e)
return None
if xmp_info:
try:
xmp_author = xmp_info.dc_creator # list
except:
xmp_author = ['']
if xmp_info.dc_title:
xmp_title = xmp_info.dc_title['x-default']
else:
xmp_title = ''
if xmp_info.dc_description:
xmp_description = xmp_info.dc_description['x-default']
else:
xmp_description = ''
languages = []
try:
for i in xmp_info.dc_language:
languages.append(isoLanguages.get_lang3(i))
except AttributeError:
languages= [""]
xmp_tags = ', '.join(xmp_info.dc_subject)
xmp_publisher = ', '.join(xmp_info.dc_publisher)
return {'author': xmp_author,
'title': xmp_title,
'subject': xmp_description,
'tags': xmp_tags,
'languages': languages,
'publisher': xmp_publisher
}
def parse_xmp(pdf_file):
@@ -154,6 +250,8 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
if use_pdf_meta:
with open(tmp_file_path, 'rb') as f:
languages = [""]
publisher = ""
pdf_file = PdfFileReader(f)
doc_info = pdf_file.getDocumentInfo()
xmp_info = parse_xmp(pdf_file)
@@ -166,20 +264,22 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
languages = xmp_info['languages']
publisher = xmp_info['publisher']
elif doc_info:
author = ' & '.join(split_authors([doc_info.author]))
title = doc_info.title
subject = doc_info.subject
tags = doc_info['/Keywords']
languages = ""
publisher = ""
if doc_info:
if author == '':
author = ' & '.join(split_authors([doc_info.author])) if doc_info.author else u'Unknown'
if title == '':
title = doc_info.title if doc_info.title else original_file_name
if subject == '':
subject = doc_info.subject
if tags == '' and '/Keywords' in doc_info:
tags = doc_info['/Keywords']
else:
author= u'Unknown'
title = original_file_name
subject = ""
tags = ""
languages = ""
languages = [""]
publisher = ""
return BookMeta(
@@ -192,8 +292,8 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
tags=tags,
series="",
series_id="",
languages=', '.join(languages)
)
languages=','.join(languages),
publisher=publisher)
def pdf_preview(tmp_file_path, tmp_dir):