def processAlternate(source_dir=None): # import a book from an alternate directory if not source_dir or os.path.isdir(source_dir) is False: logger.warn('Alternate directory must not be empty') return if source_dir == lazylibrarian.DESTINATION_DIR: logger.warn('Alternate directory must not be the same as destination') return new_book = book_file(source_dir, booktype='book') if new_book: # see if there is a metadata file in this folder with the info we need metafile = librarysync.opf_file(source_dir) try: metadata = librarysync.get_book_info(metafile) except: metadata = {} if 'title' in metadata and 'creator' in metadata: authorname = metadata['creator'] bookname = metadata['title'] # if not, try to get metadata from the book file else: try: metadata = librarysync.get_book_info(new_book) except: metadata = {} if 'title' in metadata and 'creator' in metadata: authorname = metadata['creator'] bookname = metadata['title'] myDB = database.DBConnection() authmatch = myDB.action( 'SELECT * FROM authors where AuthorName="%s"' % (authorname)).fetchone() if authmatch: logger.debug("ALT: Author %s found in database" % (authorname)) else: logger.debug("ALT: Author %s not found, adding to database" % (authorname)) importer.addAuthorToDB(authorname) bookid = librarysync.find_book_in_db(myDB, authorname, bookname) if bookid: import_book(source_dir, bookid) else: logger.warn("Book %s by %s not found in database" % (bookname, authorname)) else: logger.warn('Book %s has no metadata, unable to import' % new_book) else: logger.warn("No book file found in %s" % source_dir)
def processAlternate(source_dir=None): # import a book from an alternate directory if source_dir == None or source_dir == "": logger.warn('Alternate directory must not be empty') return if source_dir == lazylibrarian.DESTINATION_DIR: logger.warn('Alternate directory must not be the same as destination') return new_book = book_file(source_dir) if new_book: # see if there is a metadata file in this folder with the info we need metafile = librarysync.opf_file(source_dir) try: metadata = librarysync.get_book_info(metafile) except: metadata = {} if 'title' in metadata and 'creator' in metadata: authorname = metadata['creator'] bookname = metadata['title'] # if not, try to get metadata from the book file else: try: metadata = librarysync.get_book_info(new_book) except: metadata = {} if 'title' in metadata and 'creator' in metadata: authorname = metadata['creator'] bookname = metadata['title'] myDB = database.DBConnection() authmatch = myDB.action('SELECT * FROM authors where AuthorName="%s"' % (authorname)).fetchone() if authmatch: logger.debug("ALT: Author %s found in database" % (authorname)) else: logger.debug("ALT: Author %s not found, adding to database" % (authorname)) importer.addAuthorToDB(authorname) bookid = librarysync.find_book_in_db(myDB, authorname, bookname) if bookid: import_book(source_dir, bookid) else: logger.warn("Book %s by %s not found in database" % (bookname, authorname)) else: logger.warn('Book %s has no metadata, unable to import' % new_book) else: logger.warn("No book file found in %s" % source_dir)
def processAlternate(source_dir=None): # import a book from an alternate directory try: if not source_dir: logger.warn("Alternate Directory not configured") return False elif not os.path.isdir(source_dir): logger.warn("Alternate Directory [%s] not found" % source_dir) return False if source_dir == lazylibrarian.DIRECTORY('Destination'): logger.warn('Alternate directory must not be the same as Destination') return False logger.debug('Processing alternate directory %s' % source_dir) # first, recursively process any books in subdirectories for fname in os.listdir(source_dir): subdir = os.path.join(source_dir, fname) if os.path.isdir(subdir): processAlternate(subdir) # only import one book from each alternate (sub)directory, this is because # the importer may delete the directory after importing a book, # depending on lazylibrarian.DESTINATION_COPY setting # also if multiple books in a folder and only a "metadata.opf" # which book is it for? new_book = book_file(source_dir, booktype='book') if new_book: metadata = {} # see if there is a metadata file in this folder with the info we need # try book_name.opf first, or fall back to any filename.opf metafile = os.path.splitext(new_book)[0] + '.opf' if not os.path.isfile(metafile): metafile = opf_file(source_dir) if metafile and os.path.isfile(metafile): try: metadata = get_book_info(metafile) except Exception as e: logger.debug('Failed to read metadata from %s, %s' % (metafile, str(e))) else: logger.debug('No metadata file found for %s' % new_book) if 'title' not in metadata or 'creator' not in metadata: # if not got both, try to get metadata from the book file try: metadata = get_book_info(new_book) except Exception as e: logger.debug('No metadata found in %s, %s' % (new_book, str(e))) if 'title' in metadata and 'creator' in metadata: authorname = metadata['creator'] bookname = metadata['title'] myDB = database.DBConnection() authmatch = myDB.match('SELECT * FROM authors where AuthorName="%s"' % (authorname)) if not authmatch: # try goodreads preferred authorname logger.debug("Checking GoodReads for [%s]" % authorname) GR = GoodReads(authorname) try: author_gr = GR.find_author_id() except Exception: logger.debug("No author id for [%s]" % authorname) if author_gr: grauthorname = author_gr['authorname'] logger.debug("GoodReads reports [%s] for [%s]" % (grauthorname, authorname)) authorname = grauthorname authmatch = myDB.match('SELECT * FROM authors where AuthorName="%s"' % (authorname)) if authmatch: logger.debug("ALT: Author %s found in database" % (authorname)) else: logger.debug("ALT: Author %s not found, adding to database" % (authorname)) addAuthorToDB(authorname) bookid = find_book_in_db(myDB, authorname, bookname) if bookid: return import_book(source_dir, bookid) else: logger.warn("Book %s by %s not found in database" % (bookname, authorname)) else: logger.warn('Book %s has no metadata, unable to import' % new_book) else: logger.warn("No book file found in %s" % source_dir) return False except Exception as e: logger.error('Unhandled exception in processAlternate: %s' % traceback.format_exc())
def processAlternate(source_dir=None): # import a book from an alternate directory if not source_dir or os.path.isdir(source_dir) is False: logger.warn('Alternate directory not found') return if source_dir == lazylibrarian.DESTINATION_DIR: logger.warn('Alternate directory must not be the same as destination') return logger.debug('Processing alternate directory %s' % source_dir) # first, recursively process any books in subdirectories for fname in os.listdir(source_dir): subdir = os.path.join(source_dir, fname) if os.path.isdir(subdir): processAlternate(subdir) # only import one book from each alternate (sub)directory, this is because # the importer may delete the directory after importing a book, # depending on lazylibrarian.DESTINATION_COPY setting # also if multiple books in a folder and only a "metadata.opf" # which book is it for? new_book = book_file(source_dir, booktype='book') if new_book: metadata = {} # see if there is a metadata file in this folder with the info we need # try book_name.opf first, or fall back to any filename.opf metafile = os.path.splitext(new_book)[0] + '.opf' if not os.path.isfile(metafile): metafile = librarysync.opf_file(source_dir) if os.path.isfile(metafile): try: metadata = librarysync.get_book_info(metafile) except: logger.debug('Failed to read metadata from %s' % metafile) else: logger.debug('No metadata file found for %s' % new_book) if not 'title' in metadata and 'creator' in metadata: # try to get metadata from the book file try: metadata = librarysync.get_book_info(new_book) except: logger.debug('No metadata found in %s' % new_book) if 'title' in metadata and 'creator' in metadata: authorname = metadata['creator'] bookname = metadata['title'] myDB = database.DBConnection() authmatch = myDB.action('SELECT * FROM authors where AuthorName="%s"' % (authorname)).fetchone() if authmatch: logger.debug("ALT: Author %s found in database" % (authorname)) else: logger.debug("ALT: Author %s not found, adding to database" % (authorname)) importer.addAuthorToDB(authorname) bookid = librarysync.find_book_in_db(myDB, authorname, bookname) if bookid: import_book(source_dir, bookid) else: logger.warn("Book %s by %s not found in database" % (bookname, authorname)) else: logger.warn('Book %s has no metadata, unable to import' % new_book) else: logger.warn("No book file found in %s" % source_dir)