github_activity.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. from pelican import signals
  11. class GitHubActivity():
  12. """
  13. A class created to fetch github activity with feedparser
  14. """
  15. def __init__(self, generator):
  16. try:
  17. import feedparser
  18. self.activities = feedparser.parse(
  19. generator.settings['GITHUB_ACTIVITY_FEED'])
  20. except ImportError:
  21. raise Exception("Unable to find feedparser")
  22. def fetch(self):
  23. """
  24. returns a list of html snippets fetched from github actitivy feed
  25. """
  26. entries = []
  27. for activity in self.activities['entries']:
  28. entries.append(
  29. [element for element in [activity['title'],
  30. activity['content'][0]['value']]])
  31. return entries
  32. def fetch_github_activity(gen, metadata):
  33. """
  34. registered handler for the github activity plugin
  35. it puts in generator.context the html needed to be displayed on a
  36. template
  37. """
  38. if 'GITHUB_ACTIVITY_FEED' in gen.settings.keys():
  39. gen.context['github_activity'] = gen.plugin_instance.fetch()
  40. def feed_parser_initialization(generator):
  41. """
  42. Initialization of feed parser
  43. """
  44. generator.plugin_instance = GitHubActivity(generator)
  45. def register():
  46. """
  47. Plugin registration
  48. """
  49. signals.article_generator_init.connect(feed_parser_initialization)
  50. signals.article_generate_context.connect(fetch_github_activity)