Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Created November 11, 2012 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanJA/4055806 to your computer and use it in GitHub Desktop.
Save SeanJA/4055806 to your computer and use it in GitHub Desktop.
disqus comment count widget for dashing
<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>
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