Fixed writing book/author suggestion to file, and made it prettier
This commit is contained in:
parent
bd81dc4425
commit
b34f8da82b
|
@ -24,31 +24,49 @@ 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 <CTRL+D>")
|
authors_added = {}
|
||||||
for indata in sys.stdin:
|
file_prefix = "suggestion"
|
||||||
if indata in exit_commands:
|
filler = ' -------------------------------------------------- '
|
||||||
|
if not tmp_file:
|
||||||
|
print("# Enter ISBN number(s), end with eof <CTRL+D>")
|
||||||
|
for ISBN in sys.stdin:
|
||||||
|
if ISBN.strip() in exit_commands:
|
||||||
print("aborted")
|
print("aborted")
|
||||||
break
|
break
|
||||||
if book_in_db(dbconnection, indata):
|
elif book_in_db(dbconnection, ISBN):
|
||||||
action_list.append("Book with isbn: " + str(indata) + " is already in DB, skipped")
|
action_list.append("Book with ISBN: " + str(ISBN) + " is already in DB, skipped")
|
||||||
else:
|
else:
|
||||||
feed = service.search_by_keyword('isbn='+indata)
|
# First print a long comment line to separate books
|
||||||
|
new_book = filler + "Book: " + ISBN.strip() + filler
|
||||||
|
action_list.append(new_book)
|
||||||
|
feed = service.search_by_keyword('isbn='+ISBN)
|
||||||
if feed.entry:
|
if feed.entry:
|
||||||
authors = parse_authors(dbconnection, feed.entry[0])
|
authors = parse_authors(dbconnection, feed.entry[0])
|
||||||
|
# For each author, check if author is already added as a new entry
|
||||||
|
# or already in DB, otherwise make an entry for a new author
|
||||||
for author in authors:
|
for author in authors:
|
||||||
if not author_in_db(dbconnection, author):
|
if author['id'] in authors_added:
|
||||||
action_list.append("Author: \"" + str(author) + "\" was not already in database")
|
comment = "Author already added when book: " + str(authors_added[author['id']]) + " was added"
|
||||||
action_list.append(build_author(author))
|
action_list.append(comment)
|
||||||
|
elif not author_in_db(dbconnection, author):
|
||||||
|
comment = "Author: \"" + str(author) + "\" was not already in database"
|
||||||
|
d = build_author(author)
|
||||||
|
d.update({'comment':comment})
|
||||||
|
action_list.append(d)
|
||||||
|
authors_added[author['id']] = ISBN
|
||||||
|
d = {}
|
||||||
if len(authors) == 0:
|
if len(authors) == 0:
|
||||||
action_list.append("Didn't find any authors for book. !!IMPORTANT!! Add correct author and id in new-book section")
|
# TODO: test this
|
||||||
|
comment = "Didn't find any authors for book. !!IMPORTANT!! Add correct author and id in new-book section"
|
||||||
|
d.update({'comment':comment})
|
||||||
authors.append({'id':'NO_AUTHOR', 'firstname':'John', 'lastname':'Doe'})
|
authors.append({'id':'NO_AUTHOR', 'firstname':'John', 'lastname':'Doe'})
|
||||||
action_list.append(build_book(feed.entry[0], authors, indata))
|
d.update(build_book(feed.entry[0], authors, ISBN))
|
||||||
|
action_list.append(d)
|
||||||
else:
|
else:
|
||||||
print("No items found")
|
print("No items found")
|
||||||
|
|
||||||
if tmp_file:
|
if tmp_file:
|
||||||
# TODO: write to tmp file
|
write_tmpfile(file_prefix, write_actionlist(action_list))
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
print(write_actionlist(action_list))
|
print(write_actionlist(action_list))
|
||||||
|
|
||||||
|
@ -272,4 +290,4 @@ def unescape(s):
|
||||||
return es.join(list)
|
return es.join(list)
|
||||||
|
|
||||||
|
|
||||||
suggest_book(connection)
|
suggest_book(connection, tmp_file=False)
|
||||||
|
|
Reference in New Issue