github_activity.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. self.max_entries = generator.settings['GITHUB_ACTIVITY_MAX_ENTRIES']
  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[0:self.max_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. try:
  50. signals.article_generator_init.connect(feed_parser_initialization)
  51. signals.article_generator_context.connect(fetch_github_activity)
  52. except ImportError:
  53. logger.warning('`github_activity` failed to load dependency `feedparser`.'
  54. '`github_activity` plugin not loaded.')