github_activity.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. # NEEDS WORK
  3. """
  4. Copyright (c) Marco Milanesi <kpanic@gnufunk.org>
  5. Github Activity
  6. ---------------
  7. A plugin to list your Github Activity
  8. """
  9. from __future__ import unicode_literals, print_function
  10. import logging
  11. logger = logging.getLogger(__name__)
  12. from pelican import signals
  13. class GitHubActivity():
  14. """
  15. A class created to fetch github activity with feedparser
  16. """
  17. def __init__(self, generator):
  18. import feedparser
  19. self.activities = feedparser.parse(
  20. generator.settings['GITHUB_ACTIVITY_FEED'])
  21. def fetch(self):
  22. """
  23. returns a list of html snippets fetched from github actitivy feed
  24. """
  25. entries = []
  26. for activity in self.activities['entries']:
  27. entries.append(
  28. [element for element in [activity['title'],
  29. activity['content'][0]['value']]])
  30. return entries
  31. def fetch_github_activity(gen, metadata):
  32. """
  33. registered handler for the github activity plugin
  34. it puts in generator.context the html needed to be displayed on a
  35. template
  36. """
  37. if 'GITHUB_ACTIVITY_FEED' in gen.settings.keys():
  38. gen.context['github_activity'] = gen.plugin_instance.fetch()
  39. def feed_parser_initialization(generator):
  40. """
  41. Initialization of feed parser
  42. """
  43. generator.plugin_instance = GitHubActivity(generator)
  44. def register():
  45. """
  46. Plugin registration
  47. """
  48. try:
  49. signals.article_generator_init.connect(feed_parser_initialization)
  50. signals.article_generate_context.connect(fetch_github_activity)
  51. except ImportError:
  52. logger.warning('`github_activity` failed to load dependency `feedparser`.'
  53. '`github_activity` plugin not loaded.')