66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
#!/usr/bin/python
|
|
|
|
from web.library.models import *
|
|
from gdata.books.service import BookService
|
|
import re
|
|
|
|
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:
|
|
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!"
|
|
|
|
def build_book(entry, input=False):
|
|
dic = entry.to_dict()
|
|
print dic
|
|
# print entry
|
|
b = 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
|
|
else:
|
|
print "No ISBN found"
|
|
return False
|
|
if 'description' in dic:
|
|
b.description = dic['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)
|
|
b.full_print()
|
|
|
|
def find_page_number(dic):
|
|
if 'format' in dic:
|
|
for item in dic['format']:
|
|
if 'pages' in item:
|
|
return int(re.findall(r'[0-9]+',item)[0])
|
|
return None
|
|
else:
|
|
return None
|
|
|
|
def find_isbn(identifiers):
|
|
for pair in identifiers:
|
|
if pair[0] =='ISBN' and len(pair[1])==13:
|
|
return pair[1]
|
|
return False
|
|
|
|
|
|
get_book_loop()
|