disqus_static.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 disqusapi import DisqusAPI, Paginator
  9. from pelican import signals
  10. def initialized(pelican):
  11. from pelican.settings import _DEFAULT_CONFIG
  12. _DEFAULT_CONFIG.setdefault('DISQUS_SECRET_KEY', '')
  13. _DEFAULT_CONFIG.setdefault('DISQUS_PUBLIC_KEY', '')
  14. if pelican:
  15. pelican.settings.setdefault('DISQUS_SECRET_KEY', '')
  16. pelican.settings.setdefault('DISQUS_PUBLIC_KEY', '')
  17. def disqus_static(generator):
  18. disqus = DisqusAPI(generator.settings['DISQUS_SECRET_KEY'],
  19. generator.settings['DISQUS_PUBLIC_KEY'])
  20. # first retrieve the threads
  21. threads = Paginator(disqus.threads.list,
  22. forum=generator.settings['DISQUS_SITENAME'])
  23. # build a {thread_id: title} dict
  24. thread_dict = {}
  25. for thread in threads:
  26. thread_dict[thread['id']] = thread['title']
  27. # now retrieve the posts
  28. posts = Paginator(disqus.posts.list,
  29. forum=generator.settings['DISQUS_SITENAME'])
  30. # build a {post_id: [child_post1, child_post2, ...]} dict
  31. child_dict = {}
  32. for post in posts:
  33. if post['id'] not in child_dict.keys():
  34. child_dict[post['id']] = []
  35. if post['parent'] is not None:
  36. if str(post['parent']) not in child_dict.keys():
  37. child_dict[str(post['parent'])] = []
  38. child_dict[str(post['parent'])].append(post)
  39. # build a {title: [post1, post2, ...]} dict
  40. post_dict = {}
  41. for post in posts:
  42. build_post_dict(post_dict, child_dict, thread_dict, post)
  43. for article in generator.articles:
  44. if article.title in post_dict:
  45. article.disqus_comments = post_dict[article.title]
  46. article.disqus_comment_count = sum([
  47. postcounter(post) for post in post_dict[article.title]])
  48. def postcounter(node):
  49. return 1 + sum([postcounter(n) for n in node['children']])
  50. def build_post_dict(post_dict, child_dict, thread_dict, post):
  51. if post['thread'] not in thread_dict.keys():
  52. return # invalid thread, should never happen
  53. build_child_dict(child_dict, post)
  54. if post['parent'] is not None:
  55. return # this is a child post, don't want to display it here
  56. if thread_dict[post['thread']] not in post_dict.keys():
  57. post_dict[thread_dict[post['thread']]] = []
  58. post_dict[thread_dict[post['thread']]].append(post)
  59. def build_child_dict(child_dict, post):
  60. post['children'] = child_dict[post['id']]
  61. for child in child_dict[post['id']]:
  62. build_child_dict(child_dict, child)
  63. def register():
  64. signals.initialized.connect(initialized)
  65. signals.article_generator_finalized.connect(disqus_static)