Commit 2760a781 authored by Ozzie Isaacs's avatar Ozzie Isaacs

Fix metadata recognition fb2 files

parent 8f5c649d
...@@ -30,50 +30,50 @@ def get_fb2_info(tmp_file_path, original_file_extension): ...@@ -30,50 +30,50 @@ def get_fb2_info(tmp_file_path, original_file_extension):
} }
fb2_file = open(tmp_file_path) fb2_file = open(tmp_file_path)
tree = etree.fromstring(fb2_file.read()) tree = etree.fromstring(fb2_file.read().encode())
authors = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:author', namespaces=ns) authors = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:author', namespaces=ns)
def get_author(element): def get_author(element):
last_name = element.xpath('fb:last-name/text()', namespaces=ns) last_name = element.xpath('fb:last-name/text()', namespaces=ns)
if len(last_name): if len(last_name):
last_name = last_name[0].encode('utf-8') last_name = last_name[0]
else: else:
last_name = u'' last_name = u''
middle_name = element.xpath('fb:middle-name/text()', namespaces=ns) middle_name = element.xpath('fb:middle-name/text()', namespaces=ns)
if len(middle_name): if len(middle_name):
middle_name = middle_name[0].encode('utf-8') middle_name = middle_name[0]
else: else:
middle_name = u'' middle_name = u''
first_name = element.xpath('fb:first-name/text()', namespaces=ns) first_name = element.xpath('fb:first-name/text()', namespaces=ns)
if len(first_name): if len(first_name):
first_name = first_name[0].encode('utf-8') first_name = first_name[0]
else: else:
first_name = u'' first_name = u''
return (first_name.decode('utf-8') + u' ' return (first_name + u' '
+ middle_name.decode('utf-8') + u' ' + middle_name + u' '
+ last_name.decode('utf-8')).encode('utf-8') + last_name)
author = str(", ".join(map(get_author, authors))) author = str(", ".join(map(get_author, authors)))
title = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:book-title/text()', namespaces=ns) title = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:book-title/text()', namespaces=ns)
if len(title): if len(title):
title = str(title[0].encode('utf-8')) title = str(title[0])
else: else:
title = u'' title = u''
description = tree.xpath('/fb:FictionBook/fb:description/fb:publish-info/fb:book-name/text()', namespaces=ns) description = tree.xpath('/fb:FictionBook/fb:description/fb:publish-info/fb:book-name/text()', namespaces=ns)
if len(description): if len(description):
description = str(description[0].encode('utf-8')) description = str(description[0])
else: else:
description = u'' description = u''
return BookMeta( return BookMeta(
file_path=tmp_file_path, file_path=tmp_file_path,
extension=original_file_extension, extension=original_file_extension,
title=title.decode('utf-8'), title=title,
author=author.decode('utf-8'), author=author,
cover=None, cover=None,
description=description.decode('utf-8'), description=description,
tags="", tags="",
series="", series="",
series_id="", series_id="",
......
...@@ -251,10 +251,11 @@ class WebServer(object): ...@@ -251,10 +251,11 @@ class WebServer(object):
finally: finally:
self.wsgiserver = None self.wsgiserver = None
# prevent irritating log of pending tasks message from asyncio
logger.get('asyncio').setLevel(logger.logging.CRITICAL)
if not self.restart: if not self.restart:
log.info("Performing shutdown of Calibre-Web") log.info("Performing shutdown of Calibre-Web")
# prevent irritating log of pending tasks message from asyncio
logger.get('asyncio').setLevel(logger.logging.CRITICAL)
return True return True
log.info("Performing restart of Calibre-Web") log.info("Performing restart of Calibre-Web")
......
...@@ -214,7 +214,7 @@ def parse_xmp(pdf_file): ...@@ -214,7 +214,7 @@ def parse_xmp(pdf_file):
if xmp_info: if xmp_info:
try: try:
xmp_author = xmp_info.dc_creator # list xmp_author = xmp_info.dc_creator # list
except: except AttributeError:
xmp_author = ['Unknown'] xmp_author = ['Unknown']
if xmp_info.dc_title: if xmp_info.dc_title:
...@@ -228,20 +228,22 @@ def parse_xmp(pdf_file): ...@@ -228,20 +228,22 @@ def parse_xmp(pdf_file):
xmp_description = '' xmp_description = ''
languages = [] languages = []
for i in xmp_info.dc_language: try:
#calibre-web currently only takes one language. for i in xmp_info.dc_language:
languages.append(isoLanguages.get_lang3(i)) languages.append(isoLanguages.get_lang3(i))
except AttributeError:
languages.append('')
xmp_tags = ', '.join(xmp_info.dc_subject) xmp_tags = ', '.join(xmp_info.dc_subject)
xmp_publisher = ', '.join(xmp_info.dc_publisher) xmp_publisher = ', '.join(xmp_info.dc_publisher)
xmp_languages = xmp_info.dc_language
return {'author': xmp_author, return {'author': xmp_author,
'title': xmp_title, 'title': xmp_title,
'subject': xmp_description, 'subject': xmp_description,
'tags': xmp_tags, 'languages': languages, 'tags': xmp_tags,
'publisher': xmp_publisher 'languages': languages,
} 'publisher': xmp_publisher
}
def pdf_meta(tmp_file_path, original_file_name, original_file_extension): def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
...@@ -250,8 +252,6 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension): ...@@ -250,8 +252,6 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
if use_pdf_meta: if use_pdf_meta:
with open(tmp_file_path, 'rb') as f: with open(tmp_file_path, 'rb') as f:
languages = [""]
publisher = ""
pdf_file = PdfFileReader(f) pdf_file = PdfFileReader(f)
doc_info = pdf_file.getDocumentInfo() doc_info = pdf_file.getDocumentInfo()
xmp_info = parse_xmp(pdf_file) xmp_info = parse_xmp(pdf_file)
...@@ -263,6 +263,13 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension): ...@@ -263,6 +263,13 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
tags = xmp_info['tags'] tags = xmp_info['tags']
languages = xmp_info['languages'] languages = xmp_info['languages']
publisher = xmp_info['publisher'] publisher = xmp_info['publisher']
else:
author = u'Unknown'
title = ''
languages = [""]
publisher = ""
subject = ""
tags = ""
if doc_info: if doc_info:
if author == '': if author == '':
...@@ -273,14 +280,8 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension): ...@@ -273,14 +280,8 @@ def pdf_meta(tmp_file_path, original_file_name, original_file_extension):
subject = doc_info.subject subject = doc_info.subject
if tags == '' and '/Keywords' in doc_info: if tags == '' and '/Keywords' in doc_info:
tags = doc_info['/Keywords'] tags = doc_info['/Keywords']
else: else:
author= u'Unknown'
title = original_file_name title = original_file_name
subject = ""
tags = ""
languages = [""]
publisher = ""
return BookMeta( return BookMeta(
file_path=tmp_file_path, file_path=tmp_file_path,
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment