def addAuthorToDB(authorname=None, refresh=False): myDB = database.DBConnection() GR = GoodReads(authorname) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace( "'", "''") dbauthor = myDB.match(query) controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } logger.debug("Now adding new author: %s to database" % authorname) else: newValueDict = {"Status": "Loading"} logger.debug("Now updating author: %s" % authorname) myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id(refresh=refresh) if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] if 'nophoto' in authorimg: authorimg = getAuthorImage(authorid) if authorimg and authorimg.startswith('http'): newimg = cache_cover(authorid, authorimg) if newimg: authorimg = newimg controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.warn(u"Nothing found for %s" % authorname) myDB.action('DELETE from authors WHERE AuthorName="%s"' % authorname) return # process books if lazylibrarian.BOOK_API == "GoogleBooks": book_api = GoogleBooks() book_api.get_author_books(authorid, authorname, refresh=refresh) elif lazylibrarian.BOOK_API == "GoodReads": GR.get_author_books(authorid, authorname, refresh=refresh) update_totals(authorid) logger.debug("[%s] Author update complete" % authorname)
def addAuthorToDB(authorname=None, refresh=False): myDB = database.DBConnection() GR = GoodReads(authorname) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace("'", "''") dbauthor = myDB.action(query).fetchone() controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } logger.debug("Now adding new author: %s to database" % authorname) else: newValueDict = {"Status": "Loading"} logger.debug("Now updating author: %s" % authorname) myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id(refresh=refresh) if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] if 'nophoto' in authorimg: authorimg = getAuthorImage(authorid) if authorimg and authorimg.startswith('http'): newimg = cache_cover(authorid, authorimg) if newimg: authorimg = newimg controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.warn(u"Nothing found for %s" % authorname) myDB.action('DELETE from authors WHERE AuthorName="%s"' % authorname) return # process books if lazylibrarian.BOOK_API == "GoogleBooks": book_api = GoogleBooks() book_api.get_author_books(authorid, authorname, refresh=refresh) elif lazylibrarian.BOOK_API == "GoodReads": GR.get_author_books(authorid, authorname, refresh=refresh) update_totals(authorid) logger.debug("[%s] Author update complete" % authorname)
def addAuthorToDB(authorname=None): threading.currentThread().name = "DBIMPORT" type = 'author' myDB = database.DBConnection() GR = GoodReads(authorname, type) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace("'","''") dbauthor = myDB.action(query).fetchone() controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } else: newValueDict = {"Status": "Loading"} myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id() if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": formatter.today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.error("Nothing found") # process books GR.get_author_books(authorid) lastbook = myDB.action("SELECT BookName, BookLink, BookDate from books WHERE AuthorID='%s' order by BookDate DESC" % authorid).fetchone() bookCount = myDB.select("SELECT COUNT(BookName) as counter FROM books WHERE AuthorID='%s'" % authorid) for count in bookCount: controlValueDict = {"AuthorID": authorid} newValueDict = { "Status": "Active", "TotalBooks": count['counter'], "LastBook": lastbook['BookName'], "LastLink": lastbook['BookLink'], "LastDate": lastbook['BookDate'] } myDB.upsert("authors", newValueDict, controlValueDict) logger.info("Processing complete: Added %s books to the database" % str(count['counter']))
def addAuthorToDB(authorname=None, refresh=False): threading.currentThread().name = "DBIMPORT" myDB = database.DBConnection() GR = GoodReads(authorname) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace( "'", "''") dbauthor = myDB.action(query).fetchone() controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } logger.debug("Now adding new author: %s to database" % authorname) else: newValueDict = {"Status": "Loading"} logger.debug("Now updating author: %s" % authorname) myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id(refresh=refresh) if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": formatter.today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.warn(u"Nothing found for %s" % authorname) myDB.action('DELETE from authors WHERE AuthorName="%s"' % authorname) return # process books if lazylibrarian.BOOK_API == "GoogleBooks": book_api = GoogleBooks() book_api.get_author_books(authorid, authorname, refresh=refresh) elif lazylibrarian.BOOK_API == "GoodReads": GR.get_author_books(authorid, authorname, refresh=refresh) logger.debug("[%s] Author update complete" % authorname)
def addAuthorToDB(authorname=None, refresh=False): threading.currentThread().name = "DBIMPORT" #type = 'author' myDB = database.DBConnection() GR = GoodReads(authorname) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace("'", "''") dbauthor = myDB.action(query).fetchone() controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } logger.info("Now adding new author: %s to database" % authorname) else: newValueDict = {"Status": "Loading"} logger.info("Now updating author: %s" % authorname) myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id() if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": formatter.today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.error("Nothing found") # process books if lazylibrarian.BOOK_API == "GoogleBooks": book_api = GoogleBooks() book_api.get_author_books(authorid, authorname, refresh=refresh) elif lazylibrarian.BOOK_API == "GoodReads": GR.get_author_books(authorid, authorname, refresh=refresh) logger.info("[%s] Author update complete" % authorname)
def addAuthorToDB(authorname=None): threading.currentThread().name = "DBIMPORT" type = 'author' myDB = database.DBConnection() GR = GoodReads(authorname, type) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace( "'", "''") dbauthor = myDB.action(query).fetchone() controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } else: newValueDict = {"Status": "Loading"} myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id() if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": formatter.today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.error("Nothing found") # process books GR.get_author_books(authorid)
def addAuthorToDB(authorname=None): threading.currentThread().name = "DBIMPORT" type = 'author' myDB = database.DBConnection() GR = GoodReads(authorname, type) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace("'","''") dbauthor = myDB.action(query).fetchone() controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } else: newValueDict = {"Status": "Loading"} myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id() if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": formatter.today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.error("Nothing found") # process books GR.get_author_books(authorid)
def addAuthorToDB(authorname=None, refresh=False, authorid=None, addbooks=True): """ Add an author to the database by name or id, and optionally get a list of all their books If author already exists in database, refresh their details and optionally booklist """ threadname = threading.currentThread().name if "Thread-" in threadname: threading.currentThread().name = "AddAuthorToDB" try: myDB = database.DBConnection() match = False authorimg = '' new_author = not refresh if authorid: controlValueDict = {"AuthorID": authorid} newValueDict = {"Status": "Loading"} dbauthor = myDB.match("SELECT * from authors WHERE AuthorID='%s'" % authorid) if not dbauthor: authorname = 'unknown author' logger.debug("Now adding new author id: %s to database" % authorid) new_author = True else: authorname = dbauthor['authorname'] logger.debug("Now updating author %s " % authorname) new_author = False myDB.upsert("authors", newValueDict, controlValueDict) GR = GoodReads(authorname) author = GR.get_author_info(authorid=authorid) if author: authorname = author['authorname'] authorimg = author['authorimg'] controlValueDict = {"AuthorID": authorid} newValueDict = { "AuthorLink": author['authorlink'], "DateAdded": today() } if not dbauthor or (dbauthor and not dbauthor['manual']): newValueDict["AuthorName"] = author['authorname'] newValueDict["AuthorImg"] = author['authorimg'] newValueDict["AuthorBorn"] = author['authorborn'] newValueDict["AuthorDeath"] = author['authordeath'] myDB.upsert("authors", newValueDict, controlValueDict) match = True else: logger.warn(u"Nothing found for %s" % authorid) if not dbauthor: myDB.action('DELETE from authors WHERE AuthorID="%s"' % authorid) if authorname and not match: authorname = ' '.join( authorname.split()) # ensure no extra whitespace GR = GoodReads(authorname) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace( "'", "''") dbauthor = myDB.match(query) controlValueDict = {"AuthorName": authorname} if not dbauthor: newValueDict = { "AuthorID": "0: %s" % authorname, "Status": "Loading" } logger.debug("Now adding new author: %s to database" % authorname) new_author = True else: newValueDict = {"Status": "Loading"} logger.debug("Now updating author: %s" % authorname) new_author = False myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id(refresh=refresh) if author: authorid = author['authorid'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": author['authorid'], "AuthorLink": author['authorlink'], "DateAdded": today(), "Status": "Loading" } if not dbauthor or (dbauthor and not dbauthor['manual']): newValueDict["AuthorImg"] = author['authorimg'] newValueDict["AuthorBorn"] = author['authorborn'] newValueDict["AuthorDeath"] = author['authordeath'] myDB.upsert("authors", newValueDict, controlValueDict) match = True else: logger.warn(u"Nothing found for %s" % authorname) if not dbauthor: myDB.action('DELETE from authors WHERE AuthorName="%s"' % authorname) return if not match: logger.error( "AddAuthorToDB: No matching result for authorname or authorid") return # if author is set to manual, should we allow replacing 'nophoto' ? new_img = False match = myDB.match("SELECT Manual from authors WHERE AuthorID='%s'" % authorid) if not match or not match['Manual']: if authorimg and 'nophoto' in authorimg: newimg = getAuthorImage(authorid) if newimg: authorimg = newimg new_img = True # allow caching if authorimg and authorimg.startswith('http'): newimg, success = cache_img("author", authorid, authorimg, refresh=refresh) if success: authorimg = newimg new_img = True else: logger.debug('Failed to cache image for %s' % authorimg) if new_img: controlValueDict = {"AuthorID": authorid} newValueDict = {"AuthorImg": authorimg} myDB.upsert("authors", newValueDict, controlValueDict) if addbooks: if new_author: bookstatus = lazylibrarian.CONFIG['NEWAUTHOR_STATUS'] else: bookstatus = lazylibrarian.CONFIG['NEWBOOK_STATUS'] # process books if lazylibrarian.CONFIG['BOOK_API'] == "GoogleBooks": book_api = GoogleBooks() book_api.get_author_books(authorid, authorname, bookstatus, refresh=refresh) elif lazylibrarian.CONFIG['BOOK_API'] == "GoodReads": GR = GoodReads(authorname) GR.get_author_books(authorid, authorname, bookstatus, refresh=refresh) # update totals works for existing authors only. # New authors need their totals updating after libraryscan or import of books. if not new_author: update_totals(authorid) else: # if we're not loading any books, mark author as ignored controlValueDict = {"AuthorID": authorid} newValueDict = {"Status": "Ignored"} myDB.upsert("authors", newValueDict, controlValueDict) msg = "[%s] Author update complete" % authorname logger.debug(msg) return msg except Exception: msg = 'Unhandled exception in addAuthorToDB: %s' % traceback.format_exc( ) logger.error(msg) return msg
def addAuthorToDB(authorname=None): threading.currentThread().name = "DBIMPORT" type = 'author' myDB = database.DBConnection() GR = GoodReads(authorname, type) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace( "'", "''") dbauthor = myDB.action(query).fetchone() controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } else: newValueDict = {"Status": "Loading"} myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id() if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": formatter.today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.error("Nothing found") # process books bookscount = 0 books = GR.get_author_books(authorid) for book in books: controlValueDict = {"BookID": book['bookid']} newValueDict = { "AuthorName": book['authorname'], "AuthorID": authorid, "AuthorLink": authorimg, "BookName": book['bookname'], "BookSub": book['booksub'], "BookDesc": book['bookdesc'], "BookIsbn": book['bookisbn'], "BookPub": book['bookpub'], "BookGenre": book['bookgenre'], "BookImg": book['bookimg'], "BookLink": book['booklink'], "BookRate": book['bookrate'], "BookPages": book['bookpages'], "BookDate": book['bookdate'], "BookLang": book['booklang'], "Status": "Skipped", "BookAdded": formatter.today() } myDB.upsert("books", newValueDict, controlValueDict) bookscount = bookscount + 1 lastbook = myDB.action( "SELECT BookName, BookLink, BookDate from books WHERE AuthorID='%s' order by BookDate DESC" % authorid).fetchone() bookCount = myDB.select( "SELECT COUNT(BookName) as counter FROM books WHERE AuthorID='%s'" % authorid) for count in bookCount: controlValueDict = {"AuthorID": authorid} newValueDict = { "Status": "Active", "TotalBooks": count['counter'], "LastBook": lastbook['BookName'], "LastLink": lastbook['BookLink'], "LastDate": lastbook['BookDate'] } myDB.upsert("authors", newValueDict, controlValueDict) logger.info("Processing complete: Added %s books to the database" % str(count['counter']))
def addAuthorToDB(authorname=None, refresh=False): """ Add an author to the database, and get list of all their books If author already exists in database, refresh their details and booklist """ try: myDB = database.DBConnection() GR = GoodReads(authorname) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace("'", "''") dbauthor = myDB.match(query) controlValueDict = {"AuthorName": authorname} if not dbauthor: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } logger.debug("Now adding new author: %s to database" % authorname) else: newValueDict = {"Status": "Loading"} logger.debug("Now updating author: %s" % authorname) myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id(refresh=refresh) if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.warn(u"Nothing found for %s" % authorname) myDB.action('DELETE from authors WHERE AuthorName="%s"' % authorname) return new_img = False if authorimg and 'nophoto' in authorimg: authorimg = getAuthorImage(authorid) new_img = True if authorimg and authorimg.startswith('http'): newimg = cache_cover(authorid, authorimg) if newimg: authorimg = newimg new_img = True if new_img: controlValueDict = {"AuthorID": authorid} newValueDict = {"AuthorImg": authorimg} myDB.upsert("authors", newValueDict, controlValueDict) # process books if lazylibrarian.BOOK_API == "GoogleBooks": book_api = GoogleBooks() book_api.get_author_books(authorid, authorname, refresh=refresh) elif lazylibrarian.BOOK_API == "GoodReads": GR.get_author_books(authorid, authorname, refresh=refresh) # update totals works for existing authors only. # New authors need their totals updating after libraryscan or import of books. if dbauthor: update_totals(authorid) logger.debug("[%s] Author update complete" % authorname) except Exception as e: logger.error('Unhandled exception in addAuthorToDB: %s' % traceback.format_exc())
def addAuthorToDB(authorname=None, refresh=False): threading.currentThread().name = "DBIMPORT" myDB = database.DBConnection() GR = GoodReads(authorname) query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace("'", "''") dbauthor = myDB.action(query).fetchone() controlValueDict = {"AuthorName": authorname} if dbauthor is None: newValueDict = { "AuthorID": "0: %s" % (authorname), "Status": "Loading" } logger.debug("Now adding new author: %s to database" % authorname) else: newValueDict = {"Status": "Loading"} logger.debug("Now updating author: %s" % authorname) myDB.upsert("authors", newValueDict, controlValueDict) author = GR.find_author_id(refresh=refresh) if author: authorid = author['authorid'] authorlink = author['authorlink'] authorimg = author['authorimg'] controlValueDict = {"AuthorName": authorname} newValueDict = { "AuthorID": authorid, "AuthorLink": authorlink, "AuthorImg": authorimg, "AuthorBorn": author['authorborn'], "AuthorDeath": author['authordeath'], "DateAdded": formatter.today(), "Status": "Loading" } myDB.upsert("authors", newValueDict, controlValueDict) else: logger.warn(u"Nothing found for %s" % authorname) myDB.action('DELETE from authors WHERE AuthorName="%s"' % authorname) return # process books if lazylibrarian.BOOK_API == "GoogleBooks": book_api = GoogleBooks() book_api.get_author_books(authorid, authorname, refresh=refresh) elif lazylibrarian.BOOK_API == "GoodReads": GR.get_author_books(authorid, authorname, refresh=refresh) havebooks = myDB.action( 'SELECT count("BookID") as counter from books WHERE AuthorName="%s" AND (Status="Have" OR Status="Open")' % authorname).fetchone() myDB.action('UPDATE authors set HaveBooks="%s" where AuthorName="%s"' % (havebooks['counter'], authorname)) totalbooks = myDB.action( 'SELECT count("BookID") as counter FROM books WHERE AuthorName="%s"' % authorname).fetchone() myDB.action('UPDATE authors set TotalBooks="%s" where AuthorName="%s"' % (totalbooks['counter'], authorname)) unignoredbooks = myDB.action( 'SELECT count("BookID") as counter FROM books WHERE AuthorName="%s" AND Status!="Ignored"' % authorname).fetchone() myDB.action('UPDATE authors set UnignoredBooks="%s" where AuthorName="%s"' % (unignoredbooks['counter'], authorname)) logger.debug("[%s] Author update complete" % authorname)