def update_booklist(filename, path, prefix): changed = False if path_to_ext( filename) in all_formats and self.is_allowed_book_file( filename, path, prefix): try: lpath = os.path.join(path, filename).partition( self.normalize_path(prefix))[2] if lpath.startswith(os.sep): lpath = lpath[len(os.sep):] lpath = lpath.replace('\\', '/') idx = bl_cache.get(lpath, None) if idx is not None: bl_cache[lpath] = None if self.update_metadata_item(bl[idx]): # print 'update_metadata_item returned true' changed = True else: if bl.add_book(self.book_from_path(prefix, lpath), replace_metadata=False): changed = True except: # Probably a filename encoding error import traceback traceback.print_exc() return changed
def __init__(self, prefix, lpath, size=None, other=None): from calibre.ebooks.metadata.meta import path_to_ext Metadata.__init__(self, '') self._new_book = False self.device_collections = [] self.path = os.path.join(prefix, lpath) if os.sep == '\\': self.path = self.path.replace('/', '\\') self.lpath = lpath.replace('\\', '/') else: self.lpath = lpath self.mime = mime_type_ext(path_to_ext(lpath)) self.size = size # will be set later if None try: self.datetime = time.gmtime(os.path.getctime(self.path)) except: self.datetime = time.gmtime() if other: self.smart_update(other)
def update_booklist(filename, path, prefix): changed = False if path_to_ext(filename) in all_formats or self.is_a_book_file(filename, path, prefix): try: lpath = os.path.join(path, filename).partition(self.normalize_path(prefix))[2] if lpath.startswith(os.sep): lpath = lpath[len(os.sep):] lpath = lpath.replace('\\', '/') idx = bl_cache.get(lpath, None) if idx is not None: bl_cache[lpath] = None if self.update_metadata_item(bl[idx]): # print 'update_metadata_item returned true' changed = True else: if bl.add_book(self.book_from_path(prefix, lpath), replace_metadata=False): changed = True except: # Probably a filename encoding error import traceback traceback.print_exc() return changed
def update_device_books(self, connection, booklist, source_id, plugboard, dbpath): from calibre.ebooks.metadata.meta import path_to_ext opts = self.settings() upload_covers = opts.extra_customization[self.OPT_UPLOAD_COVERS] refresh_covers = opts.extra_customization[self.OPT_REFRESH_COVERS] use_sony_authors = opts.extra_customization[self.OPT_USE_SONY_AUTHORS] db_books = self.read_device_books(connection, source_id, dbpath) cursor = connection.cursor() for book in booklist: # Run through plugboard if needed if plugboard is not None: newmi = book.deepcopy_metadata() newmi.template_to_attribute(book, plugboard) else: newmi = book # Get Metadata We Want lpath = book.lpath try: if opts.use_author_sort: if newmi.author_sort: author = newmi.author_sort else: author = authors_to_sort_string(newmi.authors) else: if use_sony_authors: author = newmi.authors[0] else: author = authors_to_string(newmi.authors) except: author = _('Unknown') title = newmi.title or _('Unknown') # Get modified date # If there was a detected offset, use that. Otherwise use UTC (same as Sony software) modified_date = os.path.getmtime(book.path) * 1000 if self.device_offset is not None: modified_date = modified_date + self.device_offset if lpath not in db_books: query = ''' INSERT INTO books (title, author, source_id, added_date, modified_date, file_path, file_name, file_size, mime_type, corrupted, prevent_delete) values (?,?,?,?,?,?,?,?,?,0,0) ''' t = (title, author, source_id, int(time.time() * 1000), modified_date, lpath, os.path.basename(lpath), book.size, book.mime or mime_type_ext(path_to_ext(lpath))) cursor.execute(query, t) book.bookId = self.get_lastrowid(cursor) if upload_covers: self.upload_book_cover(connection, book, source_id) debug_print('Inserted New Book: (%u) '%book.bookId + book.title) else: query = ''' UPDATE books SET title = ?, author = ?, modified_date = ?, file_size = ? WHERE file_path = ? ''' t = (title, author, modified_date, book.size, lpath) cursor.execute(query, t) book.bookId = db_books[lpath] if refresh_covers: self.upload_book_cover(connection, book, source_id) db_books[lpath] = None if self.is_sony_periodical(book): self.periodicalize_book(connection, book) for book, bookId in db_books.items(): if bookId is not None: # Remove From Collections query = 'DELETE FROM collections WHERE content_id = ?' t = (bookId,) cursor.execute(query, t) # Remove from Books query = 'DELETE FROM books where _id = ?' t = (bookId,) cursor.execute(query, t) debug_print('Deleted Book:' + book) connection.commit() cursor.close()
def update_device_books(self, connection, booklist, source_id, plugboard, dbpath): from calibre.ebooks.metadata.meta import path_to_ext from calibre.ebooks.metadata import authors_to_sort_string, authors_to_string opts = self.settings() db_books = self.read_device_books(connection, source_id, dbpath) cursor = connection.cursor() for book in booklist: # Run through plugboard if needed if plugboard is not None: newmi = book.deepcopy_metadata() newmi.template_to_attribute(book, plugboard) else: newmi = book # Get Metadata We Want lpath = book.lpath try: if opts.use_author_sort: if newmi.author_sort: author = newmi.author_sort else: author = authors_to_sort_string(newmi.authors) else: author = authors_to_string(newmi.authors) except Exception: author = _('Unknown') title = newmi.title or _('Unknown') # Get modified date # If there was a detected offset, use that. Otherwise use UTC (same as Sony software) modified_date = os.path.getmtime(book.path) * 1000 if self.device_offset is not None: modified_date = modified_date + self.device_offset if lpath not in db_books: query = ''' INSERT INTO books (bookname, authorname, description, addeddate, seriesname, seriesorder, filename, mimetype) values (?,?,?,?,?,?,?,?) ''' t = (title, author, book.get('comments', None), int(time.time() * 1000), book.get('series', None), book.get('series_index', sys.maxint), lpath, book.mime or mime_type_ext(path_to_ext(lpath))) cursor.execute(query, t) book.bookId = connection.last_insert_rowid() debug_print('Inserted New Book: (%u) '%book.bookId + book.title) else: query = ''' UPDATE books SET bookname = ?, authorname = ?, addeddate = ? WHERE filename = ? ''' t = (title, author, modified_date, lpath) cursor.execute(query, t) book.bookId = db_books[lpath] db_books[lpath] = None for book, bookId in db_books.items(): if bookId is not None: # Remove From Collections query = 'DELETE FROM tags WHERE _id in (select tag_id from booktags where book_id = ?)' t = (bookId,) cursor.execute(query, t) # Remove from Books query = 'DELETE FROM books where _id = ?' t = (bookId,) cursor.execute(query, t) debug_print('Deleted Book:' + book) cursor.close()