public
Created

Filtered questions for TAGS script

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/*
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.