diff --git a/icons/library.icns b/icons/library.icns index 39813eb8e6..1b796e2fe0 100644 Binary files a/icons/library.icns and b/icons/library.icns differ diff --git a/icons/library.ico b/icons/library.ico index 433b4f2d51..32ce8b5d0d 100644 Binary files a/icons/library.ico and b/icons/library.ico differ diff --git a/resources/images/library.png b/resources/images/library.png index bd3b90bfb1..cd2c9075b6 100644 Binary files a/resources/images/library.png and b/resources/images/library.png differ diff --git a/resources/tracer.epub b/resources/tracer.epub new file mode 100644 index 0000000000..28f40c07d0 Binary files /dev/null and b/resources/tracer.epub differ diff --git a/src/calibre/devices/apple/driver.py b/src/calibre/devices/apple/driver.py index ae440a359e..7a90343c29 100644 --- a/src/calibre/devices/apple/driver.py +++ b/src/calibre/devices/apple/driver.py @@ -5,19 +5,21 @@ __copyright__ = '2010, Gregory Riker' __docformat__ = 'restructuredtext en' -import cStringIO, ctypes, os, re, shutil, subprocess, sys, tempfile, time, zipfile +import cStringIO, ctypes, datetime, os, re, shutil, subprocess, sys, tempfile, time from calibre.constants import DEBUG from calibre import fit_image from calibre.constants import isosx, iswindows +from calibre.devices.errors import UserFeedback from calibre.devices.interface import DevicePlugin from calibre.ebooks.BeautifulSoup import BeautifulSoup from calibre.ebooks.metadata import MetaInformation +from calibre.ebooks.metadata.epub import set_metadata from calibre.library.server.utils import strftime from calibre.utils.config import Config, config_dir -from calibre.utils.date import parse_date +from calibre.utils.date import isoformat, now, parse_date from calibre.utils.logging import Log -from calibre.devices.errors import UserFeedback +from calibre.utils.zipfile import ZipFile from PIL import Image as PILImage @@ -32,6 +34,7 @@ if isosx: if iswindows: import pythoncom, win32com.client + class ITUNES(DevicePlugin): ''' Calling sequences: @@ -175,54 +178,67 @@ class ITUNES(DevicePlugin): # Delete any obsolete copies of the book from the booklist if self.update_list: - if isosx: - if DEBUG: - self.log.info( "ITUNES.add_books_to_metadata()") - self._dump_update_list('add_books_to_metadata()') - for (j,p_book) in enumerate(self.update_list): - self.log.info("ITUNES.add_books_to_metadata():\n looking for %s" % - str(p_book['lib_book'])[-9:]) - for i,bl_book in enumerate(booklists[0]): - if bl_book.library_id == p_book['lib_book']: - booklists[0].pop(i) - self.log.info("ITUNES.add_books_to_metadata():\n removing %s %s" % - (p_book['title'], str(p_book['lib_book'])[-9:])) + if True: + self.log.info("ITUNES.add_books_to_metadata()") + #self._dump_booklist(booklists[0], header='before',indent=2) + #self._dump_update_list(header='before',indent=2) + #self._dump_cached_books(header='before',indent=2) + + for (j,p_book) in enumerate(self.update_list): + if False: + if isosx: + self.log.info(" looking for %s" % + str(p_book['lib_book'])[-9:]) + elif iswindows: + self.log.info(" looking for '%s' by %s (%s)" % + (p_book['title'],p_book['author'], p_book['uuid'])) + + # Purge the booklist, self.cached_books + for i,bl_book in enumerate(booklists[0]): + if bl_book.uuid == p_book['uuid']: + # Remove from booklists[0] + booklists[0].pop(i) + if False: + if isosx: + self.log.info(" removing old %s %s from booklists[0]" % + (p_book['title'], str(p_book['lib_book'])[-9:])) + elif iswindows: + self.log.info(" removing old '%s' from booklists[0]" % + (p_book['title'])) + + # If >1 matching uuid, remove old title + matching_uuids = 0 + for cb in self.cached_books: + if self.cached_books[cb]['uuid'] == p_book['uuid']: + matching_uuids += 1 + + if matching_uuids > 1: + for cb in self.cached_books: + if self.cached_books[cb]['uuid'] == p_book['uuid']: + if self.cached_books[cb]['title'] == p_book['title'] and \ + self.cached_books[cb]['author'] == p_book['author']: + if DEBUG: + self._dump_cached_book(self.cached_books[cb],header="removing from self.cached_books:", indent=2) + self.cached_books.pop(cb) + break break - else: - self.log.error(" update_list item '%s' by %s %s not found in booklists[0]" % - (p_book['title'], p_book['author'],str(p_book['lib_book'])[-9:])) - - if self.report_progress is not None: - self.report_progress(j+1/task_count, _('Updating device metadata listing...')) - - elif iswindows: - if DEBUG: - self.log.info("ITUNES.add_books_to_metadata()") - for (j,p_book) in enumerate(self.update_list): - #self.log.info(" looking for '%s' by %s" % (p_book['title'],p_book['author'])) - for i,bl_book in enumerate(booklists[0]): - #self.log.info(" evaluating '%s' by %s" % (bl_book.title,bl_book.author[0])) - if bl_book.title == p_book['title'] and \ - bl_book.author[0] == p_book['author']: - booklists[0].pop(i) - self.log.info(" removing outdated version of '%s'" % p_book['title']) - break - else: - self.log.error(" update_list item '%s' not found in booklists[0]" % p_book['title']) - - if self.report_progress is not None: - self.report_progress(j+1/task_count, _('Updating device metadata listing...')) + if self.report_progress is not None: + self.report_progress(j+1/task_count, _('Updating device metadata listing...')) if self.report_progress is not None: self.report_progress(1.0, _('Updating device metadata listing...')) # Add new books to booklists[0] for new_book in locations[0]: - if DEBUG: - self.log.info(" adding '%s' by '%s' to booklists[0]" % + if False: + self.log.info(" adding '%s' by '%s' to booklists[0]" % (new_book.title, new_book.author)) booklists[0].append(new_book) + if False: + self._dump_booklist(booklists[0],header='after',indent=2) + self._dump_cached_books(header='after',indent=2) + def books(self, oncard=None, end_session=True): """ Return a list of ebooks on the device. @@ -264,6 +280,7 @@ class ITUNES(DevicePlugin): this_book.device_collections = [] this_book.library_id = library_books[this_book.path] if this_book.path in library_books else None this_book.size = book.size() + this_book.uuid = book.album() # Hack to discover if we're running in GUI environment if self.report_progress is not None: this_book.thumbnail = self._generate_thumbnail(this_book.path, book) @@ -275,7 +292,8 @@ class ITUNES(DevicePlugin): 'title':book.name(), 'author':[book.artist()], 'lib_book':library_books[this_book.path] if this_book.path in library_books else None, - 'dev_book':book + 'dev_book':book, + 'uuid': book.album() } if self.report_progress is not None: @@ -310,7 +328,8 @@ class ITUNES(DevicePlugin): cached_books[this_book.path] = { 'title':book.Name, 'author':book.Artist, - 'lib_book':library_books[this_book.path] if this_book.path in library_books else None + 'lib_book':library_books[this_book.path] if this_book.path in library_books else None, + 'uuid': book.Album } if self.report_progress is not None: @@ -391,7 +410,7 @@ class ITUNES(DevicePlugin): self.ejected = True return False - self._discover_manual_sync_mode() + self._discover_manual_sync_mode(wait = 2 if self.initial_status == 'launched' else 0) return True def can_handle_windows(self, device_id, debug=False): @@ -525,8 +544,24 @@ class ITUNES(DevicePlugin): else: self.log.info(" skipping sync phase, manual_sync_mode: True") else: - self.problem_titles.append("'%s' by %s" % - (self.cached_books[path]['title'],self.cached_books[path]['author'])) + if self.manual_sync_mode: + metadata = MetaInformation(self.cached_books[path]['title'], + [self.cached_books[path]['author']]) + metadata.uuid = self.cached_books[path]['uuid'] + + if isosx: + self._remove_existing_copy(self.cached_books[path],metadata) + elif iswindows: + try: + pythoncom.CoInitialize() + self.iTunes = win32com.client.Dispatch("iTunes.Application") + self._remove_existing_copy(self.cached_books[path],metadata) + finally: + pythoncom.CoUninitialize() + + else: + self.problem_titles.append("'%s' by %s" % + (self.cached_books[path]['title'],self.cached_books[path]['author'])) def eject(self): ''' @@ -622,6 +657,8 @@ class ITUNES(DevicePlugin): Note that most of the initialization is necessarily performed in can_handle(), as we need to talk to iTunes to discover if there's a connected iPod ''' + if DEBUG: + self.log.info("ITUNES.open()") # Confirm/create thumbs archive archive_path = os.path.join(self.cache_dir, "thumbs.zip") @@ -633,7 +670,7 @@ class ITUNES(DevicePlugin): if not os.path.exists(archive_path): self.log.info(" creating zip archive") - zfw = zipfile.ZipFile(archive_path, mode='w') + zfw = ZipFile(archive_path, mode='w') zfw.writestr("iTunes Thumbs Archive",'') zfw.close() else: @@ -652,24 +689,19 @@ class ITUNES(DevicePlugin): if DEBUG: self.log.info("ITUNES.remove_books_from_metadata()") for path in paths: - self._dump_cached_book(self.cached_books[path]) - if self.cached_books[path]['lib_book']: - # Remove from the booklist - for i,book in enumerate(booklists[0]): - if book.path == path: - self.log.info(" removing '%s' from calibre booklist, index: %d" % (path, i)) - booklists[0].pop(i) - break - else: - self.log.error(" '%s' not found in self.cached_book" % path) + self._dump_cached_book(self.cached_books[path], indent=2) - # Remove from cached_books - self.cached_books.pop(path) - if DEBUG: - self.log.info(" removing '%s' from self.cached_books" % path) -# self._dump_cached_books('remove_books_from_metadata()') - else: - self.log.warning(" skipping purchased book, can't remove via automation interface") + # Purge the booklist, self.cached_books + for i,bl_book in enumerate(booklists[0]): + if bl_book.uuid == self.cached_books[path]['uuid']: + # Remove from booklists[0] + booklists[0].pop(i) + + for cb in self.cached_books: + if self.cached_books[cb]['uuid'] == self.cached_books[path]['uuid']: + self.cached_books.pop(cb) + break + break def reset(self, key='-1', log_packets=False, report_progress=None, detected_device=None) : @@ -712,13 +744,10 @@ class ITUNES(DevicePlugin): (L{books}(oncard=None), L{books}(oncard='carda'), L{books}(oncard='cardb')). ''' - if DEBUG: - self.log.info("ITUNES:sync_booklists()") if self.update_needed: if DEBUG: self.log.info(' calling _update_device') self._update_device(msg=self.update_msg, wait=False) - self.update_list = [] self.update_needed = False # Inform user of any problem books @@ -727,6 +756,7 @@ class ITUNES(DevicePlugin): details='\n'.join(self.problem_titles), level=UserFeedback.WARN) self.problem_titles = [] self.problem_msg = None + self.update_list = [] def total_space(self, end_session=True): """ @@ -776,30 +806,29 @@ class ITUNES(DevicePlugin): self.problem_msg = _("Some cover art could not be converted.\n" "Click 'Show Details' for a list.") - if DEBUG: + if False: self.log.info("ITUNES.upload_books()") - self._dump_files(files, header='upload_books()') -# self._dump_cached_books('upload_books()') - self._dump_update_list('upload_books()') + self._dump_files(files, header='upload_books()',indent=2) + self._dump_update_list(header='upload_books()',indent=2) if isosx: for (i,file) in enumerate(files): path = self.path_template % (metadata[i].title, metadata[i].author[0]) - self._remove_existing_copies(path,file,metadata[i]) - fpath = self._get_fpath(file) + self._remove_existing_copy(path, metadata[i]) + fpath = self._get_fpath(file, metadata[i], update_md=True) db_added, lb_added = self._add_new_copy(fpath, metadata[i]) - thumb = self._cover_to_thumb(path, metadata[i], lb_added, db_added) + thumb = self._cover_to_thumb(path, metadata[i], db_added, lb_added) this_book = self._create_new_book(fpath, metadata[i], path, db_added, lb_added, thumb) new_booklist.append(this_book) self._update_iTunes_metadata(metadata[i], db_added, lb_added, this_book) # Add new_book to self.cached_paths self.cached_books[this_book.path] = { - 'title': metadata[i].title, - 'author': metadata[i].author[0], + 'title': metadata[i].title, + 'author': metadata[i].author, 'lib_book': lb_added, - 'dev_book': db_added } - self._dump_cached_books(header="after upload_books()") + 'dev_book': db_added, + 'uuid': metadata[i].uuid} # Report progress if self.report_progress is not None: @@ -812,9 +841,16 @@ class ITUNES(DevicePlugin): for (i,file) in enumerate(files): path = self.path_template % (metadata[i].title, metadata[i].author[0]) - self._remove_existing_copies(path,file,metadata[i]) - fpath = self._get_fpath(file) + self._remove_existing_copy(path, metadata[i]) + fpath = self._get_fpath(file, metadata[i], update_md=True) db_added, lb_added = self._add_new_copy(fpath, metadata[i]) + + if self.manual_sync_mode and not db_added: + # Problem finding added book, probably title/author change needing to be written to metadata + self.problem_msg = ("Title and/or author metadata mismatch with uploaded books.\n" + "Click 'Show Details...' for affected books.") + self.problem_titles.append("'%s' by %s" % (metadata[i].title, metadata[i].author[0])) + thumb = self._cover_to_thumb(path, metadata[i], lb_added, db_added) this_book = self._create_new_book(fpath, metadata[i], path, db_added, lb_added, thumb) new_booklist.append(this_book) @@ -822,10 +858,11 @@ class ITUNES(DevicePlugin): # Add new_book to self.cached_paths self.cached_books[this_book.path] = { - 'title': metadata[i].title, - 'author': metadata[i].author[0], + 'title': metadata[i].title, + 'author': metadata[i].author[0], 'lib_book': lb_added, - 'dev_book': db_added } + 'dev_book': db_added, + 'uuid': metadata[i].uuid} # Report progress if self.report_progress is not None: @@ -841,11 +878,16 @@ class ITUNES(DevicePlugin): self.update_needed = True self.update_msg = "Added books to device" + if False: + self._dump_booklist(new_booklist,header="after upload_books()",indent=2) + self._dump_cached_books(header="after upload_books()",indent=2) return (new_booklist, [], []) + # Private methods def _add_device_book(self,fpath, metadata): ''' + assumes pythoncom wrapper for windows ''' self.log.info(" ITUNES._add_device_book()") if isosx: @@ -857,79 +899,72 @@ class ITUNES(DevicePlugin): break else: if DEBUG: - self.log.error(" Device|Books playlist not found") + self.log.error(" Device|Books playlist not found") # Add the passed book to the Device|Books playlist added = pl.add(appscript.mactypes.File(fpath),to=pl) - if DEBUG: - self.log.info(" adding '%s' to device" % fpath) + if False: + self.log.info(" '%s' added to Device|Books" % metadata.title) return added elif iswindows: if 'iPod' in self.sources: - try: - pythoncom.CoInitialize() - connected_device = self.sources['iPod'] - device = self.iTunes.sources.ItemByName(connected_device) + connected_device = self.sources['iPod'] + device = self.iTunes.sources.ItemByName(connected_device) - added = None - for pl in device.Playlists: - if pl.Kind == self.PlaylistKind.index('User') and \ - pl.SpecialKind == self.PlaylistSpecialKind.index('Books'): - break - else: - if DEBUG: - self.log.info(" no Books playlist found") + db_added = None + for pl in device.Playlists: + if pl.Kind == self.PlaylistKind.index('User') and \ + pl.SpecialKind == self.PlaylistSpecialKind.index('Books'): + break + else: + if DEBUG: + self.log.info(" no Books playlist found") - # Add the passed book to the Device|Books playlist - if pl: - ''' - added = pl.AddFile(fpath) - if DEBUG: - self.log.info(" adding '%s' to device" % fpath) - ''' - file_s = ctypes.c_char_p(fpath) - FileArray = ctypes.c_char_p * 1 - fa = FileArray(file_s) - op_status = pl.AddFiles(fa) + # Add the passed book to the Device|Books playlist + if pl: + file_s = ctypes.c_char_p(fpath) + FileArray = ctypes.c_char_p * 1 + fa = FileArray(file_s) + op_status = pl.AddFiles(fa) + if DEBUG: + sys.stdout.write(" uploading '%s' to Device|Books ..." % metadata.title) + sys.stdout.flush() + + while op_status.InProgress: + time.sleep(0.5) if DEBUG: - sys.stdout.write(" uploading '%s' to device ..." % metadata.title) + sys.stdout.write('.') sys.stdout.flush() + if DEBUG: + sys.stdout.write("\n") + sys.stdout.flush() - while op_status.InProgress: + # This doesn't seem to work with Device, just Library + if False: + if DEBUG: + sys.stdout.write(" waiting for handle to added '%s' ..." % metadata.title) + sys.stdout.flush() + while not op_status.Tracks: time.sleep(0.5) if DEBUG: sys.stdout.write('.') sys.stdout.flush() + if DEBUG: - sys.stdout.write("\n") - sys.stdout.flush() + print + added = op_status.Tracks[0] + else: + # This approach simply scans Library|Books for the book we just added - # This doesn't seem to work with device, just Library - if False: - if DEBUG: - sys.stdout.write(" waiting for handle to added '%s' ..." % metadata.title) - sys.stdout.flush() - while op_status.Tracks is None: - time.sleep(0.5) - if DEBUG: - sys.stdout.write('.') - sys.stdout.flush() - if DEBUG: - print - added = op_status.Tracks[0] - else: - # This approach simply scans Library|Books for the book we just added - added = self._find_device_book( - {'title': metadata.title, - 'author': metadata.author[0]}) - return added + # Try the calibre metadata first + db_added = self._find_device_book( + {'title': metadata.title, + 'author': metadata.authors[0], + 'uuid': metadata.uuid}) - finally: - pythoncom.CoUninitialize() - - return added + return db_added def _add_library_book(self,file, metadata): ''' @@ -963,7 +998,7 @@ class ITUNES(DevicePlugin): sys.stdout.write("\n") sys.stdout.flush() - if True: + if False: if DEBUG: sys.stdout.write(" waiting for handle to added '%s' ..." % metadata.title) sys.stdout.flush() @@ -978,8 +1013,9 @@ class ITUNES(DevicePlugin): else: # This approach simply scans Library|Books for the book we just added added = self._find_library_book( - {'title': metadata.title, - 'author': metadata.author[0]}) + { 'title': metadata.title, + 'author': metadata.author[0], + 'uuid': metadata.uuid}) return added def _add_new_copy(self, fpath, metadata): @@ -993,12 +1029,11 @@ class ITUNES(DevicePlugin): if self.manual_sync_mode: db_added = self._add_device_book(fpath, metadata) - if DEBUG: - self.log.info(" file uploaded to Device|Books") if not getattr(fpath, 'deleted_after_upload', False): lb_added = self._add_library_book(fpath, metadata) - if DEBUG: - self.log.info(" file added to Library|Books for iTunes:iBooks tracking") + if lb_added: + if DEBUG: + self.log.info(" file added to Library|Books for iTunes<->iBooks tracking") else: lb_added = self._add_library_book(fpath, metadata) if DEBUG: @@ -1006,7 +1041,7 @@ class ITUNES(DevicePlugin): return db_added, lb_added - def _cover_to_thumb(self, path, metadata, lb_added, db_added): + def _cover_to_thumb(self, path, metadata, db_added, lb_added): ''' assumes pythoncom wrapper for db_added ''' @@ -1025,7 +1060,7 @@ class ITUNES(DevicePlugin): db_added.artworks[1].data_.set(cover_data.read()) except: if DEBUG: - self.log.warning(" iTunes automation interface generated an error" + self.log.warning(" iTunes automation interface reported an error" " when adding artwork to '%s'" % metadata.title) #import traceback #traceback.print_exc() @@ -1061,7 +1096,7 @@ class ITUNES(DevicePlugin): if DEBUG: self.log.info( " refreshing cached thumb for '%s'" % metadata.title) archive_path = os.path.join(self.cache_dir, "thumbs.zip") - zfw = zipfile.ZipFile(archive_path, mode='a') + zfw = ZipFile(archive_path, mode='a') thumb_path = path.rpartition('.')[0] + '.jpg' zfw.writestr(thumb_path, thumb) zfw.close() @@ -1085,6 +1120,7 @@ class ITUNES(DevicePlugin): this_book.path = path this_book.thumbnail = thumb this_book.iTunes_id = lb_added + this_book.uuid = metadata.uuid if isosx: if lb_added: @@ -1116,14 +1152,32 @@ class ITUNES(DevicePlugin): return this_book + def _delete_iTunesMetadata_plist(self,fpath): + ''' + Delete the plist file from the file to force recache + ''' + zf = ZipFile(fpath,'a') + fnames = zf.namelist() + pl_name = 'iTunesMetadata.plist' + try: + plist = [x for x in fnames if pl_name in x][0] + except: + plist = None + if plist: + if DEBUG: + self.log.info(" deleting %s from %s" % (pl_name,fpath)) + zf.delete(pl_name) + zf.close() + def _discover_manual_sync_mode(self, wait=0): ''' Assumes pythoncom for windows wait is passed when launching iTunes, as it seems to need a moment to come to its senses - ''' if DEBUG: self.log.info(" ITUNES._discover_manual_sync_mode()") + if wait: + time.sleep(wait) if isosx: connected_device = self.sources['iPod'] dev_books = None @@ -1133,22 +1187,29 @@ class ITUNES(DevicePlugin): dev_books = pl.file_tracks() break else: - self.log.error(" book_playlist not found") + self.log.error(" book_playlist not found") if len(dev_books): first_book = dev_books[0] - #if DEBUG: - #self.log.info(" determing manual mode by modifying '%s' by %s" % (first_book.name(), first_book.artist())) + if False: + self.log.info(" determing manual mode by modifying '%s' by %s" % (first_book.name(), first_book.artist())) try: first_book.bpm.set(0) self.manual_sync_mode = True except: self.manual_sync_mode = False - self.log.info(" iTunes.manual_sync_mode: %s" % self.manual_sync_mode) + else: + if DEBUG: + self.log.info(" adding tracer to empty Books|Playlist") + try: + added = pl.add(appscript.mactypes.File(P('tracer.epub')),to=pl) + time.sleep(0.5) + added.delete() + self.manual_sync_mode = True + except: + self.manual_sync_mode = False elif iswindows: - if wait: - time.sleep(wait) connected_device = self.sources['iPod'] device = self.iTunes.sources.ItemByName(connected_device) @@ -1168,76 +1229,137 @@ class ITUNES(DevicePlugin): self.manual_sync_mode = True except: self.manual_sync_mode = False - self.log.info(" iTunes.manual_sync_mode: %s" % self.manual_sync_mode) + else: + if DEBUG: + self.log.info(" sending tracer to empty Books|Playlist") + fpath = P('tracer.epub') + mi = MetaInformation('Tracer',['calibre']) + try: + added = self._add_device_book(fpath,mi) + time.sleep(0.5) + added.Delete() + self.manual_sync_mode = True + except: + self.manual_sync_mode = False - def _dump_booklist(self, booklist, header=None): + self.log.info(" iTunes.manual_sync_mode: %s" % self.manual_sync_mode) + + def _dump_booklist(self, booklist, header=None,indent=0): ''' ''' if header: - msg = '\nbooklist, %s' % header + msg = '\n%sbooklist %s:' % (' '*indent,header) self.log.info(msg) - self.log.info('%s' % ('-' * len(msg))) + self.log.info('%s%s' % (' '*indent,'-' * len(msg))) for book in booklist: if isosx: - self.log.info("%-40.40s %-30.30s %-10.10s" % - (book.title, book.author, str(book.library_id)[-9:])) + self.log.info("%s%-40.40s %-30.30s %-10.10s" % + (' '*indent,book.title, book.author, str(book.library_id)[-9:])) elif iswindows: - self.log.info("%-40.40s %-30.30s" % - (book.title, book.author)) + self.log.info("%s%-40.40s %-30.30s" % + (' '*indent,book.title, book.author)) + self.log.info() - def _dump_cached_book(self, cached_book, header=None): + def _dump_cached_book(self, cached_book, header=None,indent=0): ''' ''' if header: - msg = '%s' % header + msg = '%s%s' % (' '*indent,header) self.log.info(msg) - self.log.info( "%s" % ('-' * len(msg))) + self.log.info( "%s%s" % (' '*indent, '-' * len(msg))) if isosx: - self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" % - ('title', + self.log.info("%s%-40.40s %-30.30s %-10.10s %-10.10s %s" % + (' '*indent, + 'title', 'author', 'lib_book', - 'dev_book')) - self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" % - (cached_book['title'], + 'dev_book', + 'uuid')) + self.log.info("%s%-40.40s %-30.30s %-10.10s %-10.10s %s" % + (' '*indent, + cached_book['title'], cached_book['author'], str(cached_book['lib_book'])[-9:], - str(cached_book['dev_book'])[-9:])) + str(cached_book['dev_book'])[-9:], + cached_book['uuid'])) elif iswindows: - self.log.info("%-40.40s %-30.30s" % - (cached_book['title'], - cached_book['author'])) + self.log.info("%s%-40.40s %-30.30s %s" % + (' '*indent, + cached_book['title'], + cached_book['author'], + cached_book['uuid'])) self.log.info() - def _dump_cached_books(self, header=None): + def _dump_cached_books(self, header=None, indent=0): ''' ''' if header: - msg = '\nself.cached_books, %s' % header + msg = '\n%sself.cached_books %s:' % (' '*indent,header) self.log.info(msg) - self.log.info( "%s" % ('-' * len(msg))) + self.log.info( "%s%s" % (' '*indent,'-' * len(msg))) if isosx: - self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" % - ('title', - 'author', - 'lib_book', - 'dev_book')) for cb in self.cached_books.keys(): - self.log.info("%-40.40s %-30.30s %-10.10s %-10.10s" % - (self.cached_books[cb]['title'], + self.log.info("%s%-40.40s %-30.30s %-10.10s %-10.10s %s" % + (' '*indent, + self.cached_books[cb]['title'], self.cached_books[cb]['author'], str(self.cached_books[cb]['lib_book'])[-9:], - str(self.cached_books[cb]['dev_book'])[-9:])) + str(self.cached_books[cb]['dev_book'])[-9:], + self.cached_books[cb]['uuid'])) elif iswindows: for cb in self.cached_books.keys(): - self.log.info("%-40.40s %-30.30s" % - (self.cached_books[cb]['title'], - self.cached_books[cb]['author'])) + self.log.info("%s%-40.40s %-30.30s %s" % + (' '*indent, + self.cached_books[cb]['title'], + self.cached_books[cb]['author'], + self.cached_books[cb]['uuid'])) self.log.info() + def _dump_epub_metadata(self, fpath): + ''' + ''' + self.log.info(" ITUNES.__get_epub_metadata()") + title = None + author = None + timestamp = None + zf = ZipFile(fpath,'r') + fnames = zf.namelist() + opf = [x for x in fnames if '.opf' in x][0] + if opf: + opf_raw = cStringIO.StringIO(zf.read(opf)).getvalue() + soup = BeautifulSoup(opf_raw) + title = soup.find('dc:title').renderContents() + author = soup.find('dc:creator').renderContents() + ts = soup.find('meta',attrs={'name':'calibre:timestamp'}) + if ts: + # Touch existing calibre timestamp + timestamp = ts['content'] + + if not title or not author: + if DEBUG: + self.log.error(" couldn't extract title/author from %s in %s" % (opf,fpath)) + self.log.error(" title: %s author: %s timestamp: %s" % (title, author, timestamp)) + else: + if DEBUG: + self.log.error(" can't find .opf in %s" % fpath) + zf.close() + return (title, author, timestamp) + + def _dump_files(self, files, header=None,indent=0): + if header: + msg = '\n%sfiles passed to %s:' % (' '*indent,header) + self.log.info(msg) + self.log.info( "%s%s" % (' '*indent,'-' * len(msg))) + for file in files: + if getattr(file, 'orig_file_path', None) is not None: + self.log.info(" %s%s" % (' '*indent,file.orig_file_path)) + elif getattr(file, 'name', None) is not None: + self.log.info(" %s%s" % (' '*indent,file.name)) + self.log.info() + def _dump_hex(self, src, length=16): ''' ''' @@ -1251,18 +1373,6 @@ class ITUNES(DevicePlugin): N+=length print result - def _dump_files(self, files, header=None): - if header: - msg = '\nfiles passed to %s:' % header - self.log.info(msg) - self.log.info( "%s" % ('-' * len(msg))) - for file in files: - if getattr(file, 'orig_file_path', None) is not None: - self.log.info(" %s" % file.orig_file_path) - elif getattr(file, 'name', None) is not None: - self.log.info(" %s" % file.name) - self.log.info() - def _dump_library_books(self, library_books): ''' ''' @@ -1272,52 +1382,60 @@ class ITUNES(DevicePlugin): self.log.info(" %s" % book) self.log.info() - def _dump_update_list(self,header=None): + def _dump_update_list(self,header=None,indent=0): if header: - msg = '\nself.update_list called from %s' % header + msg = '\n%sself.update_list %s' % (' '*indent,header) self.log.info(msg) - self.log.info( "%s" % ('-' * len(msg))) + self.log.info( "%s%s" % (' '*indent,'-' * len(msg))) if isosx: for ub in self.update_list: - self.log.info("%-40.40s %-30.30s %-10.10s" % - (ub['title'], + self.log.info("%s%-40.40s %-30.30s %-10.10s" % + (' '*indent, + ub['title'], ub['author'], str(ub['lib_book'])[-9:])) elif iswindows: for ub in self.update_list: - self.log.info("%-40.40s %-30.30s" % - (ub['title'], + self.log.info("%s%-40.40s %-30.30s" % + (' '*indent, + ub['title'], ub['author'])) self.log.info() - def _find_device_book(self, cached_book): + def _find_device_book(self, search): ''' Windows-only method to get a handle to device book in the current pythoncom session ''' if iswindows: - if DEBUG: - self.log.info(" ITUNES._find_device_book()") - self.log.info(" looking for '%s' by %s" % (cached_book['title'], cached_book['author'])) - dev_books = self._get_device_books_playlist() + if DEBUG: + self.log.info(" ITUNES._find_device_book(uuid)") + self.log.info(" searching for %s ('%s' by %s)" % + (search['uuid'], search['title'], search['author'])) attempts = 9 while attempts: - # Find book whose Artist field = cached_book['author'] - hits = dev_books.Search(cached_book['author'],self.SearchField.index('Artists')) + # Try by uuid + hits = dev_books.Search(search['uuid'],self.SearchField.index('Albums')) if hits: - for hit in hits: - self.log.info(" evaluating '%s' by %s" % (hit.Name, hit.Artist)) - if hit.Name == cached_book['title']: - self.log.info(" matched '%s' by %s" % (hit.Name, hit.Artist)) - return hit + hit = hits[0] + self.log.info(" found '%s' by %s (%s)" % (hit.Name, hit.Artist, hit.Album)) + return hit + + # Try by author + hits = dev_books.Search(search['author'],self.SearchField.index('Artists')) + if hits: + hit = hits[0] + self.log.info(" found '%s' by %s" % (hit.Name, hit.Artist)) + return hit + attempts -= 1 time.sleep(0.5) if DEBUG: self.log.warning(" attempt #%d" % (10 - attempts)) if DEBUG: - self.log.error(" search for '%s' yielded no hits" % cached_book['title']) + self.log.error(" no hits") return None def _find_library_book(self, cached_book): @@ -1327,7 +1445,12 @@ class ITUNES(DevicePlugin): if iswindows: if DEBUG: self.log.info(" ITUNES._find_library_book()") - self.log.info(" looking for '%s' by %s" % (cached_book['title'], cached_book['author'])) + if 'uuid' in cached_book: + self.log.info(" looking for '%s' by %s (%s)" % + (cached_book['title'], cached_book['author'], cached_book['uuid'])) + else: + self.log.info(" looking for '%s' by %s" % + (cached_book['title'], cached_book['author'])) for source in self.iTunes.sources: if source.Kind == self.Sources.index('Library'): @@ -1354,18 +1477,27 @@ class ITUNES(DevicePlugin): attempts = 9 while attempts: - # Find book whose Artist field = cached_book['author'] + # Find book whose Album field = cached_book['uuid'] + if 'uuid' in cached_book: + hits = lib_books.Search(cached_book['uuid'],self.SearchField.index('Albums')) + if hits: + hit = hits[0] + if DEBUG: + self.log.info(" found '%s' by %s (%s)" % (hit.Name, hit.Artist, hit.Album)) + return hit + hits = lib_books.Search(cached_book['author'],self.SearchField.index('Artists')) if hits: - for hit in hits: - self.log.info(" evaluating '%s' by %s" % (hit.Name, hit.Artist)) - if hit.Name == cached_book['title']: - self.log.info(" matched '%s' by %s" % (hit.Name, hit.Artist)) - return hit + hit = hits[0] + if hit.Name == cached_book['title']: + if DEBUG: + self.log.info(" found '%s' by %s (%s)" % (hit.Name, hit.Artist, hit.Album)) + return hit + attempts -= 1 time.sleep(0.5) if DEBUG: - self.log.warning(" attempt #%d" % (10 - attempts)) + self.log.warning(" attempt #%d" % (10 - attempts)) if DEBUG: self.log.error(" search for '%s' yielded no hits" % cached_book['title']) @@ -1382,11 +1514,11 @@ class ITUNES(DevicePlugin): thumb_path = book_path.rpartition('.')[0] + '.jpg' try: - zfr = zipfile.ZipFile(archive_path) + zfr = ZipFile(archive_path) thumb_data = zfr.read(thumb_path) zfr.close() except: - zfw = zipfile.ZipFile(archive_path, mode='a') + zfw = ZipFile(archive_path, mode='a') else: return thumb_data @@ -1420,9 +1552,9 @@ class ITUNES(DevicePlugin): return None # Save the cover from iTunes - tmp_thumb = os.path.join(tempfile.gettempdir(), "thumb.%s" % self.ArtworkFormat[book.Artwork.Item(1).Format]) - book.Artwork.Item(1).SaveArtworkToFile(tmp_thumb) try: + tmp_thumb = os.path.join(tempfile.gettempdir(), "thumb.%s" % self.ArtworkFormat[book.Artwork.Item(1).Format]) + book.Artwork.Item(1).SaveArtworkToFile(tmp_thumb) # Resize the cover im = PILImage.open(tmp_thumb) scaled, width, height = fit_image(im.size[0],im.size[1], 60, 80) @@ -1445,15 +1577,16 @@ class ITUNES(DevicePlugin): ''' Calculate the exploded size of file ''' - myZip = zipfile.ZipFile(file,'r') + myZip = ZipFile(file,'r') myZipList = myZip.infolist() exploded_file_size = 0 for file in myZipList: exploded_file_size += file.file_size - if DEBUG: + if False: self.log.info(" ITUNES._get_device_book_size()") self.log.info(" %d items in archive" % len(myZipList)) self.log.info(" compressed: %d exploded: %d" % (compressed_size, exploded_file_size)) + myZip.close() return exploded_file_size def _get_device_books(self): @@ -1484,8 +1617,11 @@ class ITUNES(DevicePlugin): self.log.info(" ignoring '%s' of type '%s'" % (book.name(), book.kind())) else: if DEBUG: - self.log.info(" adding %-30.30s %-30.30s [%s]" % (book.name(), book.artist(), book.kind())) + self.log.info(" %-30.30s %-30.30s %s [%s]" % + (book.name(), book.artist(), book.album(), book.kind())) device_books.append(book) + if DEBUG: + self.log.info() elif iswindows: if 'iPod' in self.sources: @@ -1513,8 +1649,10 @@ class ITUNES(DevicePlugin): self.log.info(" ignoring '%s' of type '%s'" % (book.Name, book.KindAsString)) else: if DEBUG: - self.log.info(" adding %-30.30s %-30.30s [%s]" % (book.Name, book.Artist, book.KindAsString)) + self.log.info(" %-30.30s %-30.30s %s [%s]" % (book.Name, book.Artist, book.Album, book.KindAsString)) device_books.append(book) + if DEBUG: + self.log.info() finally: pythoncom.CoUninitialize() @@ -1525,8 +1663,8 @@ class ITUNES(DevicePlugin): ''' assumes pythoncom wrapper ''' - if DEBUG: - self.log.info(" ITUNES._get_device_books_playlist()") +# if DEBUG: +# self.log.info(" ITUNES._get_device_books_playlist()") if iswindows: if 'iPod' in self.sources: pl = None @@ -1542,11 +1680,12 @@ class ITUNES(DevicePlugin): self.log.error(" no iPad|Books playlist found") return pl - def _get_fpath(self,file): + def _get_fpath(self,file, metadata, update_md=False): ''' If the database copy will be deleted after upload, we have to use file (the PersistentTemporaryFile), which will be around until calibre exits. + If we're using the database copy, delete the plist ''' if DEBUG: self.log.info(" ITUNES._get_fpath()") @@ -1554,12 +1693,25 @@ class ITUNES(DevicePlugin): fpath = file if not getattr(fpath, 'deleted_after_upload', False): if getattr(file, 'orig_file_path', None) is not None: + # Database copy fpath = file.orig_file_path + self._delete_iTunesMetadata_plist(fpath) elif getattr(file, 'name', None) is not None: + # PTF fpath = file.name else: + # Recipe - PTF if DEBUG: self.log.info(" file will be deleted after upload") + + if update_md: + self._update_epub_metadata(fpath, metadata) + +# if DEBUG: +# self.log.info(" metadata before rewrite: '{0[0]}' '{0[1]}' '{0[2]}'".format(self._dump_epub_metadata(fpath))) +# self._update_epub_metadata(fpath, metadata) +# if DEBUG: +# self.log.info(" metadata after rewrite: '{0[0]}' '{0[1]}' '{0[2]}'".format(self._dump_epub_metadata(fpath))) return fpath def _get_library_books(self): @@ -1609,12 +1761,12 @@ class ITUNES(DevicePlugin): if str(book.description()).startswith(self.description_prefix): if book.location() == appscript.k.missing_value: library_orphans[path] = book - if DEBUG: + if False: self.log.info(" found iTunes PTF '%s' in Library|Books" % book.name()) library_books[path] = book if DEBUG: - self.log.info(" adding %-30.30s %-30.30s [%s]" % (book.name(), book.artist(), book.kind())) + self.log.info(" %-30.30s %-30.30s %s [%s]" % (book.name(), book.artist(), book.album(), book.kind())) else: if DEBUG: self.log.info(' no Library playlists') @@ -1627,10 +1779,10 @@ class ITUNES(DevicePlugin): for source in self.iTunes.sources: if source.Kind == self.Sources.index('Library'): lib = source - self.log.info(" Library source: '%s' kind: %s" % (lib.Name, self.Sources[lib.Kind])) + self.log.info(" Library source: '%s' kind: %s" % (lib.Name, self.Sources[lib.Kind])) break else: - self.log.error(" Library source not found") + self.log.error(" Library source not found") if lib is not None: lib_books = None @@ -1639,22 +1791,22 @@ class ITUNES(DevicePlugin): if pl.Kind == self.PlaylistKind.index('User') and \ pl.SpecialKind == self.PlaylistSpecialKind.index('Books'): if DEBUG: - self.log.info(" Books playlist: '%s'" % (pl.Name)) + self.log.info(" Books playlist: '%s'" % (pl.Name)) lib_books = pl.Tracks break else: if DEBUG: - self.log.error(" no Library|Books playlist found") + self.log.error(" no Library|Books playlist found") else: if DEBUG: - self.log.error(" no Library playlists found") + self.log.error(" no Library playlists found") try: for book in lib_books: # This may need additional entries for international iTunes users if book.KindAsString in ['MPEG audio file']: if DEBUG: - self.log.info(" ignoring %-30.30s of type '%s'" % (book.Name, book.KindAsString)) + self.log.info(" ignoring %-30.30s of type '%s'" % (book.Name, book.KindAsString)) else: path = self.path_template % (book.Name, book.Artist) @@ -1662,12 +1814,12 @@ class ITUNES(DevicePlugin): if book.Description.startswith(self.description_prefix): if not book.Location: library_orphans[path] = book - if DEBUG: + if False: self.log.info(" found iTunes PTF '%s' in Library|Books" % book.Name) library_books[path] = book if DEBUG: - self.log.info(" adding %-30.30s %-30.30s [%s]" % (book.Name, book.Artist, book.KindAsString)) + self.log.info(" %-30.30s %-30.30s %s [%s]" % (book.Name, book.Artist, book.Album, book.KindAsString)) except: if DEBUG: self.log.info(" no books in library") @@ -1744,24 +1896,24 @@ class ITUNES(DevicePlugin): self.log.info( "ITUNES:open(): Launching iTunes" ) self.iTunes = iTunes= appscript.app('iTunes', hide=True) iTunes.run() - initial_status = 'launched' + self.initial_status = 'launched' else: self.iTunes = appscript.app('iTunes') - initial_status = 'already running' + self.initial_status = 'already running' # Read the current storage path for iTunes media cmd = "defaults read com.apple.itunes NSNavLastRootDirectory" proc = subprocess.Popen( cmd, shell=True, cwd=os.curdir, stdout=subprocess.PIPE) proc.wait() - media_dir = os.path.abspath(proc.communicate()[0].strip()) + media_dir = os.path.expanduser(proc.communicate()[0].strip()) if os.path.exists(media_dir): self.iTunes_media = media_dir else: self.log.error(" could not confirm valid iTunes.media_dir from %s" % 'com.apple.itunes') - + self.log.error(" media_dir: %s" % media_dir) if DEBUG: - self.log.info(" [%s - %s (%s), driver version %d.%d.%d]" % - (self.iTunes.name(), self.iTunes.version(), initial_status, + self.log.info(" [OSX %s - %s (%s), driver version %d.%d.%d]" % + (self.iTunes.name(), self.iTunes.version(), self.initial_status, self.version[0],self.version[1],self.version[2])) self.log.info(" iTunes_media: %s" % self.iTunes_media) if iswindows: @@ -1773,7 +1925,7 @@ class ITUNES(DevicePlugin): self.iTunes = win32com.client.Dispatch("iTunes.Application") if not DEBUG: self.iTunes.Windows[0].Minimized = True - initial_status = 'launched' + self.initial_status = 'launched' # Read the current storage path for iTunes media from the XML file with open(self.iTunes.LibraryXMLPath, 'r') as xml: @@ -1789,8 +1941,8 @@ class ITUNES(DevicePlugin): self.log.error(" '%s' not found" % media_dir) if DEBUG: - self.log.info(" [%s - %s (%s), driver version %d.%d.%d]" % - (self.iTunes.Windows[0].name, self.iTunes.Version, initial_status, + self.log.info(" [Windows %s - %s (%s), driver version %d.%d.%d]" % + (self.iTunes.Windows[0].name, self.iTunes.Version, self.initial_status, self.version[0],self.version[1],self.version[2])) self.log.info(" iTunes_media: %s" % self.iTunes_media) @@ -1801,7 +1953,7 @@ class ITUNES(DevicePlugin): This occurs when the user deletes a book in iBooks while disconnected ''' if DEBUG: - self.log.info("\n ITUNES._purge_orphans") + self.log.info(" ITUNES._purge_orphans()") #self._dump_library_books(library_books) #self.log.info(" cached_books:\n %s" % "\n ".join(cached_books.keys())) @@ -1824,39 +1976,45 @@ class ITUNES(DevicePlugin): 'author':library_books[book].Artist, 'lib_book':library_books[book]} self._remove_from_iTunes(btr) + if DEBUG: + self.log.info() - def _remove_existing_copies(self,path,file,metadata): + def _remove_existing_copy(self, path, metadata): ''' ''' if DEBUG: - self.log.info(" ITUNES._remove_existing_copies()") + self.log.info(" ITUNES._remove_existing_copy()") if self.manual_sync_mode: # Delete existing from Device|Books, add to self.update_list # for deletion from booklist[0] during add_books_to_metadata - if path in self.cached_books: - self.update_list.append(self.cached_books[path]) - self._remove_from_device(self.cached_books[path]) - if DEBUG: - self.log.info( " deleting device book '%s'" % (path)) - if not getattr(file, 'deleted_after_upload', False): - self._remove_from_iTunes(self.cached_books[path]) + for book in self.cached_books: + if self.cached_books[book]['uuid'] == metadata.uuid: + self.update_list.append(self.cached_books[book]) + self._remove_from_device(self.cached_books[book]) if DEBUG: - self.log.info(" deleting library book '%s'" % path) + self.log.info( " deleting device book '%s'" % (metadata.title)) + if not getattr(file, 'deleted_after_upload', False): + self._remove_from_iTunes(self.cached_books[book]) + if DEBUG: + self.log.info(" deleting library book '%s'" % metadata.title) + break else: if DEBUG: self.log.info(" '%s' not in cached_books" % metadata.title) else: # Delete existing from Library|Books, add to self.update_list # for deletion from booklist[0] during add_books_to_metadata - if path in self.cached_books: - self.update_list.append(self.cached_books[path]) - self._remove_from_iTunes(self.cached_books[path]) - if DEBUG: - self.log.info( " deleting library book '%s'" % path) - else: - if DEBUG: - self.log.info(" '%s' not in cached_books" % metadata.title) + for book in self.cached_books: + if self.cached_books[book]['uuid'] == metadata.uuid: + self.update_list.append(self.cached_books[book]) + self._remove_from_iTunes(self.cached_books[book]) + if DEBUG: + self.log.info( " deleting library book '%s'" % metadata.title) + break + else: + if DEBUG: + self.log.info(" '%s' not in cached_books" % metadata.title) def _remove_from_device(self, cached_book): ''' @@ -1864,22 +2022,18 @@ class ITUNES(DevicePlugin): ''' self.log.info(" ITUNES._remove_from_device()") if isosx: - if DEBUG: + if False: self.log.info(" deleting %s" % cached_book['dev_book']) cached_book['dev_book'].delete() elif iswindows: dev_pl = self._get_device_books_playlist() - hits = dev_pl.Search(cached_book['author'],self.SearchField.index('Artists')) + hits = dev_pl.Search(cached_book['uuid'],self.SearchField.index('Albums')) if hits: - for hit in hits: - if DEBUG: - self.log.info(" evaluating '%s' by %s" % (hit.Name, hit.Artist)) - if hit.Name == cached_book['title']: - if DEBUG: - self.log.info(" deleting '%s' by %s" % (hit.Name, hit.Artist)) - hit.Delete() - break + hit = hits[0] + if False: + self.log.info(" deleting '%s' by %s (%s)" % (hit.Name, hit.Artist, hit.Album)) + hit.Delete() def _remove_from_iTunes(self, cached_book): ''' @@ -1917,13 +2071,18 @@ class ITUNES(DevicePlugin): self.log.info(" author_storage_path not empty (%d objects):" % len(author_files)) self.log.info(" %s" % '\n'.join(author_files)) else: - self.log.info(" '%s' stored external to iTunes, no files deleted" % cached_book['title']) + self.log.info(" '%s' (stored external to iTunes, no files deleted)" % cached_book['title']) except: # We get here if there was an error with .location().path - self.log.info(" removing orphan '%s' from iTunes" % cached_book['title']) + if DEBUG: + self.log.info(" '%s' not found in iTunes" % cached_book['title']) - self.iTunes.delete(cached_book['lib_book']) + try: + self.iTunes.delete(cached_book['lib_book']) + except: + if DEBUG: + self.log.info(" '%s' not found in iTunes" % cached_book['title']) elif iswindows: ''' @@ -1935,28 +2094,97 @@ class ITUNES(DevicePlugin): path = book.Location except: book = self._find_library_book(cached_book) - path = book.Location - storage_path = os.path.split(book.Location) - if book.Location.startswith(self.iTunes_media): - if DEBUG: - self.log.info(" removing '%s' at %s" % - (cached_book['title'], path)) - try: - os.remove(path) - except: - self.log.warning(" could not find '%s' in iTunes storage" % path) - try: - os.rmdir(storage_path[0]) - self.log.info(" removed folder '%s'" % storage_path[0]) - except: - self.log.info(" folder '%s' not found or not empty" % storage_path[0]) + if book: + storage_path = os.path.split(book.Location) + if book.Location.startswith(self.iTunes_media): + if DEBUG: + self.log.info(" removing '%s' at %s" % + (cached_book['title'], book.Location)) + try: + os.remove(path) + except: + self.log.warning(" could not find '%s' in iTunes storage" % path) + try: + os.rmdir(storage_path[0]) + self.log.info(" removed folder '%s'" % storage_path[0]) + except: + self.log.info(" folder '%s' not found or not empty" % storage_path[0]) - # Delete from iTunes database + # Delete from iTunes database + else: + self.log.info(" '%s' (stored external to iTunes, no files deleted)" % cached_book['title']) else: - self.log.info(" '%s' stored external to iTunes, no files deleted" % cached_book['title']) + if DEBUG: + self.log.info(" '%s' not found in iTunes" % cached_book['title']) + try: + book.Delete() + except: + if DEBUG: + self.log.info(" '%s' not found in iTunes" % cached_book['title']) - book.Delete() + def _update_epub_metadata(self, fpath, metadata): + ''' + ''' + self.log.info(" ITUNES._update_epub_metadata()") + + # Refresh epub metadata + with open(fpath,'r+b') as zfo: + ''' + # Touch the timestamp to force a recache + if metadata.timestamp: + if DEBUG: + self.log.info(" old timestamp: %s" % metadata.timestamp) + old_ts = metadata.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(" new timestamp: %s" % metadata.timestamp) + else: + metadata.timestamp = isoformat(now()) + if DEBUG: + self.log.info(" add timestamp: %s" % metadata.timestamp) + ''' + # Touch the OPF timestamp + zf_opf = ZipFile(fpath,'r') + fnames = zf_opf.namelist() + opf = [x for x in fnames if '.opf' in x][0] + if opf: + opf_raw = cStringIO.StringIO(zf_opf.read(opf)).getvalue() + soup = BeautifulSoup(opf_raw) + md = soup.find('metadata') + ts = md.find('meta',attrs={'name':'calibre:timestamp'}) + if ts: + # Touch existing calibre timestamp + timestamp = ts['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) + else: + metadata.timestamp = isoformat(now()) + if DEBUG: + self.log.info(" add timestamp: %s" % metadata.timestamp) + zf_opf.close() + + # If 'News' in tags, tweak the title/author for friendlier display in iBooks + if _('News') in metadata.tags: + if metadata.title.find('[') > 0: + metadata.title = metadata.title[:metadata.title.find('[')-1] + date_as_author = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y')) + metadata.author = metadata.authors = [date_as_author] + sort_author = re.sub('^\s*A\s+|^\s*The\s+|^\s*An\s+', '', metadata.title).rstrip() + metadata.author_sort = '%s %s' % (sort_author, strftime('%Y-%m-%d')) + + # Remove any non-alpha category tags + for tag in metadata.tags: + if not self._is_alpha(tag[0]): + metadata.tags.remove(tag) + + # If windows & series, nuke tags so series used as Category during _update_iTunes_metadata() + if iswindows and metadata.series: + metadata.tags = None + + set_metadata(zfo, metadata, update_timestamp=True) def _update_device(self, msg='', wait=True): ''' @@ -2014,6 +2242,20 @@ class ITUNES(DevicePlugin): strip_tags = re.compile(r'<[^<]*?/?>') if isosx: + if lb_added: + lb_added.album.set(metadata.uuid) + lb_added.description.set("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S'))) + lb_added.enabled.set(True) + lb_added.sort_artist.set(metadata.author_sort.title()) + lb_added.sort_name.set(this_book.title_sorter) + + if db_added: + db_added.album.set(metadata.uuid) + db_added.description.set("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S'))) + db_added.enabled.set(True) + db_added.sort_artist.set(metadata.author_sort.title()) + db_added.sort_name.set(this_book.title_sorter) + if metadata.comments: if lb_added: lb_added.comment.set(strip_tags.sub('',metadata.comments)) @@ -2030,31 +2272,41 @@ class ITUNES(DevicePlugin): except: pass - if lb_added: - lb_added.description.set("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S'))) - lb_added.enabled.set(True) - lb_added.sort_artist.set(metadata.author_sort.title()) - lb_added.sort_name.set(this_book.title_sorter) - - if db_added: - db_added.description.set("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S'))) - db_added.enabled.set(True) - db_added.sort_artist.set(metadata.author_sort.title()) - db_added.sort_name.set(this_book.title_sorter) - - # Set genre from metadata - # iTunes grabs the first dc:subject from the opf metadata, - # But we can manually override with first tag starting with alpha - for tag in metadata.tags: - if self._is_alpha(tag[0]): - if lb_added: - lb_added.genre.set(tag) - if db_added: - db_added.genre.set(tag) - break - + # Set genre from series if available, else first alpha tag + # Otherwise iTunes grabs the first dc:subject from the opf metadata, + if metadata.series: + if lb_added: + lb_added.genre.set(metadata.series) + lb_added.episode_ID.set(metadata.series) + lb_added.episode_number.set(metadata.series_index) + if db_added: + db_added.genre.set(metadata.series) + db_added.episode_ID.set(metadata.series) + db_added.episode_number.set(metadata.series_index) + elif metadata.tags: + for tag in metadata.tags: + if self._is_alpha(tag[0]): + if lb_added: + lb_added.genre.set(tag) + if db_added: + db_added.genre.set(tag) + break elif iswindows: + if lb_added: + lb_added.Album = metadata.uuid + lb_added.Description = ("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S'))) + lb_added.Enabled = True + lb_added.SortArtist = (metadata.author_sort.title()) + lb_added.SortName = (this_book.title_sorter) + + if db_added: + db_added.Album = metadata.uuid + db_added.Description = ("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S'))) + db_added.Enabled = True + db_added.SortArtist = (metadata.author_sort.title()) + db_added.SortName = (this_book.title_sorter) + if metadata.comments: if lb_added: lb_added.Comment = (strip_tags.sub('',metadata.comments)) @@ -2069,29 +2321,40 @@ class ITUNES(DevicePlugin): if db_added: db_added.AlbumRating = (metadata.rating*10) except: - pass + if DEBUG: + self.log.warning(" iTunes automation interface reported an error" + " setting AlbumRating") - if lb_added: - lb_added.Description = ("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S'))) - lb_added.Enabled = True - lb_added.SortArtist = (metadata.author_sort.title()) - lb_added.SortName = (this_book.title_sorter) + # Set Category from first alpha tag, overwrite with series if available + # Otherwise iBooks uses first from opf + # iTunes balks on setting EpisodeNumber, but it sticks (9.1.1.12) - if db_added: - db_added.Description = ("%s %s" % (self.description_prefix,strftime('%Y-%m-%d %H:%M:%S'))) - db_added.SortArtist = (metadata.author_sort.title()) - db_added.SortName = (this_book.title_sorter) + if metadata.series: + if lb_added: + lb_added.Category = metadata.series + lb_added.EpisodeID = metadata.series + try: + lb_added.EpisodeNumber = metadata.series_index + except: + pass + if db_added: + db_added.Category = metadata.series + db_added.EpisodeID = metadata.series + try: + db_added.EpisodeNumber = metadata.series_index + except: + if DEBUG: + self.log.warning(" iTunes automation interface reported an error" + " setting EpisodeNumber") + elif metadata.tags: + for tag in metadata.tags: + if self._is_alpha(tag[0]): + if lb_added: + lb_added.Category = tag + if db_added: + db_added.Category = tag + break - # Set genre from metadata - # iTunes grabs the first dc:subject from the opf metadata, - # But we can manually override with first tag starting with alpha - for tag in metadata.tags: - if self._is_alpha(tag[0]): - if lb_added: - lb_added.Category = (tag) - if db_added: - db_added.Category = (tag) - break class BookList(list): ''' diff --git a/src/calibre/ebooks/chm/reader.py b/src/calibre/ebooks/chm/reader.py index bbb43af567..d0a81e8e7f 100644 --- a/src/calibre/ebooks/chm/reader.py +++ b/src/calibre/ebooks/chm/reader.py @@ -8,7 +8,7 @@ import os, re from mimetypes import guess_type as guess_mimetype from calibre.ebooks.BeautifulSoup import BeautifulSoup, NavigableString - +from calibre.constants import iswindows from calibre.utils.chm.chm import CHMFile from calibre.utils.chm.chmlib import ( CHM_RESOLVE_SUCCESS, CHM_ENUMERATE_NORMAL, @@ -135,10 +135,16 @@ class CHMReader(CHMFile): if lpath.find(';') != -1: # fix file names with ";" at the end, see _reformat() lpath = lpath.split(';')[0] - with open(lpath, 'wb') as f: - if guess_mimetype(path)[0] == ('text/html'): - data = self._reformat(data) - f.write(data) + try: + with open(lpath, 'wb') as f: + if guess_mimetype(path)[0] == ('text/html'): + data = self._reformat(data) + f.write(data) + except: + if iswindows and len(lpath) > 250: + self.log.warn('%r filename too long, skipping'%path) + continue + raise self._extracted = True files = os.listdir(output_dir) if self.hhc_path not in files: diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py index 88d971ce4d..690cca511a 100644 --- a/src/calibre/ebooks/metadata/__init__.py +++ b/src/calibre/ebooks/metadata/__init__.py @@ -260,7 +260,7 @@ class MetaInformation(object): setattr(self, x, getattr(mi, x, None)) def print_all_attributes(self): - for x in ('author', 'author_sort', 'title_sort', 'comments', 'category', 'publisher', + for x in ('title','author', 'author_sort', 'title_sort', 'comments', 'category', 'publisher', 'series', 'series_index', 'tags', 'rating', 'isbn', 'language', 'application_id', 'manifest', 'toc', 'spine', 'guide', 'cover', 'book_producer', 'timestamp', 'lccn', 'lcc', 'ddc', 'pubdate', diff --git a/src/calibre/ebooks/metadata/epub.py b/src/calibre/ebooks/metadata/epub.py index d74ed37f66..b3980451bf 100644 --- a/src/calibre/ebooks/metadata/epub.py +++ b/src/calibre/ebooks/metadata/epub.py @@ -182,7 +182,7 @@ def get_metadata(stream, extract_cover=True): def get_quick_metadata(stream): return get_metadata(stream, False) -def set_metadata(stream, mi, apply_null=False): +def set_metadata(stream, mi, apply_null=False, update_timestamp=False): stream.seek(0) reader = OCFZipReader(stream, root=os.getcwdu()) mi = MetaInformation(mi) @@ -196,6 +196,8 @@ def set_metadata(stream, mi, apply_null=False): reader.opf.tags = [] if not getattr(mi, 'isbn', None): reader.opf.isbn = None + if update_timestamp and mi.timestamp is not None: + reader.opf.timestamp = mi.timestamp newopf = StringIO(reader.opf.render()) safe_replace(stream, reader.container[OPF.MIMETYPE], newopf) diff --git a/src/calibre/gui2/device.py b/src/calibre/gui2/device.py index d00dd2782c..07b5063e6c 100644 --- a/src/calibre/gui2/device.py +++ b/src/calibre/gui2/device.py @@ -689,14 +689,28 @@ class DeviceMixin(object): # {{{ self.device_error_dialog.show() # Device connected {{{ - def device_detected(self, connected, is_folder_device): - ''' - Called when a device is connected to the computer. - ''' + + def set_device_menu_items_state(self, connected, is_folder_device): if connected: self._sync_menu.connect_to_folder_action.setEnabled(False) if is_folder_device: self._sync_menu.disconnect_from_folder_action.setEnabled(True) + self._sync_menu.enable_device_actions(True, + self.device_manager.device.card_prefix(), + self.device_manager.device) + self.eject_action.setEnabled(True) + else: + self._sync_menu.connect_to_folder_action.setEnabled(True) + self._sync_menu.disconnect_from_folder_action.setEnabled(False) + self._sync_menu.enable_device_actions(False) + self.eject_action.setEnabled(False) + + def device_detected(self, connected, is_folder_device): + ''' + Called when a device is connected to the computer. + ''' + self.set_device_menu_items_state(connected, is_folder_device) + if connected: self.device_manager.get_device_information(\ Dispatcher(self.info_read)) self.set_default_thumbnail(\ @@ -705,17 +719,10 @@ class DeviceMixin(object): # {{{ self.device_manager.device.__class__.get_gui_name()+\ _(' detected.'), 3000) self.device_connected = 'device' if not is_folder_device else 'folder' - self._sync_menu.enable_device_actions(True, - self.device_manager.device.card_prefix(), - self.device_manager.device) self.location_view.model().device_connected(self.device_manager.device) - self.eject_action.setEnabled(True) self.refresh_ondevice_info (device_connected = True, reset_only = True) else: - self._sync_menu.connect_to_folder_action.setEnabled(True) - self._sync_menu.disconnect_from_folder_action.setEnabled(False) self.device_connected = None - self._sync_menu.enable_device_actions(False) self.location_view.model().update_devices() self.vanity.setText(self.vanity_template%\ dict(version=self.latest_version, device=' ')) @@ -723,7 +730,6 @@ class DeviceMixin(object): # {{{ if self.current_view() != self.library_view: self.book_details.reset_info() self.location_view.setCurrentIndex(self.location_view.model().index(0)) - self.eject_action.setEnabled(False) self.refresh_ondevice_info (device_connected = False) def info_read(self, job): diff --git a/src/calibre/gui2/metadata.py b/src/calibre/gui2/metadata.py index daed69725c..cd4cc1be41 100644 --- a/src/calibre/gui2/metadata.py +++ b/src/calibre/gui2/metadata.py @@ -84,12 +84,12 @@ class DownloadMetadata(Thread): if mi.isbn: args['isbn'] = mi.isbn else: - if not mi.title: + if not mi.title or mi.title == _('Unknown'): self.failures[id] = \ (str(id), _('Book has neither title nor ISBN')) continue args['title'] = mi.title - if mi.authors: + if mi.authors and mi.authors[0] != _('Unknown'): args['author'] = mi.authors[0] if self.key: args['isbndb_key'] = self.key diff --git a/src/calibre/gui2/ui.py b/src/calibre/gui2/ui.py index aa2d94a637..6452890883 100644 --- a/src/calibre/gui2/ui.py +++ b/src/calibre/gui2/ui.py @@ -410,6 +410,8 @@ class Main(MainWindow, Ui_MainWindow, DeviceMixin, ToolbarMixin, # {{{ self.tags_view.set_new_model() # in case columns changed self.tags_view.recount() self.create_device_menu() + self.set_device_menu_items_state(bool(self.device_connected), + self.device_connected == 'folder') if not patheq(self.library_path, d.database_location): newloc = d.database_location diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index 9e05babecc..73e0fae8e8 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -788,6 +788,7 @@ class BasicNewsRecipe(Recipe): } .summary_byline { + text-align:left; font-family:monospace; } @@ -1139,12 +1140,6 @@ class BasicNewsRecipe(Recipe): mi = MetaInformation(self.short_title() + strftime(self.timefmt), [__appname__]) mi.publisher = __appname__ mi.author_sort = __appname__ - if self.output_profile.name == 'iPad': - date_as_author = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y')) - mi = MetaInformation(self.short_title(), [date_as_author]) - mi.publisher = __appname__ - sort_author = re.sub('^\s*A\s+|^\s*The\s+|^\s*An\s+', '', self.title).rstrip() - mi.author_sort = '%s %s' % (sort_author, strftime('%Y-%m-%d')) mi.publication_type = 'periodical:'+self.publication_type mi.timestamp = nowf() mi.comments = self.description