Start work on tool to upgrade EPUB 2 to 3 automatically

This commit is contained in:
Kovid Goyal 2018-04-13 09:27:48 +05:30
parent b88c6580d7
commit 39032f5b5e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 39 additions and 2 deletions

View File

@ -9,7 +9,8 @@ from lxml import etree
from calibre.ebooks.metadata.opf3 import (
DC, OPF, XPath, create_rating, create_series, create_timestamp,
encode_is_multiple, ensure_id, parse_date, read_prefixes, read_refines,
read_user_metadata2, refdef, remove_element, set_refines, set_user_metadata3
read_user_metadata2, refdef, remove_element, set_last_modified, set_refines,
set_user_metadata3
)
from calibre.ebooks.metadata.utils import parse_opf, pretty_print_opf
@ -97,7 +98,7 @@ def upgrade_timestamp(root, data):
except Exception:
pass
else:
create_timestamp(m, val)
create_timestamp(root, data.prefixes, m, val)
def upgrade_date(root, data):
@ -186,6 +187,7 @@ def upgrade_metadata(root):
upgrade_meta(root, data)
remove_invalid_attrs_in_dc_metadata(root, data)
set_last_modified(root, data.prefixes, data.refines)
pretty_print_opf(root)

View File

@ -0,0 +1,35 @@
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from calibre.ebooks.metadata.opf_2_to_3 import upgrade_metadata
def epub_2_to_3(container, report):
upgrade_metadata(container.opf)
container.dirty(container.opf_name)
container.opf.set('version', '3.0')
def upgrade_book(container, report):
if container.book_type != 'epub' or container.opf_version_parsed.major >= 3:
report(_('No upgrade needed'))
return False
epub_2_to_3(container, report)
report(_('Updated EPUB from version 2 to 3'))
return True
if __name__ == '__main__':
from calibre.ebooks.oeb.polish.container import get_container
from calibre.utils.logging import default_log
default_log.filter_level = default_log.DEBUG
inbook = sys.argv[-1]
ebook = get_container(inbook, default_log)
if upgrade_book(ebook, print):
outbook = inbook.rpartition('.')[0] + '-upgraded.' + inbook.rpartition('.')[-1]
ebook.commit(outbook)
print('Upgraded book written to:', outbook)