Last active
August 29, 2015 14:06
-
-
Save erichelgeson/a0a0d34e195b823f7bd1 to your computer and use it in GitHub Desktop.
Twitter fav archiver Google Docs
This file contains hidden or 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
/* | |
T W I T T E R (fav) A R C H I V E R | |
- - - - - - - - - - - - - - - - - - | |
- Eric Helgeson | |
Based on work by Amit Agarwal www.ctrlq.org | |
http://lifehacker.com/twitter-archiver-saves-tweets-by-keyword-or-hashtag-in-1598972400 | |
*/ | |
var TWITTER_CONSUMER_KEY = "Yours Here"; | |
var TWITTER_CONSUMER_SECRET = "Yours here"; | |
var TWITTER_FAV_URL = "https://api.twitter.com/1.1/favorites/list.json"; | |
var TWITTER_FAV_COUNT = "60" | |
function logTweet_(sheet, tweet) { | |
var log = []; | |
var url = '=HYPERLINK("https://twitter.com/' | |
+ tweet.user.screen_name + '/status/' + tweet.id_str + '","' + tweet.user.name + '")' | |
// If there are urls, flatten them into one string. | |
var expanded_urls = ""; | |
var titles = ""; | |
for(var i=0;i<tweet.entities.urls.length;i++){ | |
if(i != 0) { | |
expanded_urls += " | "; // Seperator if more than one. | |
titles += " | "; | |
} | |
expanded_urls += tweet.entities.urls[i].expanded_url; | |
titles = _getHTMLTitle(tweet.entities.urls[i].expanded_url); | |
} | |
log.push(new Date(tweet.created_at)); | |
log.push(url); | |
log.push(tweet.user.followers_count); | |
log.push(tweet.user.friends_count); | |
log.push(tweet.retweet_count); | |
log.push(tweet.favorite_count); | |
log.push(tweet.text.replace(/\r\n|\n|\r/g, " ").replace("<", "<").replace(">", ">")); | |
log.push("{% include article.html url=\""+ expanded_urls +"\" name=\""+tweet.user.name+"\" twitter=\""+ tweet.user.screen_name +"\" title=\""+ titles +"\" content=\"\" %}"); | |
sheet.insertRowBefore(3).getRange(3,1, 1, sheet.getLastColumn()).setValues([log]); | |
} | |
/* | |
Given a url, return the HTML Title as a string. | |
https://stackoverflow.com/questions/14169449/urlfetchapp-fetch-windows-1251 | |
*/ | |
function _getHTMLTitle(url) { | |
var titles = ""; | |
try { | |
var page = UrlFetchApp.fetch(url); | |
response = page.getContentText(); | |
doc = Xml.parse(response, true); | |
var tbody = doc.html.head; | |
var title = tbody.getElements("title"); | |
titles += title[0].getText(); | |
Logger.log(titles); | |
} catch (e) { | |
// Just Log error and continue, if we cant get the title no biggie. | |
Logger.log(e.toString()); | |
} | |
return titles | |
} | |
function encodeString_(q) { | |
var str = encodeURIComponent(q); | |
str = str.replace(/!/g,'%21'); | |
str = str.replace(/\*/g,'%2A'); | |
str = str.replace(/\(/g,'%28'); | |
str = str.replace(/\)/g,'%29'); | |
str = str.replace(/'/g,'%27'); | |
return str; | |
} | |
function oAuth_() { | |
var oauthConfig = UrlFetchApp.addOAuthService("twittersearch"); | |
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(TWITTER_CONSUMER_KEY); | |
oauthConfig.setConsumerSecret(TWITTER_CONSUMER_SECRET); | |
} | |
function authorizeTwitter_() { | |
try { | |
var api = "https://api.twitter.com/1.1/application/rate_limit_status.json?resources=favorites"; | |
var options = { | |
"method": "get", | |
"oAuthServiceName":"twittersearch", | |
"oAuthUseToken":"always" | |
}; | |
oAuth_(); | |
var result = UrlFetchApp.fetch(api, options); | |
if ( result.getResponseCode() == 200 ) | |
return true; | |
} catch (e) { | |
Logger.log(e.toString()); | |
} | |
return false; | |
} | |
function Initialize() { | |
try { | |
var tweet_text, sinceID, maxID, api, sheet, search; | |
var options, result, json, tweets, tweet, sender; | |
Stop(); | |
if ( authorizeTwitter_() ) { | |
sheet = SpreadsheetApp.getActiveSheet(); | |
search = sheet.getName(); | |
options = { | |
"method": "get", | |
"oAuthServiceName":"twittersearch", | |
"oAuthUseToken":"always" | |
}; | |
oAuth_(); | |
api = TWITTER_FAV_URL + "?count=" + TWITTER_FAV_COUNT + "&screen_name=" + encodeString_(search); | |
result = UrlFetchApp.fetch(api, options); | |
if (result.getResponseCode() == 200) { | |
json = Utilities.jsonParse(result.getContentText()); | |
if (json) { | |
tweets = json; | |
setProperty_("SINCEID", tweets[0].id_str); | |
setProperty_("MAXID", tweets[tweets.length-1].id_str); | |
for (var i=tweets.length-1; i>=0; i--) { | |
logTweet_(sheet, tweets[i]); | |
} | |
} | |
} | |
ScriptApp.newTrigger("saveTweets") | |
.timeBased().everyMinutes(15).create(); | |
} | |
} catch (e) { | |
Logger.log(e.toString()); | |
} | |
} | |
function Clear() { | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
sheet.getRange("A3:H" + (sheet.getLastRow() > 3 ? sheet.getLastRow() : "3")).clear(); | |
Stop(); | |
} | |
function saveTweets() { | |
var tweet_text, sinceID, maxID, api, sheet, search; | |
var options, result, json, tweets, tweet, sender; | |
try { | |
sheet = SpreadsheetApp.getActiveSheet(); | |
search = sheet.getName(); | |
sinceID = setProperty_("SINCEID"); | |
options = { | |
"method": "get", | |
"oAuthServiceName":"twittersearch", | |
"oAuthUseToken":"always" | |
}; | |
oAuth_(); | |
api = TWITTER_FAV_URL + "?count=100&screen_name=" + encodeString_(search) + "&since_id=" + sinceID; | |
result = UrlFetchApp.fetch(api, options); | |
if (result.getResponseCode() == 200) { | |
json = Utilities.jsonParse(result.getContentText()); | |
if (json) { | |
tweets = json; | |
if (tweets.length) { | |
setProperty_("SINCEID", tweets[0].id_str); | |
} | |
for (var i=tweets.length-1; i>=0; i--) { | |
logTweet_(sheet, tweets[i]); | |
} | |
} | |
} | |
} | |
catch (e) { | |
Logger.log(e.toString()); | |
} | |
} | |
/* | |
Sets properties on the Spreadsheet to store the SINCEID and MAXID | |
https://developers.google.com/apps-script/reference/properties/properties-service | |
*/ | |
function setProperty_(key, value) { | |
var properties = PropertiesService.getScriptProperties(); | |
if (value) { | |
properties.setProperty(key, value); | |
} else { | |
return properties.getProperty(key) || ""; | |
} | |
} | |
function Stop() { | |
var triggers = ScriptApp.getScriptTriggers(); | |
for(var i=0; i < triggers.length; i++) { | |
ScriptApp.deleteTrigger(triggers[i]); | |
} | |
setProperty_("SINCEID", ""); | |
setProperty_("MAXID", ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment