Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Created October 9, 2012 21:09
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 mhawksey/3861455 to your computer and use it in GitHub Desktop.
Save mhawksey/3861455 to your computer and use it in GitHub Desktop.
Filtered questions for TAGS script
/*
To use Filter Questions Interface
1. Run > setup twice (first time to authorise, second to fun the function)
2. File > Manage Versions and enter any description you like and Save New Version
3. Publish > Deploy as web app... and click Update
4. Run > getUrl and then open View > Logs... and copy the url into your browser address bar
*/
function setup(){
ScriptProperties.setProperty('active', SpreadsheetApp.getActiveSpreadsheet().getId());
var svc = ScriptApp.getService();
if (!svc.isEnabled()) {
// it's not enabled, and should be
svc.enable(svc.Restriction.ALL);
}
}
function getUrl(){
var serviceUrl = ScriptApp.getService().getUrl();
var title = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Readme/Settings").getRange("B10").getValue();
Logger.log("http://hawksey.info/labs/tweetQ.html?url="+serviceUrl+"&title="+encodeURIComponent(title));
}
function getReplies(sourceArray, replyArray){
var output= [];
for (i in sourceArray){
output.push([alltrim(sourceArray[i][0])]);
for (j in replyArray){
if (alltrim(sourceArray[i][0])==replyArray[j][1]) {
output.pop();
output.push([replyArray[j][0]]);
}
}
}
return output;
}
function doGet(e){
var col = e.parameter.col;
var output = ContentService.createTextOutput();
if (col == undefined){
var jsonData = tweetEmbeds(1);
} else {
var jsonData = tweetEmbeds(col);
}
output.setMimeType(ContentService.MimeType.JSON);
output.setContent("parseResponse("+JSON.stringify(jsonData)+")");
return output;
}
function tweetEmbeds(col) {
var col = parseInt(col);
var doc = SpreadsheetApp.openById(ScriptProperties.getProperty('active'));
var sheet = doc.getSheetByName("questionsFilter");
var data = sheet.getRange(2, col, sheet.getLastRow()).getValues();
var output = {};
for (i in data){
var id = alltrim(data[i][0]);
if (id != "") output[id] = tweetEmbedCode(id);
}
return output;
}
function tweetEmbedCode(id) {
if (isConfigured()){
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
oauthConfig.setAccessTokenUrl(
"https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl(
"https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl(
"https://api.twitter.com/oauth/authorize");
oauthConfig.setConsumerKey(getConsumerKey());
oauthConfig.setConsumerSecret(getConsumerSecret());
var requestData = {
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
}
try {
var cache = CacheService.getPublicCache(); // using Cache service to prevent too many urlfetch
var cached = cache.get("tweet-embed"+id);
if (cached != null) { // if value in cache return it
return cached;
}
var response = UrlFetchApp.fetch("https://api.twitter.com/1/statuses/oembed.json?omit_script=true&align=cener&id="+id+"f", requestData);
Utilities.sleep(1000);
var contentHeader = response.getHeaders();
if (response.getResponseCode() == 200) {
var data = Utilities.jsonParse(response.getContentText());
if (response.getResponseCode() == 200) {
var html = data.html;
if (html){
cache.put("tweet-embed"+id, html, 86400); // cache for a day
return html;
}
}
}
}
catch(e){
Logger.log("Line "+e.lineNumber+" "+e.message+e.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment