Creating a Script to Pull Data from Twitter API: As Explained through Elon Musk Account Metrics
I am fascinated by Twitter.
I recently built a project that pulls data from the Twitter API for metadata about accounts. The functionality is written to allow data to be updated with the click of a button.
Twitter data can be used to harvest data about detailed account-level metrics. In this article, I am going to discuss two common endpoints: /Get/Followers and Get/Following.
Top 5 Most Followed Accounts:
First, let’s look at the 5 most followed accounts. Props to Barack Obama for being the most followed account on Twitter. Interestingly, 3 of the top 5 are musicians.
- @barackobama — 133 million followers
- @elonmusk — 119 million
- @justinbieber— 113 million
- @katyperry — 108 million
- @rihanna — 107 million
Let’s call this endpoint on Elon Musk’s account see the account-level metrics for him:
{ data: { username: ‘elonmusk’,
name: ‘Elon Musk’, id: ‘44196397’,
created_at: ‘2009–06–02T20:12:29.000Z’,
public_metrics: {
followers_count: 119375032,
following_count: 130,
tweet_count: 20855,
listed_count: 103808 },
description: ‘’,
verified: true } }
Here we learn that Musk’s account was created in June 2009. He has tweeted over 20,000 times. He has a large following and he is following 130 accounts .
Video Demo of the Application:
Goals of the Application:
- Call the ‘User’ endpoint https://api.twitter.com/2/users/by/username/
System Design Diagram of Twitter:
Shout out to venerable NeetCode. He puts up nice technical CS videos like the one below.
Usage of the Twitter API:
As I am building an app to automate data from the Twitter API which is where the data is stored––on Twitter’s servers––and storing it in a JavaScript variable and then printing it in a Google spreadsheet.
Interesting Analytics:
As we can see in the above video, the Twitter API is saying that Musk has 119,396,818 followers.
My account @bazill_thg is listed as 373 followers.
Link to my Twitter account: Basil Latif
Code to Get Following Information:
The Twitter API allows us to learn about who an account follows.
Here, I define a url which populates from an ID (linked to an account username) from the sheet. Then, the script calls the URL with the required headers.
var url = "https://api.twitter.com/2/users/" + current_id + "/following?max_results=1000"
//writing the call
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + bearer_token
},
'method' : 'get',
'contentType' : 'application/json'
});
Which is then parsed with the following code:
var json_response = JSON.parse(response);
var follower_arr = json_response['data']
console.log(json_response)
Which yields the following object array:
{ data: [ { id: ‘16076032’, name: ‘Glenn Greenwald’, username: ‘ggreenwald’ },
{ id: ‘3072160164’, name: ‘John Kraus’, username: ‘johnkrausphotos’ },
{ id: ‘17842366’, name: ‘Discovery’, username: ‘Discovery’ },
{ id: ‘1592542747726069766’, name: ‘Sandy Munro’, username: ‘teardowntitan’ },
{ id: ‘4767021’, name: ‘𝙺𝚒𝚖𝚋𝚊𝚕 𝙼𝚞𝚜𝚔 🤠’, username: ‘kimbal’ },
{ id: ‘1663172653’, name: ‘Terrible Maps’, username: ‘TerribleMaps’ },
{ id: ‘5405152’, name: ‘D.A. Wallach’, username: ‘dawallach’ },
{ id: ‘3918111614’, name: ‘Oriol Vinyals’, username: ‘OriolVinyalsML’ },
{ id: ‘945817135816654848’, name: ‘Trung Phan’, username: ‘TrungTPhan’ },
{ id: ‘4885277053’, name: ‘Daily Stoic’, username: ‘dailystoic’ } ],
meta: { result_count: 10, next_token: ‘81EN4U1D2L1HGZZZ’ } }
This represents accounts that Mr. Musk is following.
Analyzing A Sample of 9 accounts that Elon Musk follows:
1 — Glenn Greenwald: journalist
2 — John Kraus: spaceflight photographer
3 — Sandy Monro: founder of Munro & Associates
4 — Kimbal Musk: entreprenuer (brother)
5 — Terrible Maps:
6 — D.A. Wallach: Life sciences & healthcare investor.
7 — Oriol Vinyals: RD & Deep Learning Lead @DeepMind
8 — Trung Phan: writer
9 — Daily Stoic: philosophy bot
Musk seems to be interested in a broad array of types of accounts if one trusts this random sample I’ve extracted. Musk seems to follow journalists/photographers; entrepreneurs and businesspeople, and an assortment of other writers and thinkers.
Implications on Broader Society:
This software architecture can enable one to interact and communicate with a potentially large number of people through mining Twitter usernames.
Follow me on Medium if you enjoyed this.