From 4624c8f4a85f396dc4cce222740ffddea4a4bd35 Mon Sep 17 00:00:00 2001 From: tirilane Date: Sat, 5 Mar 2011 17:20:04 +0000 Subject: [PATCH] =?UTF-8?q?#2=20p=C3=A5begynt.=20Fikset=20build=5Fbook=20s?= =?UTF-8?q?lik=20at=20den=20ikke=20skriver=20til=20databasen,=20men=20retu?= =?UTF-8?q?rnerer=20en=20stor=20dictionary=20som=20inneholder=20data=20fra?= =?UTF-8?q?=20Google=20Books,=20i=20henhold=20til=20commit-file-format.txt?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/google_interface.py | 108 +++++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 39 deletions(-) diff --git a/python/google_interface.py b/python/google_interface.py index bab177a..994ec0f 100644 --- a/python/google_interface.py +++ b/python/google_interface.py @@ -1,5 +1,7 @@ #!/usr/bin/python +import os +os.environ['DJANGO_SETTINGS_MODULE']='web.settings' from web.library.models import * from gdata.books.service import BookService #from dibbler_snippet import ConfirmMenu @@ -14,46 +16,78 @@ exit_commands = ['exit', 'abort', 'quit', 'bye', 'eat flaming death', 'q'] def get_book_loop(): service = BookService(source='Programvareverkstedet - Worblehat - 0.1a ') while True: - input = raw_input('Enter ISBN number> ') - if input in exit_commands: + indata = raw_input('Enter ISBN number> ') + if indata in exit_commands: break - feed = service.search_by_keyword('isbn='+input) - if len(feed.entry) == 0: - print "No items found" - elif len(feed.entry) == 1: - print "Found one item: "+feed.entry[0].dc_title[0].text - b = build_book(feed.entry[0], input) - else: - print "Found several items, OWNOES!" + feed = service.search_by_keyword('isbn='+indata) + if feed.entry: + if len(feed.entry) == 1: + print "Found one book: "+feed.entry[0].dc_title[0].text + build_book(feed.entry[0], indata) + else: + print "Found multiple books: " + for i in range(len(feed.entry)): #entry in feed.entry: + print "%d: %s" % (i, +# build_authors(feed.entry[i].to_dict())[0], + feed.entry[i].dc_title[0].text) + bookno = (int)(raw_input("Which book do you want? [0-%d] " % (len(feed.entry) - 1))) + build_book(feed.entry[bookno], indata) + else: + print "No items found" -def build_book(entry, input=False): +def found_item(entry, indata): + print "Found: "+entry.dc_title[0].text + build_book(entry, indata) + +def build_book(entry, indata=False): dic = entry.to_dict() -# print dic -# print entry - b = Book(title=entry.dc_title[0].text) + + book = {} + + book['action'] = 'new-book' + + book['title'] = entry.dc_title[0].text if len(entry.dc_title) > 0: - b.subtitle = ''.join(map(lambda x: x.text, entry.dc_title[1:])) - isbn = find_isbn(dic['identifiers']) - if isbn: - b.isbn = isbn - elif input: - if len(input) == 13: - b.isbn = input + book['subtitle'] = ''.join(map(lambda x: x.text, entry.dc_title[1:])) + + isbn = find_isbn(dic['identifiers']) + if isbn: + book['isbn'] = isbn + elif indata: + if len(indata) == 13: + book['isbn'] = indata else: print "No ISBN found" return False - b.description = find_description(dic) - print entry.description - if 'date' in dic: - b.published_year = int(dic['date'][:4]) - if 'publishers' in dic: - b.publisher = ','.join(dic['publishers']) - b.num_pages = find_page_number(dic) - new_objects = build_authors(b, dic) - b.full_print() - b.save() - for object in new_objects: - object.save() + + book['description'] = find_description(dic) + + if 'date' in dic: + book['published_year'] = int(dic['date'][:4]) + + if 'publishers' in dic: + book['publisher'] = ','.join(dic['publishers']) + + book['num_pages'] = find_page_number(dic) + + book['persons'] = {} + book['persons']['author'] = build_authors(dic) + + book['references'] = {} + book['references']['google'] = dic['preview'] + + keys_to_add = ['edition', 'series', 'category', 'thumbnail', 'picture'] + for key in keys_to_add: + set_value(book, dic, key) + + return book + + +def set_value(book, dic, key): + if key in dic: + book[key] = dic[key] + else: + book[key] = '' def find_description(dic): @@ -77,16 +111,12 @@ def find_isbn(identifiers): return pair[1] return False -def build_authors(book, dictionary): +def build_authors(dictionary): if 'authors' in dictionary: author_list = [] for author in dictionary['authors']: author_list.append(get_or_create_author(author)) - relation_list = [] - auth_rel = Relation.objects.get_or_create(name="Author")[0] - for author in author_list: - relation_list.append(BookPerson(book=book,person=author,relation=auth_rel)) - return author_list+relation_list + return author_list else: print "No authors found" return []