suggest_book now conceptually working, needs to look better
This commit is contained in:
parent
2000445902
commit
6cd76a205d
|
@ -24,42 +24,64 @@ connection = pgdb.connect(database='oysteini_pbb2',
|
||||||
def suggest_book(dbconnection, tmp_file=False):
|
def suggest_book(dbconnection, tmp_file=False):
|
||||||
service = BookService(source='Programvareverkstedet - Worblehat - 0.1a ')
|
service = BookService(source='Programvareverkstedet - Worblehat - 0.1a ')
|
||||||
action_list = []
|
action_list = []
|
||||||
print("Enter ISBN number(s), end with eof")
|
print("Enter ISBN number(s), end with eof <CTRL+D>")
|
||||||
for indata in sys.stdin:
|
for indata in sys.stdin:
|
||||||
if indata in exit_commands:
|
if indata in exit_commands:
|
||||||
print("aborted")
|
print("aborted")
|
||||||
break
|
break
|
||||||
if book_in_db(dbconnection, indata):
|
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:
|
else:
|
||||||
feed = service.search_by_keyword('isbn='+indata)
|
feed = service.search_by_keyword('isbn='+indata)
|
||||||
if feed.entry:
|
if feed.entry:
|
||||||
# action_list is passed to build_book to fill in missing authors
|
authors = parse_authors(dbconnection, feed.entry[0])
|
||||||
action_list.append(build_book(dbconnection, feed.entry[0], indata, action_list))
|
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:
|
else:
|
||||||
print("No items found")
|
print("No items found")
|
||||||
|
|
||||||
if tmp_file:
|
if tmp_file:
|
||||||
# TODO: write to tmp file
|
# TODO: write to tmp file
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
print(write_actionlist(action_list))
|
print(write_actionlist(action_list))
|
||||||
|
|
||||||
def book_in_db(dbconnection, isbn):
|
def book_in_db(dbconnection, isbn):
|
||||||
cursor = dbconnection.cursor()
|
cursor = dbconnection.cursor()
|
||||||
query = "SELECT book FROM books WHERE lastname=%(last)s OR firstname=%(first)s"
|
query = "SELECT * FROM book WHERE isbn=%(num)s"
|
||||||
cursor.execute(query, {'last':last, 'first':first} )
|
cursor.execute(query, {'num':isbn})
|
||||||
candidates = fetchall_dict(cursor)
|
if cursor.rowcount > 0:
|
||||||
pass
|
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):
|
def found_item(entry, indata):
|
||||||
print "Found: "+entry.dc_title[0].text
|
print "Found: "+entry.dc_title[0].text
|
||||||
build_book(entry, indata)
|
build_book(entry, indata)
|
||||||
|
|
||||||
def build_book(dbconnection, entry, indata=False):
|
def build_book(entry, authors, indata=False):
|
||||||
dic = entry.to_dict()
|
dic = entry.to_dict()
|
||||||
print("Dict in build_book:" + str(dic))
|
|
||||||
print("")
|
|
||||||
|
|
||||||
book = {}
|
book = {}
|
||||||
|
|
||||||
book['action'] = 'new-book'
|
book['action'] = 'new-book'
|
||||||
|
@ -81,7 +103,6 @@ def build_book(dbconnection, entry, indata=False):
|
||||||
if len(entry.dc_title) > 0:
|
if len(entry.dc_title) > 0:
|
||||||
book['subtitle'] = unicode(''.join(map(lambda x: x.text, entry.dc_title[1:])))
|
book['subtitle'] = unicode(''.join(map(lambda x: x.text, entry.dc_title[1:])))
|
||||||
|
|
||||||
authors = build_authors(dbconnection, dic)
|
|
||||||
book['persons'] = {}
|
book['persons'] = {}
|
||||||
book['persons']['author'] = [author['id'] for author in authors]
|
book['persons']['author'] = [author['id'] for author in authors]
|
||||||
|
|
||||||
|
@ -102,10 +123,6 @@ def build_book(dbconnection, entry, indata=False):
|
||||||
book['references'] = {}
|
book['references'] = {}
|
||||||
book['references']['google-books'] = [unicode(dic['preview'])]
|
book['references']['google-books'] = [unicode(dic['preview'])]
|
||||||
|
|
||||||
global comments
|
|
||||||
book['comment'] = comments
|
|
||||||
comments = ''
|
|
||||||
|
|
||||||
return book
|
return book
|
||||||
|
|
||||||
def comment(comm):
|
def comment(comm):
|
||||||
|
@ -140,6 +157,42 @@ def find_isbn(identifiers):
|
||||||
return pair[1]
|
return pair[1]
|
||||||
return False
|
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):
|
def build_authors(dbconnection, dictionary):
|
||||||
if 'authors' in dictionary:
|
if 'authors' in dictionary:
|
||||||
author_list = []
|
author_list = []
|
||||||
|
@ -150,6 +203,7 @@ def build_authors(dbconnection, dictionary):
|
||||||
comment("No authors found.")
|
comment("No authors found.")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
# deprecated
|
||||||
def get_or_create_author(dbconnection, author_name):
|
def get_or_create_author(dbconnection, author_name):
|
||||||
author = {}
|
author = {}
|
||||||
author['action'] = 'new-person'
|
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")
|
print("No candidate found for " + author_name + ", making new person")
|
||||||
newid = ''.join([i[0] for i in names]).lower()
|
newid = ''.join([i[0] for i in names]).lower()
|
||||||
# Check if id already exists
|
# Check if id already exists
|
||||||
idq =
|
|
||||||
|
|
||||||
|
|
||||||
#Cargo-cult coded function to unescape special XML characters
|
#Cargo-cult coded function to unescape special XML characters
|
||||||
|
|
Reference in New Issue