-
-
Save alfcrisci/5642705 to your computer and use it in GitHub Desktop.
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 getTweets(searchTerm, maxResults, sinceid, languageCode) { | |
//Based on Mikael Thuneberg getTweets - mod by mhawksey to convert to json | |
// if you include setRowsData this can be used to output chosen entries | |
var data = []; | |
var idx = 0; | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sumSheet = ss.getSheetByName("Readme/Settings"); | |
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" | |
}; | |
} else { | |
Browser.msgBox("Twitter API Configuration Required") | |
} | |
try { | |
var max_id = ""; | |
var max_id_url = ""; | |
var page = 1; | |
var done = false; | |
var params = {q: searchTerm, | |
count: 100, | |
result_type: "recent", | |
include_entities: true, | |
with_twitter_user_id: true}; | |
if (sinceid != undefined) { | |
params.since_id = sinceid; | |
} | |
var SEARCH_DURATION = sumSheet.getRange("B11").getValue(); | |
// prepare search term | |
if (SEARCH_DURATION != "default"){ | |
switch (SEARCH_DURATION){ | |
case "yesterday": | |
period = 0; | |
break; | |
case "-2 days": | |
period = 1; | |
break; | |
case "-3 days": | |
period = 2; | |
break; | |
case "-4 days": | |
period = 3; | |
break; | |
case "-5 days": | |
period = 4; | |
break; | |
case "-6 days": | |
period = 5; | |
break; | |
case "-7 days": | |
period = 6; | |
break; | |
} | |
var until=new Date(); | |
until.setDate(until.getDate()-period); | |
var since = new Date(until); | |
since.setDate(since.getDate()-1-period); | |
params.since = twDate(since); | |
params.until = twDate(until); | |
} | |
if (languageCode != undefined) { | |
params.lang= languageCode; | |
} | |
var baseUrl = "https://api.twitter.com/1.1/search/tweets.json"; | |
var url = buildUrl("", params); // make url | |
while(!done){ | |
var response = UrlFetchApp.fetch(baseUrl+url+max_id_url, requestData); | |
if (response.getResponseCode() == 200) { | |
var responseData = Utilities.jsonParse(response.getContentText()); | |
var objects = responseData.statuses; | |
if (objects.length>0){ // if data returned | |
for (i in objects){ // for the data returned we put in montly bins ready for writting/updating files | |
if(objects[i].id_str != max_id && objects[i].user.followers_count > 0){ | |
if (objects[i].geo != null){ | |
objects[i]["geo_coordinates"] = "loc: "+objects[i].geo.coordinates[0]+","+objects[i].geo.coordinates[1]; | |
} | |
for (j in objects[i].user){ | |
objects[i]["user_"+j] = objects[i].user[j]; | |
} | |
objects[i]["from_user"] = objects[i]["user_screen_name"]; | |
objects[i]["from_user_id_str"] = objects[i]["user_id_str"] | |
objects[i]["profile_image_url"] = objects[i]["user_profile_image_url"]; | |
objects[i]["status_url"] = "http://twitter.com/"+objects[i].user_screen_name+"/statuses/"+objects[i].id_str; | |
objects[i]["time"] = new Date(objects[i]["created_at"]); | |
objects[i]["entities_str"] = Utilities.jsonStringify(objects[i]["entities"]); | |
data[idx]=objects[i]; | |
idx ++; | |
} | |
} | |
if (responseData.search_metadata.next_results != undefined) { | |
url = responseData.search_metadata.next_results; | |
} else { | |
done = true; | |
} | |
} else { // if not data break the loop | |
done = true; | |
} | |
} | |
page ++; | |
if (page > 15) done = true; // if collected 16 pages (the max) break the loop | |
} | |
return data; | |
} catch (e) { | |
Logger.log("Line "+e.lineNumber+" "+e.message+e.name); | |
//Browser.msgBox("Line "+e.lineNumber+" "+e.message+e.name); | |
ScriptProperties.setProperty("errormsg","Line "+e.lineNumber+" "+e.message+e.name); | |
return data; | |
} | |
} | |
/** | |
* Build a querystring from a object http://stackoverflow.com/a/5340658/1027723 | |
* @param {String} base url. | |
* @param {Object} objects to add to string. | |
* @return {String} url. | |
*/ | |
function buildUrl(url, parameters){ | |
var qs = ""; | |
for(var key in parameters) { | |
var value = parameters[key]; | |
qs += encodeURIComponent(key) + "=" + encodeURIComponent(value) + "&"; | |
} | |
if (qs.length > 0){ | |
qs = qs.substring(0, qs.length-1); //chop off last "&" | |
url = url + "?" + qs; | |
} | |
return url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment