summaryrefslogtreecommitdiff
path: root/plugins/disqus_static/disqus_static.py
blob: 6fb087acef29f52eeb227a17ece5d2ac6e48d07c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
"""
Disqus static comment plugin for Pelican
====================================
This plugin adds a disqus_comments property to all articles.          
Comments are fetched at generation time using disqus API.
"""

from __future__ import unicode_literals
from disqusapi import DisqusAPI, Paginator
from pelican import signals

def initialized(pelican):
    from pelican.settings import DEFAULT_CONFIG
    DEFAULT_CONFIG.setdefault('DISQUS_SECRET_KEY', '')
    DEFAULT_CONFIG.setdefault('DISQUS_PUBLIC_KEY', '')
    if pelican:
        pelican.settings.setdefault('DISQUS_SECRET_KEY', '')
        pelican.settings.setdefault('DISQUS_PUBLIC_KEY', '')

def disqus_static(generator):
    disqus = DisqusAPI(generator.settings['DISQUS_SECRET_KEY'], 
                       generator.settings['DISQUS_PUBLIC_KEY'])
    # first retrieve the threads
    threads = Paginator(disqus.threads.list, 
                        forum=generator.settings['DISQUS_SITENAME'])
    # build a {thread_id: title} dict
    thread_dict = {}
    for thread in threads:
        thread_dict[thread['id']] = thread['title']

    # now retrieve the posts
    posts = Paginator(disqus.posts.list, 
                      forum=generator.settings['DISQUS_SITENAME'])

    # build a {post_id: [child_post1, child_post2, ...]} dict
    child_dict = {}
    for post in posts:
        if post['id'] not in child_dict.keys():
            child_dict[post['id']] = []
        if post['parent'] is not None:
            if str(post['parent']) not in child_dict.keys():
                child_dict[str(post['parent'])] = []
            child_dict[str(post['parent'])].append(post)

    # build a {title: [post1, post2, ...]} dict
    post_dict = {}
    for post in posts:
        build_post_dict(post_dict, child_dict, thread_dict, post)

    for article in generator.articles:
        if article.title in post_dict:
            article.disqus_comments = post_dict[article.title]
            article.disqus_comment_count = sum([
                postcounter(post) for post in post_dict[article.title]])

def postcounter(node):
    return 1 + sum([postcounter(n) for n in node['children']])

def build_post_dict(post_dict, child_dict, thread_dict, post):
    if post['thread'] not in thread_dict.keys():
        return # invalid thread, should never happen

    build_child_dict(child_dict, post)

    if post['parent'] is not None:
        return # this is a child post, don't want to display it here

    if thread_dict[post['thread']] not in post_dict.keys():
        post_dict[thread_dict[post['thread']]] = []
    post_dict[thread_dict[post['thread']]].append(post)

def build_child_dict(child_dict, post):
    post['children'] = child_dict[post['id']]
    for child in child_dict[post['id']]:
        build_child_dict(child_dict, child)

def register():
    signals.initialized.connect(initialized)
    signals.article_generator_finalized.connect(disqus_static)