goodreads_activity.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. """
  3. Goodreads Activity
  4. ==================
  5. A Pelican plugin to lists books from your Goodreads shelves.
  6. Copyright (c) Talha Mansoor
  7. """
  8. from __future__ import unicode_literals
  9. import logging
  10. logger = logging.getLogger(__name__)
  11. from pelican import signals
  12. class GoodreadsActivity():
  13. def __init__(self, generator):
  14. import feedparser
  15. self.activities = feedparser.parse(
  16. generator.settings['GOODREADS_ACTIVITY_FEED'])
  17. def fetch(self):
  18. goodreads_activity = {
  19. 'shelf_title': self.activities.feed.title,
  20. 'books': []
  21. }
  22. for entry in self.activities['entries']:
  23. book = {
  24. 'title': entry.title,
  25. 'author': entry.author_name,
  26. 'link': entry.link,
  27. 'l_cover': entry.book_large_image_url,
  28. 'm_cover': entry.book_medium_image_url,
  29. 's_cover': entry.book_small_image_url,
  30. 'description': entry.book_description,
  31. 'rating': entry.user_rating,
  32. 'review': entry.user_review,
  33. 'tags': entry.user_shelves
  34. }
  35. goodreads_activity['books'].append(book)
  36. return goodreads_activity
  37. def fetch_goodreads_activity(gen, metadata):
  38. if 'GOODREADS_ACTIVITY_FEED' in gen.settings:
  39. gen.context['goodreads_activity'] = gen.goodreads.fetch()
  40. def initialize_feedparser(generator):
  41. generator.goodreads = GoodreadsActivity(generator)
  42. def register():
  43. try:
  44. signals.article_generator_init.connect(initialize_feedparser)
  45. signals.article_generate_context.connect(fetch_goodreads_activity)
  46. except ImportError:
  47. logger.warning('`goodreads_activity` failed to load dependency `feedparser`.'
  48. '`goodreads_activity` plugin not loaded.')