disqus comment count widget for dashing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<li data-row="1" data-col="1" data-sizex="1" data-sizey="1"> | |
<div data-id="disqus_comments" data-view="Number" data-title="Comments" data-moreinfo="This Month" style="background-color:#427EB4;"></div> | |
<i class="icon-comments icon-background"></i> | |
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'json' | |
# See http://disqus.com/api/docs/ | |
module DisqusAPI | |
class Base | |
include HTTParty | |
format :json | |
base_uri 'http://disqus.com/api/3.0' | |
#if you need to debug something | |
#debug_output $stderr | |
end | |
class Post < Base | |
# See http://disqus.com/api/docs/posts/list/ | |
def count_since (since) | |
cursor = '' | |
count = 0 | |
begin | |
url = "/posts/list.json?api_secret=#{API_SECRET}&since=#{since}&limit=100&order=asc" | |
# first one doesn't have a cursor pointer | |
if cursor | |
url << "&cursor=#{cursor}" | |
end | |
result = self.class.get(url) | |
json = JSON.parse(result.body) | |
# if there is a cursor, get it | |
if !json['cursor']['next'].nil? | |
cursor = json['cursor']['next'] | |
end | |
# count the comments | |
count += json['response'].count | |
end while json['cursor']['hasNext'] == true | |
return count | |
end | |
end | |
end | |
class Disqus | |
include DisqusAPI | |
end | |
# Secret key from http://disqus.com/api/applications/ | |
DisqusAPI::API_SECRET = '...' | |
SCHEDULER.every '60m', :first_in => 10 do | |
disqus = Disqus::Post.new | |
#first of the month | |
timestamp = Date.new(Date.today.year, Date.today.month, 1 ).to_time.to_i | |
send_event('disqus_comments', { current: disqus.count_since(timestamp) }) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment