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

Google-interfjaset fungerer, og printer action-lista til standard out. Dette bør skrives til fil en gang i framtida.

This commit is contained in:
Tiril Anette Langfeldt Rødland 2011-03-05 21:51:29 +00:00
parent 54d4288f95
commit 76fcb16adf
1 changed files with 73 additions and 39 deletions

View File

@ -10,11 +10,18 @@ from gdata.books.service import BookService
import xml.parsers.expat import xml.parsers.expat
import readline import readline
import re import re
import random
from web.library.fileformat import *
exit_commands = ['exit', 'abort', 'quit', 'bye', 'eat flaming death', 'q'] exit_commands = ['exit', 'abort', 'quit', 'bye', 'eat flaming death', 'q']
comments = ""
action_list = []
def get_book_loop(): def get_book_loop():
service = BookService(source='Programvareverkstedet - Worblehat - 0.1a ') service = BookService(source='Programvareverkstedet - Worblehat - 0.1a ')
global action_list
while True: while True:
indata = raw_input('Enter ISBN number> ') indata = raw_input('Enter ISBN number> ')
if indata in exit_commands: if indata in exit_commands:
@ -23,7 +30,7 @@ def get_book_loop():
if feed.entry: if feed.entry:
if len(feed.entry) == 1: if len(feed.entry) == 1:
print "Found one book: "+feed.entry[0].dc_title[0].text print "Found one book: "+feed.entry[0].dc_title[0].text
build_book(feed.entry[0], indata) action_list.append(build_book(feed.entry[0], indata))
else: else:
print "Found multiple books: " print "Found multiple books: "
for i in range(len(feed.entry)): #entry in feed.entry: for i in range(len(feed.entry)): #entry in feed.entry:
@ -31,10 +38,12 @@ def get_book_loop():
# build_authors(feed.entry[i].to_dict())[0], # build_authors(feed.entry[i].to_dict())[0],
feed.entry[i].dc_title[0].text) feed.entry[i].dc_title[0].text)
bookno = (int)(raw_input("Which book do you want? [0-%d] " % (len(feed.entry) - 1))) bookno = (int)(raw_input("Which book do you want? [0-%d] " % (len(feed.entry) - 1)))
build_book(feed.entry[bookno], indata) action_list.append(build_book(feed.entry[bookno], indata))
else: else:
print "No items found" print "No items found"
print write_actionlist(action_list)
def found_item(entry, indata): def found_item(entry, indata):
print "Found: "+entry.dc_title[0].text print "Found: "+entry.dc_title[0].text
build_book(entry, indata) build_book(entry, indata)
@ -46,48 +55,60 @@ def build_book(entry, indata=False):
book['action'] = 'new-book' book['action'] = 'new-book'
book['title'] = entry.dc_title[0].text isbn = find_isbn(unicode(dic['identifiers']))
if len(entry.dc_title) > 0:
book['subtitle'] = ''.join(map(lambda x: x.text, entry.dc_title[1:]))
isbn = find_isbn(dic['identifiers'])
if isbn: if isbn:
book['isbn'] = isbn book['isbn'] = unicode(isbn)
elif indata: elif indata:
if len(indata) == 13: if len(indata) == 13:
book['isbn'] = indata book['isbn'] = unicode(indata)
else: else:
print "No ISBN found" comment("No ISBN found.")
return False return False
book['description'] = find_description(dic) book['title'] = unicode(entry.dc_title[0].text)
set_value(book, dic, 'category')
if len(entry.dc_title) > 0:
book['subtitle'] = unicode(''.join(map(lambda x: x.text, entry.dc_title[1:])))
authors = build_authors(dic)
book['persons'] = {}
book['persons']['author'] = [author['id'] for author in authors]
if 'publishers' in dic:
book['publisher'] = unicode(','.join(dic['publishers']))
if 'date' in dic: if 'date' in dic:
book['published_year'] = int(dic['date'][:4]) book['published_year'] = int(dic['date'][:4])
if 'publishers' in dic: set_value(book, dic, 'edition')
book['publisher'] = ','.join(dic['publishers'])
book['num_pages'] = find_page_number(dic) book['num_pages'] = find_page_number(dic)
book['persons'] = {} set_value(book, dic, 'series')
book['persons']['author'] = build_authors(dic)
book['description'] = unicode(find_description(dic))
book['references'] = {} book['references'] = {}
book['references']['google'] = dic['preview'] book['references']['google-books'] = [unicode(dic['preview'])]
keys_to_add = ['edition', 'series', 'category', 'thumbnail', 'picture'] global comments
for key in keys_to_add: print comments
set_value(book, dic, key) book['comment'] = comments
comments = ''
return book return book
def comment(comm):
global comments
comments += u'%s\n' % comm
def set_value(book, dic, key): def set_value(book, dic, key):
if key in dic: if key in dic:
book[key] = dic[key] book[key] = unicode(dic[key])
else: else:
book[key] = '' book[key] = None
def find_description(dic): def find_description(dic):
@ -118,46 +139,59 @@ def build_authors(dictionary):
author_list.append(get_or_create_author(author)) author_list.append(get_or_create_author(author))
return author_list return author_list
else: else:
print "No authors found" comment("No authors found.")
return [] return []
def get_or_create_author(author_name): def get_or_create_author(author_name):
print "Processing author: ", author_name # print "Processing author: ", author_name
global action_list
author = {}
author['action'] = 'new-person'
names = author_name.split() names = author_name.split()
first = ' '.join(names[:-1]) first = ' '.join(names[:-1])
last = names[-1] last = names[-1]
author['first_name'] = first
author['last_name'] = last
candidates = Person.objects.filter(first_name__contains=names[0],last_name=last) candidates = Person.objects.filter(first_name__contains=names[0],last_name=last)
# comment("Authors found: %s" % (', '.join(str(v) for v in )))
selected = select_from_list(candidates, attributes=['first_name','last_name'], item_name='author') selected = select_from_list(candidates, attributes=['first_name','last_name'], item_name='author')
if selected: if selected:
return selected return selected
else: else:
print "No author found, creating author" # print "No author found, creating author"
comment("No author found, creating author.")
newid = ''.join([i[0] for i in names]).lower() newid = ''.join([i[0] for i in names]).lower()
print newid author['id'] = newid
# print newid
while True: while True:
existing = Person.objects.filter(id=newid) existing = Person.objects.filter(id=newid)
if len(existing)==0: if len(existing)==0:
return Person(first_name=first, last_name=last, id=newid) action_list.append(author)
newid = raw_input("Another authour found with same intials, please suggest an id> ") return author #Person(first_name=first, last_name=last, id=newid)
comment("Another author found with same initials, please fix id.")
newid = newid+"%f" % random.random()
author['id'] = newid
# newid = raw_input("Another author found with same intials, please suggest an id> ")
#Cargo-cult coded function to unescape special XML characters #Cargo-cult coded function to unescape special XML characters
def select_from_list(list, attributes=False, item_name=""): def select_from_list(list, attributes=False, item_name=""):
if len(list) == 0: if len(list) == 0:
print "No candidate %sfound" %(item_name+' ') # print "No candidate %sfound" %(item_name+' ')
return None return None
elif len(list) == 1: elif len(list) == 1:
if attributes: # if attributes:
answer = raw_input(str("Found one %s: %s. Use this? [y]/n> " %(item_name,' '.join([eval("list[0]."+attribute) for attribute in attributes])))) # answer = raw_input(str("Found one %s: %s. Use this? [y]/n> " %(item_name,' '.join([eval("list[0]."+attribute) for attribute in attributes]))))
else: # else:
answer = raw_input(str("Found one %s: %s. Use this? [y]/n> " %(item_name,list[0]))) # answer = raw_input(str("Found one %s: %s. Use this? [y]/n> " %(item_name,list[0])))
if answer in ['yes', 'y', '']: # if answer in ['yes', 'y', '']:
# return list[0]
# else:
# return None
return list[0] return list[0]
else: else:
return None # print "several candidates found"
else:
print "several candidates found"
return None return None
def unescape(s): def unescape(s):