Last active
February 12, 2023 05:40
Add public metrics
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
const BearerTokenKey = 'twitterBearerToken'; | |
function onOpen() { | |
SpreadsheetApp | |
.getUi() | |
.createMenu('Twitter') | |
.addItem('Set Bearer token', 'helpers.requestBearerToken') | |
.addItem('Sign out', 'helpers.logout') | |
.addToUi(); | |
} | |
const helpers = {}; | |
helpers.bearerToken = () => PropertiesService.getScriptProperties().getProperty(BearerTokenKey); | |
helpers.checkBearerToken = () => { | |
if (!helpers.bearerToken()) { | |
throw new Error('You need to set a Bearer token before using this function. Select "Set Bearer Token" from the Twitter menu to set it.'); | |
} | |
} | |
helpers.lookupUser = (tweet) => { | |
const authorId = tweet.data.author_id; | |
let author = {}; | |
for (const user of tweet.includes.users) { | |
if (user.id === authorId) { | |
author = user; | |
return user; | |
} | |
} | |
return {}; | |
} | |
helpers.logout = () => PropertiesService.getScriptProperties().deleteAllProperties(); | |
helpers.tweetIdFromURL = (url) => { | |
const tweetRegex = url.match(/status\/(\d{1,19})/); | |
if (!tweetRegex) { | |
throw new Error('Invalid URL'); | |
} | |
return tweetRegex[1]; | |
} | |
helpers.requestBearerToken = () => { | |
// Build the input prompt | |
const ui = SpreadsheetApp.getUi(); | |
const result = ui.prompt( | |
'Bearer token', | |
'A Bearer token is the access token to make requests to the Twitter API.\nYou can find the Bearer token in your Twitter Developer Portal under Keys and Tokens.\n\nPaste the Bearer token here:', | |
ui.ButtonSet.OK); | |
// Proceed if the user clicked OK | |
if (result.getSelectedButton() == ui.Button.OK) { | |
const bearerToken = result.getResponseText().trim(); | |
// Do nothing if the user clicked OK without specifying a value | |
// (we can always ask for a token later) | |
if (bearerToken.length > 0) { | |
const properties = PropertiesService.getScriptProperties(); | |
properties.setProperty(BearerTokenKey, bearerToken); | |
} | |
} | |
} | |
helpers.tweet = (tweetId) => { | |
helpers.checkBearerToken(); | |
const lookupURL = `https://api.twitter.com/2/tweets/${tweetId}?expansions=author_id&user.fields=description&tweet.fields=public_metrics`; | |
const response = UrlFetchApp.fetch( | |
lookupURL, { | |
headers: { | |
'Authorization': `Bearer ${helpers.bearerToken()}` | |
} | |
}); | |
const tweet = JSON.parse(response.getContentText()); | |
if (tweet.errors && !tweet.data) { | |
throw new Error(JSON.stringify(tweet.errors)); | |
} | |
return tweet; | |
} | |
function TWEET(url) { | |
const tweetId = helpers.tweetIdFromURL(url); | |
const tweet = helpers.tweet(tweetId); | |
return tweet.data.text; | |
} | |
function TWEET_AUTHOR(tweetURL) { | |
const tweetId = helpers.tweetIdFromURL(tweetURL); | |
const tweet = helpers.tweet(tweetId); | |
const user = helpers.lookupUser(tweet); | |
return user.username || ''; | |
} | |
function BIO_OF_TWEET_AUTHOR(tweetURL) { | |
const tweetId = helpers.tweetIdFromURL(tweetURL); | |
const tweet = helpers.tweet(tweetId); | |
const user = helpers.lookupUser(tweet); | |
return user.description || ''; | |
} | |
function LIKES_OF_TWEET(tweetURL) { | |
const tweetId = helpers.tweetIdFromURL(tweetURL); | |
const tweet = helpers.tweet(tweetId); | |
return tweet.data.public_metrics.like_count || ''; | |
} | |
function REPLIES_OF_TWEET(tweetURL) { | |
const tweetId = helpers.tweetIdFromURL(tweetURL); | |
const tweet = helpers.tweet(tweetId); | |
return tweet.data.public_metrics.reply_count || ''; | |
} | |
function RETWEETS_OF_TWEET(tweetURL) { | |
const tweetId = helpers.tweetIdFromURL(tweetURL); | |
const tweet = helpers.tweet(tweetId); | |
return tweet.data.public_metrics.retweet_count || ''; | |
} | |
function QUOTES_OF_TWEET(tweetURL) { | |
const tweetId = helpers.tweetIdFromURL(tweetURL); | |
const tweet = helpers.tweet(tweetId); | |
return tweet.data.public_metrics.quote_count || ''; | |
} |
Hi @iamdaniele me again here. Does it possible now to retrieve video_url for v2 twitter api?
Is it possible to retrieve the view count of tweet that is showing now below each tweet?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@iamdaniele Thank you. The gist kind of does 😂 It's my take on it so it's not entirely functional! I will head over to the forums and thank you so much for your help and the inspiration.