From 9d3d77cbf6d7595ec2c94bda41893732a9fd91ca Mon Sep 17 00:00:00 2001 From: almelid Date: Thu, 23 Sep 2010 17:04:28 +0000 Subject: [PATCH] faset over til google books og gdata --- python/google_interface.py | 39 +++++++++++++++++++++++++++++++++++- python/web/library/models.py | 7 +++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/python/google_interface.py b/python/google_interface.py index 505d5bb..74664fa 100644 --- a/python/google_interface.py +++ b/python/google_interface.py @@ -1,3 +1,5 @@ +#!/usr/bin/python + from web.library.models import * from gdata.books.service import BookService @@ -13,6 +15,41 @@ def get_book_loop(): if len(feed.entry) == 0: print "No items found" elif len(feed.entry) == 1: - print "Found one item: "+feed.entry[0].title.text + 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']) + print entry.format + b.full_print() + +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() diff --git a/python/web/library/models.py b/python/web/library/models.py index 3840589..eb592ed 100644 --- a/python/web/library/models.py +++ b/python/web/library/models.py @@ -22,7 +22,7 @@ class Reference(models.Model): class Book(models.Model): isbn = models.CharField(max_length=13, primary_key=True) - id = models.CharField(max_length=255, unique=True) + id = models.CharField(max_length=255) title = models.CharField(max_length=511) subtitle = models.CharField(max_length=511, null=True, blank=True) category = models.ForeignKey(Category) @@ -38,7 +38,10 @@ class Book(models.Model): def full_print(self): for field in book_fields.items(): - print '%15s: %-30s' % (field[1],eval('self.'+field[0])) + try: + print '%-15s: %50s' % (field[1],eval('self.'+field[0])) + except Category.DoesNotExist: + print '%-15s: %50s' % (field[1], 'Does not exist') class AlternativeTitle(models.Model): book = models.ForeignKey(Book)