Created
April 29, 2017 03:36
-
-
Save greenido/a2fe868acdecaff07e3ed1491817e66c to your computer and use it in GitHub Desktop.
Apps script to fetch stats on tweets (retweets, likes etc')
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
/** | |
* This code is fetching twitter stats on our tweets: retweets and likes at the moment. | |
* | |
* @Author: Ido Green | |
* @date: 4/2017 | |
* @see: | |
* https://github.com/googlesamples/apps-script-oauth1 | |
* https://dev.twitter.com/rest/reference/get/statuses/show/id | |
* | |
*/ | |
// | |
// menu | |
// | |
function onOpen() { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var entries = [{ | |
name : "Update All", functionName : "getStatsOntweets" | |
}]; | |
spreadsheet.addMenu("Stats", entries); | |
}; | |
// | |
// The main runner that fetch all the stats per tweet | |
// | |
function getStatsOntweets() { | |
var curSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Tweets'); | |
var tweetsLinks = curSheet.getRange('E2:E').getValues(); | |
var j = 2; | |
for (i in tweetsLinks) { | |
var curLink = tweetsLinks[i][0]; | |
var inx1 = curLink.indexOf("status", 10); | |
if (inx1 > 0) { | |
// we have a link to work on | |
var id = curLink.substring(inx1 + 7, curLink.length); | |
var retData = makeRequest(id); | |
curSheet.getRange(j, 7).setValue(retData[0]); | |
curSheet.getRange(j, 8).setValue(retData[1]); | |
j++; | |
} | |
else { | |
break; | |
} | |
} | |
SpreadsheetApp.getActiveSpreadsheet().toast("Done updating " + i + " tweets"); | |
} | |
// | |
// Use the API to fetch data per tweet | |
// | |
function makeRequest(tweetID) { | |
var twitterService = getTwitterService(); | |
var response = twitterService.fetch('https://api.twitter.com/1.1/statuses/show.json?id=' + tweetID); | |
var resObj = JSON.parse(response); | |
var retData = [resObj.retweet_count, resObj.favorite_count]; | |
return retData; | |
} | |
// | |
// | |
// | |
function authCallback(request) { | |
var twitterService = getTwitterService(); | |
var isAuthorized = twitterService.handleCallback(request); | |
if (isAuthorized) { | |
return HtmlService.createHtmlOutput('Success! You can close this tab.'); | |
makeRequest(); | |
} else { | |
return HtmlService.createHtmlOutput('Denied. You can close this tab'); | |
} | |
} | |
// | |
// | |
// | |
function getTwitterService() { | |
// Create a new service with the given name. The name will be used when | |
// persisting the authorized token, so ensure it is unique within the | |
// scope of the property store. | |
return OAuth1.createService('twitter') | |
// Set the endpoint URLs. | |
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token') | |
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token') | |
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize') | |
// Set the consumer key and secret. | |
.setConsumerKey('..TODO..') | |
.setConsumerSecret('..TODO..') | |
// Set the name of the callback function in the script referenced | |
// above that should be invoked to complete the OAuth flow. | |
.setCallbackFunction('authCallback') | |
// Set the property store where authorized tokens should be persisted. | |
.setPropertyStore(PropertiesService.getUserProperties()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment