Skip to content

Instantly share code, notes, and snippets.

@julianh2o
Created March 23, 2016 04:43
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 julianh2o/b74484a4e2243ea4bda9 to your computer and use it in GitHub Desktop.
Save julianh2o/b74484a4e2243ea4bda9 to your computer and use it in GitHub Desktop.
A function for Google Sheets that automatically fetches the number of subscribers that a subreddit has. Simply pass in the name of the subreddit.
// Installation: On a Google Sheet, go to Tools > Script Editor and paste the code into the resulting editor.
// Save and return to your editor and use as follows:
// Usage: getSubredditSubscribers("funny")
// Supports subreddits in the following forms:
// programming
// http://www.reddit.com/r/electronics
//Refresh key can be used for force a refresh by including an arbitrary number
function getSubredditSubscribers(input,refreshkey) {
if (input == undefined) {
input = ["babyelephantgifs"];
Logger.log("debug mode");
}
if (!Array.isArray(input)) {
input = [input];
}
var retarray = [];
for (var i=0; i<input.length; i++) {
var subreddit = input[i];
var url = subreddit;
if (!subreddit.indexOf("http") == 0) {
url = "https://www.reddit.com/r/"+subreddit;
}
if (url[url.length-1] == "/") url = url.substring(0,url.length-1);
url = url + "/about.json";
var content = UrlFetchApp.fetch(url).getContentText();
var data = JSON.parse(content);
var subscribers = data['data']['subscribers'];
retarray.push(subscribers);
}
Logger.log(retarray);
return retarray.length == 1 ? retarray[0] : retarray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment