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

#2 påbegynt. Fikset build_book slik at den ikke skriver til databasen, men returnerer en stor dictionary som inneholder data fra Google Books, i henhold til commit-file-format.txt.

This commit is contained in:
Tiril Anette Langfeldt Rødland 2011-03-05 17:20:04 +00:00
parent 9952201f54
commit 4624c8f4a8
1 changed files with 69 additions and 39 deletions

View File

@ -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 []