Displaying some of the books from user shelf.

pull/1/head
Raymond Yee 2011-10-25 16:07:44 -07:00
parent 2f81b8cebd
commit a4dd33b047
4 changed files with 63 additions and 46 deletions

View File

@ -142,27 +142,38 @@ class GoodreadsClient(object):
request_url = urljoin(GoodreadsClient.url, path) request_url = urljoin(GoodreadsClient.url, path)
# loop through all the pages starting with page
more_pages = True
while (more_pages):
r = request(method,request_url,params=params) r = request(method,request_url,params=params)
if r.status_code != httplib.OK: if r.status_code != httplib.OK:
logger.info('headers, content: %s | %s ' % (r.headers,r.content)) #logger.info('headers, content: %s | %s ' % (r.headers,r.content))
raise GoodreadsException('Error in review_list: %s %s ' % (r.headers, r.content)) raise GoodreadsException('Error in review_list: %s %s ' % (r.headers, r.content))
else: else:
logger.info('headers, content: %s | %s ' % (r.headers,r.content)) #logger.info('headers, content: %s | %s ' % (r.headers,r.content))
doc = ET.fromstring(r.content) doc = ET.fromstring(r.content)
# for the moment convert to a iterable of book data presented as dict -- one the way to paging through all results # for the moment convert to a iterable of book data presented as dict -- one the way to paging through all results
reviews = doc.findall('reviews/review') reviews = doc.findall('reviews/review')
for review in reviews: for review in reviews:
yield ({'id':review.find('id').text, yield ({'id':review.find('id').text,
'book': {'id': review.find('book/id').text, 'book': {'id': review.find('book/id').text.strip(),
'isbn13':review.find('book/isbn13').text, 'isbn13':review.find('book/isbn13').text,
'title':review.find('book/title').text, 'title':review.find('book/title').text.strip(),
'text_reviews_count':review.find('book/text_reviews_count').text, 'text_reviews_count':review.find('book/text_reviews_count').text.strip(),
'link':review.find('book/link').text, 'link':review.find('book/link').text.strip(),
'small_image_url':review.find('book/small_image_url').text, 'small_image_url':review.find('book/small_image_url').text.strip(),
'ratings_count':review.find('book/ratings_count').text, 'ratings_count':review.find('book/ratings_count').text.strip(),
'description':review.find('book/description').text} 'description':review.find('book/description').text.strip()}
}) })
if len(reviews) == 0:
more_pages = False
else:
params["page"] += 1
def shelves_list(self,user_id,page=1): def shelves_list(self,user_id,page=1):
path = "/shelf/list.xml" path = "/shelf/list.xml"

View File

@ -21,15 +21,20 @@
{% endif %} {% endif %}
{% if shelves_info %} {% if shelves_info %}
<p>You have {{shelves_info.total}} shelves. </p> <p>You have {{shelves_info.total}} shelves. </p>
{% if shelves_info.user_shelves %}
<ul>
{% for shelf in shelves_info.user_shelves %} {% for shelf in shelves_info.user_shelves %}
<p>{{shelf.name}}: {{shelf.book_count}} book(s)</p> <li>{{shelf.name}}: {{shelf.book_count}} book(s)</li>
{% endfor %} {% endfor %}
</ul>
{% endif %}
{% endif %} {% endif %}
<!-- list user's books --> <!-- list user's books -->
{% if reviews %} {% if reviews %}
<p>Here are some of your books that you've put on your Goodreads shelves:</p>
<ul id="id"> <ul id="id">
{% for review in reviews %} {% for review in reviews|slice:":50" %}
<li>{{review.book.title}}</li> <li><img src="{{review.book.small_image_url}}"/><a href="{{review.book.link}}">{{review.book.title}}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}

View File

@ -15,6 +15,7 @@ from django.contrib.auth.decorators import login_required
from django.shortcuts import render, render_to_response, get_object_or_404 from django.shortcuts import render, render_to_response, get_object_or_404
import oauth2 as oauth import oauth2 as oauth
from itertools import islice
from regluit.core import tasks from regluit.core import tasks
from regluit.core import models, bookloader from regluit.core import models, bookloader
@ -251,7 +252,7 @@ class GoodreadsDisplayView(TemplateView):
# if we have a userid, grab info about the book shelves and books # if we have a userid, grab info about the book shelves and books
if session.get("goodreads_userid") is not None: if session.get("goodreads_userid") is not None:
context["shelves_info"] = gr_client.shelves_list(user_id=session["goodreads_userid"]) context["shelves_info"] = gr_client.shelves_list(user_id=session["goodreads_userid"])
context["reviews"] = gr_client.review_list(user_id=session["goodreads_userid"],per_page=10) context["reviews"] = list(islice(gr_client.review_list(user_id=session["goodreads_userid"],per_page=50),50))
# also, now grab the books on the user's shelves # also, now grab the books on the user's shelves