disqus_static.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. """
  3. Disqus static comment plugin for Pelican
  4. ====================================
  5. This plugin adds a disqus_comments property to all articles.
  6. Comments are fetched at generation time using disqus API.
  7. """
  8. from __future__ import unicode_literals
  9. from disqusapi import DisqusAPI, Paginator
  10. from pelican import signals
  11. def initialized(pelican):
  12. from pelican.settings import DEFAULT_CONFIG
  13. DEFAULT_CONFIG.setdefault('DISQUS_SECRET_KEY', '')
  14. DEFAULT_CONFIG.setdefault('DISQUS_PUBLIC_KEY', '')
  15. if pelican:
  16. pelican.settings.setdefault('DISQUS_SECRET_KEY', '')
  17. pelican.settings.setdefault('DISQUS_PUBLIC_KEY', '')
  18. def disqus_static(generator):
  19. disqus = DisqusAPI(generator.settings['DISQUS_SECRET_KEY'],
  20. generator.settings['DISQUS_PUBLIC_KEY'])
  21. # first retrieve the threads
  22. threads = Paginator(disqus.threads.list,
  23. forum=generator.settings['DISQUS_SITENAME'])
  24. # build a {thread_id: title} dict
  25. thread_dict = {}
  26. for thread in threads:
  27. thread_dict[thread['id']] = thread['title']
  28. # now retrieve the posts
  29. posts = Paginator(disqus.posts.list,
  30. forum=generator.settings['DISQUS_SITENAME'])
  31. # build a {post_id: [child_post1, child_post2, ...]} dict
  32. child_dict = {}
  33. for post in posts:
  34. if post['id'] not in child_dict.keys():
  35. child_dict[post['id']] = []
  36. if post['parent'] is not None:
  37. if str(post['parent']) not in child_dict.keys():
  38. child_dict[str(post['parent'])] = []
  39. child_dict[str(post['parent'])].append(post)
  40. # build a {title: [post1, post2, ...]} dict
  41. post_dict = {}
  42. for post in posts:
  43. build_post_dict(post_dict, child_dict, thread_dict, post)
  44. for article in generator.articles:
  45. if article.title in post_dict:
  46. article.disqus_comments = post_dict[article.title]
  47. article.disqus_comment_count = sum([
  48. postcounter(post) for post in post_dict[article.title]])
  49. def postcounter(node):
  50. return 1 + sum([postcounter(n) for n in node['children']])
  51. def build_post_dict(post_dict, child_dict, thread_dict, post):
  52. if post['thread'] not in thread_dict.keys():
  53. return # invalid thread, should never happen
  54. build_child_dict(child_dict, post)
  55. if post['parent'] is not None:
  56. return # this is a child post, don't want to display it here
  57. if thread_dict[post['thread']] not in post_dict.keys():
  58. post_dict[thread_dict[post['thread']]] = []
  59. post_dict[thread_dict[post['thread']]].append(post)
  60. def build_child_dict(child_dict, post):
  61. post['children'] = child_dict[post['id']]
  62. for child in child_dict[post['id']]:
  63. build_child_dict(child_dict, child)
  64. def register():
  65. signals.initialized.connect(initialized)
  66. signals.article_generator_finalized.connect(disqus_static)