Chapter 10 Using APIs

Today, we will be focusing on the package academictwitteR. academictwitteR is a package that allows you to collect tweets through the Twitter API. In order to use academictwitteR, you must have access to the Academic Track of the Twitter API. If you do not have this access, I recommend using the Twitter 1.0 API (and rtweet).

The key advantage of the Twitter academic track API is the ability to search as far back as possible. However, the amount of data you can collect from the Twitter 2.0 API when streaming is more limited.

You can view the documentation for academictwitteR: https://github.com/cjbarrie/academictwitteR

#install.library("academictwitteR") #to install this package
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.2 --
## v ggplot2 3.3.6     v purrr   0.3.4
## v tibble  3.1.8     v dplyr   1.0.9
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(academictwitteR) #note that the only capitalized letter is the R at the end.

In order to use academictwitteR (or the Twitter API generally), it is necessary for you to get a “bearer token.” In the Twitter 1.1 endpoint, this was also called an “access token.” Keep in mind that a bearer token and an access token are NOT the same thing. Your bearer token should be one very (very) long string.

10.1 Setting up your bearer token

Once you have signed up for access to the Twitter 2.0 Academic Track API, you are now ready to proceed with using academicTwitteR. The first thing you will want to do is set your bearer token. You can do this in academicTwitteR with the set_bearer() function, which takes no arguments.

Once you use set_bearer(), you will be directed to a .Renviorn file, where you will be directed to place your bearer token.

set_bearer()

#vignette("academictwitteR-auth") #use this line to see the help page for authorizing!

You might also see instructions that look something like this: bearer token instructions For more information, check out the authorization section of the documentation (the author has made several very helpful gifs).

Once you’ve set your bearer token in your .Renviorn (which is like a global setting), you are ready to use the package!

10.2 Searching for Tweets

The most popular (and the main) function in academicTwitteR is get_all_tweets(), which is how you would query keywords from Twitter. When using this function the arguments you must have are the query terms and the start_tweets and end_tweets arguments (this is the start and end of your time frame). If you use ?get_all_tweets, you’ll also see a range of other arguments that are useful for modifying your data collection. For example, the n argument indicates how many tweets you want to collect (it defaults to the first 100).

potholes_data <- get_all_tweets("potholes",
                               "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z")
## Warning: Recommended to specify a data path in order to mitigate data loss when
## ingesting large amounts of data.
## Warning: Tweets will not be stored as JSONs or as a .rds file and will only be
## available in local memory if assigned to an object.
## query:  potholes 
## Total pages queried: 1 (tweets captured this page: 472).
## Total tweets captured now reach 100 : finishing collection.
nrow(potholes_data) #number of tweets collected
## [1] 100
colnames(potholes_data) #check out the column names (aka the variables) of the data
##  [1] "entities"            "conversation_id"     "id"                 
##  [4] "created_at"          "possibly_sensitive"  "public_metrics"     
##  [7] "source"              "text"                "attachments"        
## [10] "author_id"           "lang"                "referenced_tweets"  
## [13] "in_reply_to_user_id" "geo"                 "withheld"
head(potholes_data) #check out the first 6 lines of the data
##                 entities.annotations
## 1 79, 89, 0.6653, Place, Pima County
## 2                               NULL
## 3                               NULL
## 4   18, 26, 0.9613, Place, Bethlehem
## 5                               NULL
## 6  24, 26, 0.7877, Organization, CDC
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          entities.urls
## 1 235, 259, 258, 282, https://t.co/DbM5iH9lLO, https://t.co/UpIUGCXUmY, https://webcms.pima.gov/government/transportation/, https://twitter.com/PimaCountyDOT/status/1395528429252976641/photo/1, webcms.pima.gov/government/tra…, pic.twitter.com/UpIUGCXUmY, https://pbs.twimg.com/news_img/1526951942974365696/GLlerybu?format=jpg&name=orig, https://pbs.twimg.com/news_img/1526951942974365696/GLlerybu?format=jpg&name=150x150, 450, 150, 281, 150, 200, NA, Transportation - Pima County, NA, Roadway,improvement,improvements,transportation,highlights,divisions,documents,road,closure,construction,alerts,regional,authority,project, NA, https://webcms.pima.gov/government/transportation/, NA, NA, 3_1395528395077783554
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NULL
## 3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              64, 87, https://t.co/XePgDCYETQ, https://twitter.com/missodessa/status/1395085847045709826, twitter.com/missodessa/sta…
## 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NULL
## 5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NULL
## 6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NULL
##                            entities.mentions   entities.hashtags
## 1                                       NULL                NULL
## 2                3, 14, Phoenixwmn, 16832931                NULL
## 3                                       NULL                NULL
## 4                                       NULL                NULL
## 5              3, 15, Joyster2012, 615063544 119, 129, audiobook
## 6 3, 18, newjerseymemes, 1155233445603418115                NULL
##       conversation_id                  id               created_at
## 1 1395528429252976641 1395528429252976641 2021-05-20T23:54:39.000Z
## 2 1395528223283458049 1395528223283458049 2021-05-20T23:53:50.000Z
## 3 1395527080213889026 1395527080213889026 2021-05-20T23:49:18.000Z
## 4 1395526031642087426 1395526031642087426 2021-05-20T23:45:08.000Z
## 5 1395525103153123331 1395525103153123331 2021-05-20T23:41:26.000Z
## 6 1395524668875067393 1395524668875067393 2021-05-20T23:39:43.000Z
##   possibly_sensitive public_metrics.retweet_count public_metrics.reply_count
## 1              FALSE                            2                          1
## 2              FALSE                            2                          0
## 3              FALSE                            0                          0
## 4              FALSE                            0                          1
## 5              FALSE                           11                          0
## 6              FALSE                           80                          0
##   public_metrics.like_count public_metrics.quote_count              source
## 1                         4                          0     Twitter Web App
## 2                         0                          0 Twitter for Android
## 3                         0                          0  Twitter for iPhone
## 4                         0                          0  Twitter for iPhone
## 5                         0                          0     Twitter Web App
## 6                         0                          0  Twitter for iPhone
##                                                                                                                                                                                                                                                                                             text
## 1 Crews respond to service requests to repair potholes throughout unincorporated Pima County today. \n\nNeed to Report a Non-Emergency Transportation-Related Road Condition Concern?\n\nIt’s as easy as SeeClickFix. Download the app or visit: https://t.co/DbM5iH9lLO https://t.co/UpIUGCXUmY
## 2                                                                                                                                               RT @Phoenixwmn: Both as RN &amp; person still in treatment for BC, I don't think ppl realize horrific side effects frm conventionl cancer treat…
## 3                                                                                                                                                                                                        Turbulence is just potholes in the sky. Somebody call the city. https://t.co/XePgDCYETQ
## 4                                                                                                                                                                                                                        I knew we were in Bethlehem by the bus shaking about cause of potholes.
## 5                                                                                                                                                   RT @Joyster2012: POTHOLES AND MAGIC CARPETS. The dramatic stories of couples linked by blood, friendship, or lust. The #audiobook is on #Au…
## 6                                                                                                                                                                                                                                RT @newjerseymemes: the CDC says to fill these f*cking potholes
##              media_keys          author_id lang              referenced_tweets
## 1 3_1395528395077783554 892102518850732036   en                           NULL
## 2                  NULL 823208500910313478   en retweeted, 1395522195812671489
## 3                  NULL          156731813   en    quoted, 1395085847045709826
## 4                  NULL         4570833323   en                           NULL
## 5                  NULL 743768036239245312   en retweeted, 1395426106531057664
## 6                  NULL 936993506563907585   en retweeted, 1394694703614939137
##   in_reply_to_user_id geo.place_id geo.coordinates.type
## 1                <NA>         <NA>                 <NA>
## 2                <NA>         <NA>                 <NA>
## 3                <NA>         <NA>                 <NA>
## 4                <NA>         <NA>                 <NA>
## 5                <NA>         <NA>                 <NA>
## 6                <NA>         <NA>                 <NA>
##   geo.coordinates.coordinates withheld.copyright withheld.country_codes
## 1                        NULL                 NA                   NULL
## 2                        NULL                 NA                   NULL
## 3                        NULL                 NA                   NULL
## 4                        NULL                 NA                   NULL
## 5                        NULL                 NA                   NULL
## 6                        NULL                 NA                   NULL

In this process, you may notice two interesting things. First, you will likely get a warning or note that reads like this: Recommended to specify a data path in order to mitigate data loss when ingesting large amounts of data.Tweets will not be stored as JSONs or as a .rds file and will only be available in local memory if assigned to an object. This is a standard warning you may get from using get_all_tweets() without indicating a place to save. When you use get_all_tweets() with just the basic arguments (the query, start_tweets, and end_tweets), the data will be only stored in your R envionrment. For smaller data searches (<20,000), this is not that big of a deal. However, if you are doing a larger data collection (like, more than 100,000 tweets), you will want to set an external save point. This is helpful in case your computer restarts or crashes as you are collecting data (e.g., if your computer runs out of space to save the data or if your computer accidentally restarts to update). You can set a data path by using the file and data_path argument:

potholes_data <- get_all_tweets("potholes",
                         "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z",
                         file = "potholes", #saves a backup RDS file with this name
                         data_path = "C:/Users/comm-lukito/Documents") #saves another file, a json, with this data
## query:  potholes
## Warning: Directory already exists. Existing JSON files may be parsed and
## returned, choose a new path if this is not intended.
## Total pages queried: 1 (tweets captured this page: 472).
## Total tweets captured now reach 100 : finishing collection.

In this query, I save two external files: the file argument saves an RDS version of the data and the data_path argument saves the data as a json file (your data path can be different from your working directory). RDS files are fairly popular in R because they tend to be much smaller than csv files and other data types. json files are also small, and they are the default way that the Twitter API sends you data (the get_all_tweets() function converts the json files from the Twitter API into a data frame in R).

The other interesting thing you may notice is some information about a “page” (in this case, the text looks like this: queried: 1 (tweets captured this page: 498).). With large data collections that may crash in the middle, pages are very useful because they allow you to break up a data collection into multiple pages. A standard “page” is about 500 tweets (you can set a smaller number of tweets per page by using the page_n argument). The larger your query, the more pages you will naturally have.

You may also get annoyed with all the updates provided by the get_all_tweets() function. You can ignore these using the verbose = FALSE argument (verbose, the argument which provides details as the data are being queried, is set to TRUE by default, which is why you see all these messages about the pages).

potholes_data <- get_all_tweets("potholes",
                         "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z",
                         file = "potholes",
                         data_path = "C:/Users/comm-lukito/Documents",
                         verbose = FALSE)

10.2.1 Multiple Keywords

What if you wanted to search a set of keywords, rather than just one keyword? Well, the query argument in get_all_tweets() can take a vector of strings. You can use this to search a set of hashtags and keywords.

keywords <- c("potholes", "pot holes", "#potholes") #this is a character vector with 3 items

potholes_data <- get_all_tweets(keywords,
                         "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z",
                         data_path = "C:/Users/comm-lukito/Documents",
                         verbose = FALSE)

In this search, your query is potholes OR pot holes OR #potholes. If you wanted to search by AND (potholes AND road, for example), you can include both words in the same string

potholes_data <- get_all_tweets("potholes road", #searches for potholes AND road
                         "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z",
                         data_path = "C:/Users/comm-lukito/Documents",
                         verbose = FALSE)

10.2.2 Negations in Keywords

You can also exclude words using the - negation:

potholes_data <- get_all_tweets("potholes -road", #searches for potholes AND road
                         "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z",
                         data_path = "C:/Users/comm-lukito/Documents",
                         verbose = FALSE)

In fact, you can use the - negation to remove other things, like retweets! Below, I use the negation in front of is:retweet, which is a search parameter you can use to get only retweets (by negating it, I therefore search for only NOT retweets).

potholes_data <- get_all_tweets("potholes road -is:retweet", #searches for potholes AND road, but 
                         "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z",
                         data_path = "C:/Users/comm-lukito/Documents",
                         verbose = FALSE)

There are a lot of neat ways to use the query part to search for what you need. One of the most common ones I use is lang:en, which indicates that I only want English tweets. For more information, you can check out the build a query page on Twitter.

#vignette("academictwitteR-build") #you can also check out this helpful tutorial by removing the # before this line.

10.3 Collecting a lot of tweets

Often, the data you are interested in collecting has more than 100 tweets. What then? You can increase your query using the n argument, as I show below:

#?get_all_tweets
potholes_data <- get_all_tweets("potholes",
                                "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z", #start and end dates
                                n = 1000, #number of tweets I want
                                file = "potholes", #rds file name
                                data_path = "C:/Users/comm-lukito/Documents") #place to store json file
## query:  potholes 
## Total pages queried: 1 (tweets captured this page: 472).
## Total pages queried: 2 (tweets captured this page: 470).
## Total pages queried: 3 (tweets captured this page: 467).
## Total tweets captured now reach 1000 : finishing collection.

Notice how my larger query produces more pages?

Importantly, when your query has multiple pages, your json files will be stored in the separate pages (however, you’ll notice that your data frame in your R environment and your RDS file will have all the tweets combined into one file).

10.3.1 count_all_tweets()

But what if you don’t know how large your dataset should be (aka you don’t know what to put in n). What then?

Thankfully, academictwitteR has a neat function called count_all_tweets(), which will give you information about the number of tweets per day for your query. count_all_tweets() has, at minimum, three arguments (the keywords, the start date, and the end date).

count_all_tweets("potholes", "2021-05-10T00:00:00Z", "2021-05-21T00:00:00Z")
## query:  potholes 
## Total pages queried: 1 (tweets captured this page: 11).
## This is the last page for potholes : finishing collection.
##                         end                    start tweet_count
## 1  2021-05-11T00:00:00.000Z 2021-05-10T00:00:00.000Z         893
## 2  2021-05-12T00:00:00.000Z 2021-05-11T00:00:00.000Z        1489
## 3  2021-05-13T00:00:00.000Z 2021-05-12T00:00:00.000Z        8707
## 4  2021-05-14T00:00:00.000Z 2021-05-13T00:00:00.000Z        1978
## 5  2021-05-15T00:00:00.000Z 2021-05-14T00:00:00.000Z        1212
## 6  2021-05-16T00:00:00.000Z 2021-05-15T00:00:00.000Z         727
## 7  2021-05-17T00:00:00.000Z 2021-05-16T00:00:00.000Z        7051
## 8  2021-05-18T00:00:00.000Z 2021-05-17T00:00:00.000Z        1752
## 9  2021-05-19T00:00:00.000Z 2021-05-18T00:00:00.000Z        2045
## 10 2021-05-20T00:00:00.000Z 2021-05-19T00:00:00.000Z        1202
## 11 2021-05-21T00:00:00.000Z 2021-05-20T00:00:00.000Z        1114

10.4 Collecting tweets from an account

What if you wanted to query by user rather than by search. For example, maybe you want to collect tweets from the Transportation Security Administration @TSA. You can actually use get_all_tweets() to do this, too! But, instead of using the default “query” search, you can search for users, like below. Note that the users argument takes a character vector, even if it is a character vector of one.

tsa_data <- get_all_tweets(users = "tsa", #user search
                          start_tweets = "2021-05-10T00:00:00Z", 
                          end_tweets = "2021-05-21T00:00:00Z", 
                          n = 200,
                          verbose = FALSE) 
nrow(tsa_data)
## [1] 52

Note that if you request more data than there is on Twitter, Twitter will return as much data as it can (for example, I asked for 200 tweets here and received 52).

Want to see what TSA tweets mention the airport? You can combine queries and user information to do this:

tsa_data <- get_all_tweets(query = "airport", #airport search
                           users = c("tsa"), #user search
                           start_tweets = "2021-05-10T00:00:00Z", end_tweets = "2021-06-10T00:00:00Z", 
                           n = 200,
                           verbose = FALSE)

nrow(tsa_data) #30 tweets in this time frame from the TSA that uses the word "airport"
## [1] 30

Another way to do this is to use the get_user_timeline() function. With this function, you can query the timeline of a user or set of users. get_user_timeline() does not take a query, only the ID of a user.

tsa_data$author_id[1] #each tweet comes with an "author_id" for the author and an "id" for the specific tweet
## [1] "414331122"
tsa_data2 <- get_user_timeline("414331122",
                               start_tweets = "2020-01-01T00:00:00Z", end_tweets = "2020-12-31T00:00:00Z", 
)
## Warning: Recommended to specify a data path in order to mitigate data loss when
## ingesting large amounts of data.
## Warning: Tweets will not be stored as JSONs or as a .rds file and will only be
## available in local memory if assigned to an object.
## user:  414331122 
## Total pages queried: 1 (tweets captured this page: 100).
## Total tweets captured now reach 100 : finishing collection.

Why would you use get_user_timeline() instead of get_all_tweets()? In some instances, you may come across a list of twitter users from an academic paper. In many instances, the author will provide only the author ID and not the username (this is related to Twitter’s terms of service, which allows you to only share tweet and author IDs publicly). In these instances, it may be faster and more efficient to use get_user_timeline().

10.4.1 Getting user information

With the author ID, you can also get interesting information about the author, such as their profile, their followings, and their followers.

To get the profile of the user, use get_user_profile() (note that I am using the author_id here).

get_user_profile("414331122") #this provides some basic information about the TSA user, including their description, their created date, and whether they are verified.
## Processing from 1 to 1
##         location        id username               created_at
## 1 Washington, DC 414331122      TSA 2011-11-16T22:47:58.000Z
##                                                                                                                        description
## 1 Official Twitter account of the Transportation Security Administration. Read our social media policy at https://t.co/9HmDrK3xG2.
##   name                                                 entities.urls
## 1  TSA 0, 23, https://t.co/xGW3bdeMlQ, https://www.tsa.gov/, tsa.gov
##                                                entities.urls protected verified
## 1 104, 127, https://t.co/9HmDrK3xG2, http://tsa.gov, tsa.gov     FALSE     TRUE
##       pinned_tweet_id                     url
## 1 1561386651179372544 https://t.co/xGW3bdeMlQ
##                                                             profile_image_url
## 1 https://pbs.twimg.com/profile_images/932647430906155008/cW3A_gul_normal.jpg
##   public_metrics.followers_count public_metrics.following_count
## 1                         251417                            475
##   public_metrics.tweet_count public_metrics.listed_count
## 1                      12675                        1711

Want to get a list of accounts that the TSA follows? Use get_user_following().

get_user_following("414331122") %>% head()
## Processing 414331122
## Total data points:  475 
## This is the last page for  414331122 : finishing collection.
##         username                              name     pinned_tweet_id
## 1        reesean Sean Ree <U+0001F1FA><U+0001F1E6> 1561436343670132737
## 2     BUFAirport                   Buffalo Airport                <NA>
## 3 TSA_NewEngland                    TSA_NewEngland                <NA>
## 4          POTUS                   President Biden                <NA>
## 5     mspairport                       MSP Airport                <NA>
## 6    USAFReserve                 Air Force Reserve                <NA>
##                                                                         entities.urls
## 1 0, 23, https://t.co/HRB3q7Nzfa, http://reesean.wordpress.com, reesean.wordpress.com
## 2   0, 23, https://t.co/7IKlMTHgc4, http://www.buffaloairport.com, buffaloairport.com
## 3              0, 23, https://t.co/LKx07Y8oq4, https://www.tsa.gov/news, tsa.gov/news
## 4               0, 23, https://t.co/MzB1JVXAkq, http://WhiteHouse.gov, WhiteHouse.gov
## 5           0, 23, https://t.co/5tcUhwtLDU, http://www.mspairport.com, mspairport.com
## 6                  0, 22, http://t.co/htCCTvwdBi, http://www.afrc.af.mil, afrc.af.mil
##                                                                 entities.description.urls
## 1                                                                                    NULL
## 2                                                                                    NULL
## 3                                                                                    NULL
## 4 98, 121, https://t.co/HDhBZB391k, http://whitehouse.gov/privacy, whitehouse.gov/privacy
## 5                                                                                    NULL
## 6                                                                                    NULL
##   entities.description.mentions
## 1                          NULL
## 2                          NULL
## 3                          NULL
## 4                48, 55, FLOTUS
## 5                          NULL
## 6                          NULL
##                                                                                     entities.description.hashtags
## 1                                                                                                            NULL
## 2                                                                                                            NULL
## 3                                                                                                            NULL
## 4                                                                                                            NULL
## 5                                                                                                            NULL
## 6 97, 103, 114, 128, 146, 102, 113, 127, 145, 160, AFRC, AFReserve, ReserveReady, ReserveResilient, ReserveReform
##   public_metrics.followers_count public_metrics.following_count
## 1                           1715                           5002
## 2                           5129                            890
## 3                           1198                            727
## 4                       24674373                             12
## 5                          37989                           1915
## 6                          34717                           1196
##   public_metrics.tweet_count public_metrics.listed_count verified
## 1                     204979                         138    FALSE
## 2                       4251                         102    FALSE
## 3                       1350                          21     TRUE
## 4                       3799                       19421     TRUE
## 5                      10488                         898     TRUE
## 6                       7642                         456     TRUE
##                                                                                                                                                               description
## 1                           Fevered dreams of future tyrannies. Armchair analyst. I drop knowledge and opinions. Academics: literature, theology, history. Generation: X.
## 2              Up to 100 daily nonstop flights to 26 airports & beyond. The Buffalo Niagara International Airport is the affordable Gateway to Buffalo Niagara & Toronto.
## 3            Dan Velez - TSA Media Spokesperson for New England and Manager of the TSA_NewEngland Twitter account. Air Force Veteran from Rutland, Vermont and SNHU Alum.
## 4                      46th President of the United States, husband to @FLOTUS, proud dad & pop. Tweets may be archived: https://t.co/HDhBZB391k\nText me: (302) 404-0880
## 5                The official Twitter page of the Minneapolis-St.Paul International Airport - serving 165 nonstop markets. Best Airport in North America award winner x5.
## 6 Official Twitter page of U.S. Air Force Reserve Command (Following, RTs and links <U+2260> endorsement) #AFRC #AFReserve #ReserveReady #ReserveResilient #ReserveReform
##                  location
## 1            St. Paul, MN
## 2         Cheektowaga, NY
## 3        TSA Headquarters
## 4                    <NA>
## 5 Minneapolis-St.Paul, MN
## 6          Robins AFB, GA
##                                                              profile_image_url
## 1 https://pbs.twimg.com/profile_images/1396657900295049218/dnF_0cfE_normal.jpg
## 2 https://pbs.twimg.com/profile_images/1542883714987950082/qcN56vOm_normal.jpg
## 3 https://pbs.twimg.com/profile_images/1554110267667222531/9-Oi6Lqb_normal.jpg
## 4 https://pbs.twimg.com/profile_images/1380530524779859970/TfwVAbyX_normal.jpg
## 5  https://pbs.twimg.com/profile_images/988451404825100288/z53Uzd58_normal.jpg
## 6 https://pbs.twimg.com/profile_images/1196866876485320709/pTiFkuMN_normal.png
##                 created_at                     url                  id
## 1 2009-11-11T15:49:12.000Z https://t.co/HRB3q7Nzfa            89213165
## 2 2015-03-31T12:24:23.000Z https://t.co/7IKlMTHgc4          3130667535
## 3 2020-07-11T08:17:42.000Z https://t.co/LKx07Y8oq4 1281864953742458882
## 4 2021-01-13T00:37:08.000Z https://t.co/MzB1JVXAkq 1349149096909668363
## 5 2009-04-15T13:26:17.000Z https://t.co/5tcUhwtLDU            31405817
## 6 2009-04-24T19:43:39.000Z  http://t.co/htCCTvwdBi            35017909
##   protected   from_id
## 1     FALSE 414331122
## 2     FALSE 414331122
## 3     FALSE 414331122
## 4     FALSE 414331122
## 5     FALSE 414331122
## 6     FALSE 414331122

The TSA follows 473 people (here, I show the first 6 by piping in the head() function). When you use get_user_following(), you will get the information of those users, including their profile, their username, and their author ID.

Want to get the number of followers that follow an account? Use get_user_followers()! Now, the TSA has a lot of followers (~239k), so I’m going to use my own account, which has only 2000 or so followers.

get_user_followers("24355706") %>% head()
## Processing 24355706
## Total data points:  1000 
## Total data points:  2000 
## Total data points:  3000 
## Total data points:  3291 
## This is the last page for  24355706 : finishing collection.
##   verified               created_at
## 1    FALSE 2018-10-29T07:53:22.000Z
## 2    FALSE 2009-02-24T15:57:15.000Z
## 3    FALSE 2009-05-25T22:05:45.000Z
## 4    FALSE 2009-04-25T13:15:56.000Z
## 5    FALSE 2017-02-19T14:47:38.000Z
## 6    FALSE 2009-06-22T14:13:23.000Z
##                                                              profile_image_url
## 1 https://pbs.twimg.com/profile_images/1537469718617272320/g8NG--j-_normal.jpg
## 2 https://pbs.twimg.com/profile_images/1134505075777257472/kv_JXYNr_normal.jpg
## 3 https://pbs.twimg.com/profile_images/1535901008458825729/IxLtgbJt_normal.jpg
## 4 https://pbs.twimg.com/profile_images/1325085165362704385/JHpG4CS-_normal.jpg
## 5 https://pbs.twimg.com/profile_images/1435805995955982340/UQ7YHhTA_normal.jpg
## 6 https://pbs.twimg.com/profile_images/1531370627936006145/OHUG8Se8_normal.jpg
##                    id                       name
## 1 1056816244622413824    Md Nahidul Islam (Emon)
## 2            21765347 Dr. Belinha De Abreu, PhD.
## 3            42498255              Chris Marsden
## 4            35208333               Kunto Wibowo
## 5  833327186958508032           Trang Nhung Pham
## 6            49639366                        Lew
##                                                                                            entities.urls
## 1 0, 23, https://t.co/nOasUB4Bzf, https://nahidulislaminfo.wordpress.com, nahidulislaminfo.wordpress.com
## 2                      0, 23, https://t.co/y81o6SDQnp, http://www.belinhadeabreu.com, belinhadeabreu.com
## 3             0, 23, https://t.co/7GDNHIDg0C, https://twitter.com/prof_marsden, twitter.com/prof_marsden
## 4                                                                                                   NULL
## 5          0, 23, https://t.co/ItNaRZnJbM, https://lynnsalcove.wordpress.com/, lynnsalcove.wordpress.com
## 6                                                                                                   NULL
##                                      entities.description.urls
## 1                                                         NULL
## 2 66, 89, https://t.co/38h8Kz9a5W, http://imlrs.net, imlrs.net
## 3                                                         NULL
## 4                                                         NULL
## 5                                                         NULL
## 6                                                         NULL
##                                        entities.description.hashtags
## 1                                                               NULL
## 2 112, 127, 141, 126, 140, 149, medialiteracy, newsliteracy, DigiCit
## 3                                                               NULL
## 4                                                               NULL
## 5                                                               NULL
## 6                                                               NULL
##           entities.description.mentions entities.description.cashtags
## 1                                  NULL                          NULL
## 2                       104, 110, IC4ML                          NULL
## 3                147, 160, prof_marsden                          NULL
## 4 9, 58, 15, 74, unpad, surveiKedaiKopi                          NULL
## 5                                  NULL                          NULL
## 6                                  NULL                          NULL
##             location
## 1         Bangladesh
## 2   Connecticut, USA
## 3   Hamburg, Germany
## 4 Bandung, Indonesia
## 5     Hanoi, Vietnam
## 6               <NA>
##                                                                                                                                                        description
## 1                          Communication | PR | Non-profit Organization Leader | Organizer | Youth Leader | Freelance Content Writer | Wordpress website devoloper
## 2            International Media Literacy Educator-Researcher-Author; Founder: https://t.co/38h8Kz9a5W; President of @IC4ML; #medialiteracy #newsliteracy #DigiCit
## 3 AI & Internet regulation lawyer: all views personal, RT not endorsement, Likes mean bookmark, etc. Locked ~3000. DoB PPM 324-now 421 Academic work @prof_marsden
## 4                                                                                       Lecturer @unpad Dept Communication; Executive Director of @surveiKedaiKopi
## 5                                                                                                             Mundus Journalism student eager to feel the world ^^
## 6                                                                         PhD Candidate, UC Santa Barbara |\nGraduate Researcher, Nanyang Technological University
##   public_metrics.followers_count public_metrics.following_count
## 1                            110                           1870
## 2                           3455                           5010
## 3                           3091                           2039
## 4                           1236                            651
## 5                            173                            668
## 6                             68                            215
##   public_metrics.tweet_count public_metrics.listed_count
## 1                        176                           0
## 2                      10281                          93
## 3                     101792                         249
## 4                       5320                          10
## 5                        846                           2
## 6                          8                           0
##                       url      username protected     pinned_tweet_id  from_id
## 1 https://t.co/nOasUB4Bzf      niemon24     FALSE                <NA> 24355706
## 2 https://t.co/y81o6SDQnp      belmedia     FALSE                <NA> 24355706
## 3 https://t.co/7GDNHIDg0C ChrisTMarsden      TRUE 1551412928070713344 24355706
## 4                             skuntoadi     FALSE 1235010875725144064 24355706
## 5 https://t.co/ItNaRZnJbM    Lynnnnn199     FALSE                <NA> 24355706
## 6                                leuuzj     FALSE                <NA> 24355706