Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

Fixed writing book/author suggestion to file, and made it prettier

This commit is contained in:
Andreas Lindahl Flåten 2011-10-08 19:20:02 +00:00
parent bd81dc4425
commit b34f8da82b
1 changed files with 32 additions and 14 deletions

View File

@ -24,31 +24,49 @@ 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 <CTRL+D>")
for indata in sys.stdin:
if indata in exit_commands:
authors_added = {}
file_prefix = "suggestion"
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")
break
if book_in_db(dbconnection, indata):
action_list.append("Book with isbn: " + str(indata) + " is already in DB, skipped")
elif book_in_db(dbconnection, ISBN):
action_list.append("Book with ISBN: " + str(ISBN) + " is already in DB, skipped")
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:
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:
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 author['id'] in authors_added:
comment = "Author already added when book: " + str(authors_added[author['id']]) + " was added"
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:
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'})
action_list.append(build_book(feed.entry[0], authors, indata))
d.update(build_book(feed.entry[0], authors, ISBN))
action_list.append(d)
else:
print("No items found")
if tmp_file:
# TODO: write to tmp file
pass
write_tmpfile(file_prefix, write_actionlist(action_list))
else:
print(write_actionlist(action_list))
@ -272,4 +290,4 @@ def unescape(s):
return es.join(list)
suggest_book(connection)
suggest_book(connection, tmp_file=False)