Apple driver: Set the year field in iTunes based on the published date in calibre. Fixes #909050 (Books sent to iTunes lose publication date)

This commit is contained in:
Kovid Goyal 2012-01-05 19:42:42 +05:30
commit 3cde73301d

View File

@ -6,6 +6,7 @@ __docformat__ = 'restructuredtext en'
import cStringIO, ctypes, datetime, os, re, shutil, sys, tempfile, time import cStringIO, ctypes, datetime, os, re, shutil, sys, tempfile, time
from calibre.constants import __appname__, __version__, DEBUG from calibre.constants import __appname__, __version__, DEBUG
from calibre import fit_image, confirm_config_name from calibre import fit_image, confirm_config_name
from calibre.constants import isosx, iswindows from calibre.constants import isosx, iswindows
@ -806,6 +807,7 @@ class ITUNES(DriverBase):
''' '''
if DEBUG: if DEBUG:
self.log.info("ITUNES.get_file(): exporting '%s'" % path) self.log.info("ITUNES.get_file(): exporting '%s'" % path)
outfile.write(open(self.cached_books[path]['lib_book'].location().path).read()) outfile.write(open(self.cached_books[path]['lib_book'].location().path).read())
def open(self, connected_device, library_uuid): def open(self, connected_device, library_uuid):
@ -1945,7 +1947,7 @@ class ITUNES(DriverBase):
return thumb_data return thumb_data
if DEBUG: if DEBUG:
self.log.info(" ITUNES._generate_thumbnail():") self.log.info(" ITUNES._generate_thumbnail('%s'):" % title)
if isosx: if isosx:
# Fetch the artwork from iTunes # Fetch the artwork from iTunes
@ -2759,9 +2761,14 @@ class ITUNES(DriverBase):
STRIP_TAGS = re.compile(r'<[^<]*?/?>') STRIP_TAGS = re.compile(r'<[^<]*?/?>')
# Confirm writable metadata if directly connected to device
if db_added:
self._wait_for_writable_metadata(db_added)
# Update metadata from plugboard # Update metadata from plugboard
# If self.plugboard is None (no transforms), original metadata is returned intact # If self.plugboard is None (no transforms), original metadata is returned intact
metadata_x = self._xform_metadata_via_plugboard(metadata, this_book.format) metadata_x = self._xform_metadata_via_plugboard(metadata, this_book.format)
if isosx: if isosx:
if lb_added: if lb_added:
lb_added.name.set(metadata_x.title) lb_added.name.set(metadata_x.title)
@ -2772,10 +2779,9 @@ class ITUNES(DriverBase):
lb_added.enabled.set(True) lb_added.enabled.set(True)
lb_added.sort_artist.set(icu_title(metadata_x.author_sort)) lb_added.sort_artist.set(icu_title(metadata_x.author_sort))
lb_added.sort_name.set(metadata_x.title_sort) lb_added.sort_name.set(metadata_x.title_sort)
lb_added.year.set(metadata_x.pubdate.year)
if db_added: if db_added:
self.log.warning(" waiting for db_added to become writeable ")
time.sleep(1.0)
db_added.name.set(metadata_x.title) db_added.name.set(metadata_x.title)
db_added.album.set(metadata_x.title) db_added.album.set(metadata_x.title)
db_added.artist.set(authors_to_string(metadata_x.authors)) db_added.artist.set(authors_to_string(metadata_x.authors))
@ -2784,6 +2790,7 @@ class ITUNES(DriverBase):
db_added.enabled.set(True) db_added.enabled.set(True)
db_added.sort_artist.set(icu_title(metadata_x.author_sort)) db_added.sort_artist.set(icu_title(metadata_x.author_sort))
db_added.sort_name.set(metadata_x.title_sort) db_added.sort_name.set(metadata_x.title_sort)
db_added.year.set(metadata_x.pubdate.year)
if metadata_x.comments: if metadata_x.comments:
if lb_added: if lb_added:
@ -2871,6 +2878,7 @@ class ITUNES(DriverBase):
lb_added.Enabled = True lb_added.Enabled = True
lb_added.SortArtist = icu_title(metadata_x.author_sort) lb_added.SortArtist = icu_title(metadata_x.author_sort)
lb_added.SortName = metadata_x.title_sort lb_added.SortName = metadata_x.title_sort
lb_added.Year = metadata_x.pubdate.year
if db_added: if db_added:
self.log.warning(" waiting for db_added to become writeable ") self.log.warning(" waiting for db_added to become writeable ")
@ -2883,6 +2891,7 @@ class ITUNES(DriverBase):
db_added.Enabled = True db_added.Enabled = True
db_added.SortArtist = icu_title(metadata_x.author_sort) db_added.SortArtist = icu_title(metadata_x.author_sort)
db_added.SortName = metadata_x.title_sort db_added.SortName = metadata_x.title_sort
db_added.Year = metadata_x.pubdate.year
if metadata_x.comments: if metadata_x.comments:
if lb_added: if lb_added:
@ -2981,6 +2990,31 @@ class ITUNES(DriverBase):
db_added.Genre = tag db_added.Genre = tag
break break
def _wait_for_writable_metadata(self, db_added, delay=0.5):
'''
Ensure device metadata is writable
'''
if DEBUG:
self.log.info(" ITUNES._wait_for_writable_metadata()")
attempts = 9
while attempts:
try:
if isosx:
db_added.bpm.set(0)
elif iswindows:
db_added.BPM = 0
break
except:
attempts -= 1
time.sleep(delay)
if DEBUG:
self.log.warning(" waiting for iDevice metadata to become writable, attempt #%d" %
(10 - attempts))
else:
if DEBUG:
self.log.error(" failed to write device metadata")
def _xform_metadata_via_plugboard(self, book, format): def _xform_metadata_via_plugboard(self, book, format):
''' Transform book metadata from plugboard templates ''' ''' Transform book metadata from plugboard templates '''
if DEBUG: if DEBUG:
@ -3090,6 +3124,7 @@ class ITUNES_ASYNC(ITUNES):
for (i,book) in enumerate(library_books): for (i,book) in enumerate(library_books):
format = 'pdf' if library_books[book].kind().startswith('PDF') else 'epub' format = 'pdf' if library_books[book].kind().startswith('PDF') else 'epub'
this_book = Book(library_books[book].name(), library_books[book].artist()) this_book = Book(library_books[book].name(), library_books[book].artist())
#this_book.path = library_books[book].location().path
this_book.path = self.path_template % (library_books[book].name(), this_book.path = self.path_template % (library_books[book].name(),
library_books[book].artist(), library_books[book].artist(),
format) format)