mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Revised _update_epub_metadata
This commit is contained in:
parent
dd8960eeac
commit
a4ed02cf0d
@ -2647,69 +2647,61 @@ class ITUNES(DriverBase):
|
|||||||
|
|
||||||
# Refresh epub metadata
|
# Refresh epub metadata
|
||||||
with open(fpath,'r+b') as zfo:
|
with open(fpath,'r+b') as zfo:
|
||||||
# Touch the OPF timestamp
|
if False:
|
||||||
try:
|
try:
|
||||||
zf_opf = ZipFile(fpath,'r')
|
zf_opf = ZipFile(fpath,'r')
|
||||||
fnames = zf_opf.namelist()
|
fnames = zf_opf.namelist()
|
||||||
opf = [x for x in fnames if '.opf' in x][0]
|
opf = [x for x in fnames if '.opf' in x][0]
|
||||||
except:
|
except:
|
||||||
raise UserFeedback("'%s' is not a valid EPUB" % metadata.title,
|
raise UserFeedback("'%s' is not a valid EPUB" % metadata.title,
|
||||||
None,
|
None,
|
||||||
level=UserFeedback.WARN)
|
level=UserFeedback.WARN)
|
||||||
|
|
||||||
|
#Touch the OPF timestamp
|
||||||
|
opf_tree = etree.fromstring(zf_opf.read(opf))
|
||||||
|
md_els = opf_tree.xpath('.//*[local-name()="metadata"]')
|
||||||
|
if md_els:
|
||||||
|
ts = md_els[0].find('.//*[@name="calibre:timestamp"]')
|
||||||
|
if ts is not None:
|
||||||
|
timestamp = ts.get('content')
|
||||||
|
old_ts = parse_date(timestamp)
|
||||||
|
metadata.timestamp = datetime.datetime(old_ts.year, old_ts.month, old_ts.day, old_ts.hour,
|
||||||
|
old_ts.minute, old_ts.second, old_ts.microsecond+1, old_ts.tzinfo)
|
||||||
|
if DEBUG:
|
||||||
|
self.log.info(" existing timestamp: %s" % metadata.timestamp)
|
||||||
|
else:
|
||||||
|
metadata.timestamp = now()
|
||||||
|
if DEBUG:
|
||||||
|
self.log.info(" add timestamp: %s" % metadata.timestamp)
|
||||||
|
|
||||||
opf_tree = etree.fromstring(zf_opf.read(opf))
|
|
||||||
md_els = opf_tree.xpath('.//*[local-name()="metadata"]')
|
|
||||||
if md_els:
|
|
||||||
ts = md_els[0].find('.//*[@name="calibre:timestamp"]')
|
|
||||||
if ts is not None:
|
|
||||||
timestamp = ts.get('content')
|
|
||||||
old_ts = parse_date(timestamp)
|
|
||||||
metadata.timestamp = datetime.datetime(old_ts.year, old_ts.month, old_ts.day, old_ts.hour,
|
|
||||||
old_ts.minute, old_ts.second, old_ts.microsecond+1, old_ts.tzinfo)
|
|
||||||
if DEBUG:
|
|
||||||
self.log.info(" existing timestamp: %s" % metadata.timestamp)
|
|
||||||
else:
|
else:
|
||||||
metadata.timestamp = now()
|
metadata.timestamp = now()
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
|
self.log.warning(" missing <metadata> block in OPF file")
|
||||||
self.log.info(" add timestamp: %s" % metadata.timestamp)
|
self.log.info(" add timestamp: %s" % metadata.timestamp)
|
||||||
|
|
||||||
else:
|
zf_opf.close()
|
||||||
metadata.timestamp = now()
|
|
||||||
if DEBUG:
|
|
||||||
self.log.warning(" missing <metadata> block in OPF file")
|
|
||||||
self.log.info(" add timestamp: %s" % metadata.timestamp)
|
|
||||||
# Force the language declaration for iBooks 1.1
|
|
||||||
#metadata.language = get_lang().replace('_', '-')
|
|
||||||
|
|
||||||
# Updates from metadata plugboard (ignoring publisher)
|
|
||||||
metadata.language = metadata_x.language
|
|
||||||
|
|
||||||
if DEBUG:
|
|
||||||
if metadata.language != metadata_x.language:
|
|
||||||
self.log.info(" rewriting language: <dc:language>%s</dc:language>" % metadata.language)
|
|
||||||
|
|
||||||
zf_opf.close()
|
|
||||||
|
|
||||||
# If 'News' in tags, tweak the title/author for friendlier display in iBooks
|
# If 'News' in tags, tweak the title/author for friendlier display in iBooks
|
||||||
if _('News') in metadata.tags or \
|
if _('News') in metadata_x.tags or \
|
||||||
_('Catalog') in metadata.tags:
|
_('Catalog') in metadata_x.tags:
|
||||||
if metadata.title.find('[') > 0:
|
if metadata_x.title.find('[') > 0:
|
||||||
metadata.title = metadata.title[:metadata.title.find('[')-1]
|
metadata_x.title = metadata_x.title[:metadata_x.title.find('[')-1]
|
||||||
date_as_author = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y'))
|
date_as_author = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y'))
|
||||||
metadata.author = metadata.authors = [date_as_author]
|
metadata_x.author = metadata_x.authors = [date_as_author]
|
||||||
sort_author = re.sub('^\s*A\s+|^\s*The\s+|^\s*An\s+', '', metadata.title).rstrip()
|
sort_author = re.sub('^\s*A\s+|^\s*The\s+|^\s*An\s+', '', metadata_x.title).rstrip()
|
||||||
metadata.author_sort = '%s %s' % (sort_author, strftime('%Y-%m-%d'))
|
metadata_x.author_sort = '%s %s' % (sort_author, strftime('%Y-%m-%d'))
|
||||||
|
|
||||||
# Remove any non-alpha category tags
|
# Remove any non-alpha category tags
|
||||||
for tag in metadata.tags:
|
for tag in metadata_x.tags:
|
||||||
if not self._is_alpha(tag[0]):
|
if not self._is_alpha(tag[0]):
|
||||||
metadata.tags.remove(tag)
|
metadata_x.tags.remove(tag)
|
||||||
|
|
||||||
# If windows & series, nuke tags so series used as Category during _update_iTunes_metadata()
|
# If windows & series, nuke tags so series used as Category during _update_iTunes_metadata()
|
||||||
if iswindows and metadata.series:
|
if iswindows and metadata_x.series:
|
||||||
metadata.tags = None
|
metadata_x.tags = None
|
||||||
|
|
||||||
set_metadata(zfo, metadata, apply_null=True, update_timestamp=True)
|
set_metadata(zfo, metadata_x, apply_null=True, update_timestamp=True)
|
||||||
|
|
||||||
def _update_device(self, msg='', wait=True):
|
def _update_device(self, msg='', wait=True):
|
||||||
'''
|
'''
|
||||||
@ -2838,6 +2830,8 @@ class ITUNES(DriverBase):
|
|||||||
break
|
break
|
||||||
|
|
||||||
if db_added:
|
if db_added:
|
||||||
|
self.log.warning(" waiting for db_added to become writeable ")
|
||||||
|
time.sleep(1.0)
|
||||||
# If no title_sort plugboard tweak, create sort_name from series/index
|
# If no title_sort plugboard tweak, create sort_name from series/index
|
||||||
if metadata.title_sort == metadata_x.title_sort:
|
if metadata.title_sort == metadata_x.title_sort:
|
||||||
db_added.sort_name.set("%s %s" % (self.title_sorter(metadata_x.series), series_index))
|
db_added.sort_name.set("%s %s" % (self.title_sorter(metadata_x.series), series_index))
|
||||||
@ -2878,6 +2872,8 @@ class ITUNES(DriverBase):
|
|||||||
lb_added.SortName = metadata_x.title_sort
|
lb_added.SortName = metadata_x.title_sort
|
||||||
|
|
||||||
if db_added:
|
if db_added:
|
||||||
|
self.log.warning(" waiting for db_added to become writeable ")
|
||||||
|
time.sleep(1.0)
|
||||||
db_added.Name = metadata_x.title
|
db_added.Name = metadata_x.title
|
||||||
db_added.Album = metadata_x.title
|
db_added.Album = metadata_x.title
|
||||||
db_added.Artist = authors_to_string(metadata_x.authors)
|
db_added.Artist = authors_to_string(metadata_x.authors)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user