-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_writer.py
executable file
·131 lines (101 loc) · 4.84 KB
/
metadata_writer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from __future__ import (unicode_literals, division, absolute_import, print_function)
import re
from calibre.customize import MetadataWriterPlugin
__license__ = "GPL v3"
__copyright__ = "2016-2024, John Howell <[email protected]>"
ASIN_RE = r"^B[0-9A-Z]{9}$"
AUTO_PAGES = "(auto)" # fake name for automatic page number generation, instead of a lookup name
class KFXMetadataWriter(MetadataWriterPlugin):
name = "Set KFX metadata (from KFX Output)"
file_types = {"kfx"} # accept only monolithic KFX since that will be the format produced
description = "Set metadata in KFX files"
author = "jhowell"
def set_metadata(self, stream, mi, type_):
from calibre_plugins.kfx_output.kfxlib import (set_logger, YJ_Book, YJ_Metadata)
from calibre.ebooks import normalize as normalize_unicode
from calibre.ebooks.metadata import author_to_author_sort
from calibre.utils.config_base import tweaks
from calibre.utils.date import (is_date_undefined, isoformat)
from calibre.utils.logging import Log
from calibre.utils.localization import (canonicalize_lang, lang_as_iso639_1)
def mapped_author_to_author_sort(author):
if hasattr(mi, "author_sort_map"):
author_sort = mi.author_sort_map.get(author) # use mapping if provided
if author_sort:
return author_sort
return author_to_author_sort(author)
def normalize(s):
if not isinstance(s, type("")):
s = s.decode("utf8", "ignore")
return normalize_unicode(s)
log = set_logger(Log())
filename = stream.name if hasattr(stream, "name") else "stream"
log.info("KFX metadata writer activated for %s" % filename)
try:
from calibre.ebooks.conversion.config import load_defaults
prefs = load_defaults('kfx_output')
except Exception:
prefs = {}
log.info("Failed to read default KFX Output preferences")
md = YJ_Metadata(author_sort_fn=mapped_author_to_author_sort)
md.title = normalize(mi.title)
md.authors = [normalize(author) for author in mi.authors]
if mi.publisher:
md.publisher = normalize(mi.publisher)
if mi.pubdate and not is_date_undefined(mi.pubdate):
md.issue_date = str(isoformat(mi.pubdate)[:10])
if mi.comments:
# Strip user annotations
a_offset = mi.comments.find('<div class="user_annotations">')
ad_offset = mi.comments.find('<hr class="annotations_divider" />')
if a_offset >= 0:
mi.comments = mi.comments[:a_offset]
if ad_offset >= 0:
mi.comments = mi.comments[:ad_offset]
md.description = normalize(mi.comments)
if not mi.is_null('language'):
lang = canonicalize_lang(mi.language)
lang = lang_as_iso639_1(lang) or lang
if lang:
md.language = normalize(lang)
if mi.cover_data[1]:
md.cover_image_data = mi.cover_data
elif mi.cover:
md.cover_image_data = ("jpg", open(mi.cover, 'rb').read())
if prefs.get("cde_type_pdoc", False):
md.cde_content_type = "PDOC"
md.asin = True
elif not tweaks.get("kfx_output_ignore_asin_metadata", False):
value = mi.identifiers.get("mobi-asin")
if value is not None and re.match(ASIN_RE, value):
md.asin = value
else:
for ident, value in mi.identifiers.items():
if ident.startswith("amazon") and re.match(ASIN_RE, value):
md.asin = value
break
else:
value = mi.identifiers.get("asin")
if value is not None and re.match(ASIN_RE, value):
md.asin = value
if md.asin:
md.cde_content_type = "EBOK"
if prefs.get("approximate_pages", False):
page_count = 0
number_of_pages_field = prefs.get("number_of_pages_field", AUTO_PAGES)
if number_of_pages_field and number_of_pages_field != AUTO_PAGES:
number_of_pages = mi.get(number_of_pages_field, "")
try:
page_count = int(number_of_pages)
except Exception:
pass
else:
page_count = -1
book = YJ_Book(stream, log)
book.decode_book(set_metadata=md, set_approximate_pages=page_count)
new_data = book.convert_to_single_kfx()
set_logger()
stream.seek(0)
stream.truncate()
stream.write(new_data)
stream.seek(0)