From b34f8da82b9b5c86924b1569c28ad71b7bc81ca3 Mon Sep 17 00:00:00 2001 From: andrfla Date: Sat, 8 Oct 2011 19:20:02 +0000 Subject: [PATCH] Fixed writing book/author suggestion to file, and made it prettier --- cli/google_interface.py | 46 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/cli/google_interface.py b/cli/google_interface.py index 51294fc..8d6557c 100644 --- a/cli/google_interface.py +++ b/cli/google_interface.py @@ -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 ") - 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 ") + 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)