Created
March 1, 2013 21:58
-
-
Save sarfata/5068200 to your computer and use it in GitHub Desktop.
Google Script example on how to get the total number of comments for a disus forum
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
function disqusComments(cursor) { | |
var api_key = "YOUR_API_KEY"; | |
var forum = "YOUR_FORUM"; | |
var url = "https://disqus.com/api/3.0/forums/listThreads.json?api_key=" + api_key + "&forum=" + forum; //+ "&limit=100"; | |
if (cursor) { | |
url = url + "&cursor=" + cursor; | |
} | |
try | |
{ | |
Logger.log("Fetching data from: " + url); | |
var response = UrlFetchApp.fetch(url); | |
} | |
catch (e) | |
{ | |
throw "Disqus API error: " + e; | |
} | |
if (response.getResponseCode() != 200) | |
throw "Unexpected response code from Disqus."; | |
var responseText = response.getContentText(); | |
if (responseText == null || responseText == "") | |
throw "Empty response from Disqus:"; | |
var data = Utilities.jsonParse(responseText); | |
var threads = data['response']; | |
var totalComments = 0; | |
for (var i = 0; i < threads.length; i++) { | |
totalComments += threads[i]['posts']; | |
} | |
Logger.log("Total number of disqus comments: " + totalComments) | |
if (data['cursor']['hasNext']) { | |
totalComments += disqusComments(data['cursor']['next']); | |
} | |
return totalComments; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wha'ts the forum here? Is it the site name?