#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:
parent
9952201f54
commit
4624c8f4a8
|
@ -1,5 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import os
|
||||||
|
os.environ['DJANGO_SETTINGS_MODULE']='web.settings'
|
||||||
from web.library.models import *
|
from web.library.models import *
|
||||||
from gdata.books.service import BookService
|
from gdata.books.service import BookService
|
||||||
#from dibbler_snippet import ConfirmMenu
|
#from dibbler_snippet import ConfirmMenu
|
||||||
|
@ -14,46 +16,78 @@ exit_commands = ['exit', 'abort', 'quit', 'bye', 'eat flaming death', 'q']
|
||||||
def get_book_loop():
|
def get_book_loop():
|
||||||
service = BookService(source='Programvareverkstedet - Worblehat - 0.1a ')
|
service = BookService(source='Programvareverkstedet - Worblehat - 0.1a ')
|
||||||
while True:
|
while True:
|
||||||
input = raw_input('Enter ISBN number> ')
|
indata = raw_input('Enter ISBN number> ')
|
||||||
if input in exit_commands:
|
if indata in exit_commands:
|
||||||
break
|
break
|
||||||
feed = service.search_by_keyword('isbn='+input)
|
feed = service.search_by_keyword('isbn='+indata)
|
||||||
if len(feed.entry) == 0:
|
if feed.entry:
|
||||||
print "No items found"
|
if len(feed.entry) == 1:
|
||||||
elif len(feed.entry) == 1:
|
print "Found one book: "+feed.entry[0].dc_title[0].text
|
||||||
print "Found one item: "+feed.entry[0].dc_title[0].text
|
build_book(feed.entry[0], indata)
|
||||||
b = build_book(feed.entry[0], input)
|
|
||||||
else:
|
else:
|
||||||
print "Found several items, OWNOES!"
|
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()
|
dic = entry.to_dict()
|
||||||
# print dic
|
|
||||||
# print entry
|
book = {}
|
||||||
b = Book(title=entry.dc_title[0].text)
|
|
||||||
|
book['action'] = 'new-book'
|
||||||
|
|
||||||
|
book['title'] = entry.dc_title[0].text
|
||||||
if len(entry.dc_title) > 0:
|
if len(entry.dc_title) > 0:
|
||||||
b.subtitle = ''.join(map(lambda x: x.text, entry.dc_title[1:]))
|
book['subtitle'] = ''.join(map(lambda x: x.text, entry.dc_title[1:]))
|
||||||
|
|
||||||
isbn = find_isbn(dic['identifiers'])
|
isbn = find_isbn(dic['identifiers'])
|
||||||
if isbn:
|
if isbn:
|
||||||
b.isbn = isbn
|
book['isbn'] = isbn
|
||||||
elif input:
|
elif indata:
|
||||||
if len(input) == 13:
|
if len(indata) == 13:
|
||||||
b.isbn = input
|
book['isbn'] = indata
|
||||||
else:
|
else:
|
||||||
print "No ISBN found"
|
print "No ISBN found"
|
||||||
return False
|
return False
|
||||||
b.description = find_description(dic)
|
|
||||||
print entry.description
|
book['description'] = find_description(dic)
|
||||||
|
|
||||||
if 'date' in dic:
|
if 'date' in dic:
|
||||||
b.published_year = int(dic['date'][:4])
|
book['published_year'] = int(dic['date'][:4])
|
||||||
|
|
||||||
if 'publishers' in dic:
|
if 'publishers' in dic:
|
||||||
b.publisher = ','.join(dic['publishers'])
|
book['publisher'] = ','.join(dic['publishers'])
|
||||||
b.num_pages = find_page_number(dic)
|
|
||||||
new_objects = build_authors(b, dic)
|
book['num_pages'] = find_page_number(dic)
|
||||||
b.full_print()
|
|
||||||
b.save()
|
book['persons'] = {}
|
||||||
for object in new_objects:
|
book['persons']['author'] = build_authors(dic)
|
||||||
object.save()
|
|
||||||
|
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):
|
def find_description(dic):
|
||||||
|
@ -77,16 +111,12 @@ def find_isbn(identifiers):
|
||||||
return pair[1]
|
return pair[1]
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def build_authors(book, dictionary):
|
def build_authors(dictionary):
|
||||||
if 'authors' in dictionary:
|
if 'authors' in dictionary:
|
||||||
author_list = []
|
author_list = []
|
||||||
for author in dictionary['authors']:
|
for author in dictionary['authors']:
|
||||||
author_list.append(get_or_create_author(author))
|
author_list.append(get_or_create_author(author))
|
||||||
relation_list = []
|
return author_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
|
|
||||||
else:
|
else:
|
||||||
print "No authors found"
|
print "No authors found"
|
||||||
return []
|
return []
|
||||||
|
|
Reference in New Issue