From 6cd76a205d10bfbb17fd5e8e96f7a69165ba67d2 Mon Sep 17 00:00:00 2001 From: andrfla Date: Sat, 8 Oct 2011 17:11:34 +0000 Subject: [PATCH] suggest_book now conceptually working, needs to look better --- cli/google_interface.py | 93 ++++++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 20 deletions(-) diff --git a/cli/google_interface.py b/cli/google_interface.py index 36b143b..51294fc 100644 --- a/cli/google_interface.py +++ b/cli/google_interface.py @@ -24,42 +24,64 @@ connection = pgdb.connect(database='oysteini_pbb2', def suggest_book(dbconnection, tmp_file=False): service = BookService(source='Programvareverkstedet - Worblehat - 0.1a ') action_list = [] - print("Enter ISBN number(s), end with eof") + print("Enter ISBN number(s), end with eof ") for indata in sys.stdin: if indata in exit_commands: print("aborted") break if book_in_db(dbconnection, indata): - action_list.append("# Book with isbn: " + str(indata) + " is already in DB, skipped") + action_list.append("Book with isbn: " + str(indata) + " is already in DB, skipped") else: feed = service.search_by_keyword('isbn='+indata) if feed.entry: - # action_list is passed to build_book to fill in missing authors - action_list.append(build_book(dbconnection, feed.entry[0], indata, action_list)) - else: - print("No items found") + authors = parse_authors(dbconnection, feed.entry[0]) + for author in authors: + if not author_in_db(dbconnection, author): + action_list.append("Author: \"" + str(author) + "\" was not already in database") + action_list.append(build_author(author)) + if len(authors) == 0: + action_list.append("Didn't find any authors for book. !!IMPORTANT!! Add correct author and id in new-book section") + authors.append({'id':'NO_AUTHOR', 'firstname':'John', 'lastname':'Doe'}) + action_list.append(build_book(feed.entry[0], authors, indata)) + else: + print("No items found") if tmp_file: # TODO: write to tmp file + pass else: print(write_actionlist(action_list)) def book_in_db(dbconnection, isbn): cursor = dbconnection.cursor() - query = "SELECT book FROM books WHERE lastname=%(last)s OR firstname=%(first)s" - cursor.execute(query, {'last':last, 'first':first} ) - candidates = fetchall_dict(cursor) - pass + query = "SELECT * FROM book WHERE isbn=%(num)s" + cursor.execute(query, {'num':isbn}) + if cursor.rowcount > 0: + return True + return False + +def author_in_db(dbconnection, author): + cursor = dbconnection.cursor() + query = "SELECT * FROM person WHERE firstname=%(fname)s OR lastname=%(lname)s" + cursor.execute(query, {'fname':author['firstname'], 'lname':author['lastname']}) + if cursor.rowcount > 0: + return True + return False + +def person_id_in_db(dbconnection, id): + cursor = dbconnection.cursor() + query = "SELECT * FROM person WHERE id=%(i)s" + cursor.execute(query, {'i':id}) + if cursor.rowcount > 0: + return True + return False def found_item(entry, indata): print "Found: "+entry.dc_title[0].text build_book(entry, indata) -def build_book(dbconnection, entry, indata=False): +def build_book(entry, authors, indata=False): dic = entry.to_dict() - print("Dict in build_book:" + str(dic)) - print("") - book = {} book['action'] = 'new-book' @@ -81,7 +103,6 @@ def build_book(dbconnection, entry, indata=False): if len(entry.dc_title) > 0: book['subtitle'] = unicode(''.join(map(lambda x: x.text, entry.dc_title[1:]))) - authors = build_authors(dbconnection, dic) book['persons'] = {} book['persons']['author'] = [author['id'] for author in authors] @@ -102,10 +123,6 @@ def build_book(dbconnection, entry, indata=False): book['references'] = {} book['references']['google-books'] = [unicode(dic['preview'])] - global comments - book['comment'] = comments - comments = '' - return book def comment(comm): @@ -140,6 +157,42 @@ def find_isbn(identifiers): return pair[1] return False +def parse_authors(dbconnection, feed_entry): + dict = feed_entry.to_dict() + cursor = dbconnection.cursor() + author_list = [] + if 'authors' in dict: + for author in dict['authors']: + # First look in db for matches + auth_q = "SELECT * FROM person WHERE firstname LIKE %(fname)s OR lastname=%(lname)s" + names = author.split() + first_name_wildcard = names[0] + '%' + last_name = names[len(names)-1] + cursor.execute(auth_q, {'fname':first_name_wildcard, 'lname':last_name}) + match = fetchone_dict(cursor) + if match != None and 'id' in match: + author_list.append(match) + continue + # Otherwise make new id + newid = ''.join([i[0] for i in author.split()]).lower() + if person_id_in_db(dbconnection, newid): + i = "1" + while person_id_in_db(dbconnection, newid+i): + i = str(int(i) + 1) + newid = newid + i + first_name = names[0] + " ".join(names[1:len(names)-1]) + new_author = {'id':newid, 'firstname':first_name, 'lastname':last_name} + author_list.append(new_author) + return author_list + +def build_author(new_author): + author = new_author + author['action'] = 'new-person' + + return author + + +# deprecated def build_authors(dbconnection, dictionary): if 'authors' in dictionary: author_list = [] @@ -150,6 +203,7 @@ def build_authors(dbconnection, dictionary): comment("No authors found.") return [] +# deprecated def get_or_create_author(dbconnection, author_name): author = {} author['action'] = 'new-person' @@ -178,7 +232,6 @@ def get_or_create_author(dbconnection, author_name): print("No candidate found for " + author_name + ", making new person") newid = ''.join([i[0] for i in names]).lower() # Check if id already exists - idq = #Cargo-cult coded function to unescape special XML characters