4.16 X-Lab: Twitter’s streaming API
4.16.1 Authenticating
Before we can start collecting Twitter data, we need to create an OAuth token that will allow us to authenticate our connection and access our personal data.
After the new API changes, getting a new token requires submitting an application for a developer account, which may take a few days. For teaching purposes only, I will temporarily share one of my tokens with each of you, so that we can use the API without having to do the authentication.
However, if in the future you want to get your own token, here’s how you would do it:
Follow these steps to create your token:
- Go to https://developer.twitter.com/en/apps and sign in.
- If you don’t have a developer account, you will need to apply for one first. Fill in the application form and wait for a response.
- Once it’s approved, click on “Create New App”. You will need to have a phone number associated with your account in order to be able to create a token.
- Fill name, description, and website (it can be anything, even http://www.google.com). Make sure you leave ‘Callback URL’ empty.
- Agree to user conditions.
- From the “Keys and Access Tokens” tab, copy consumer key and consumer secret and paste below
- Click on “Create my access token”, then copy and paste your access token and access token secret below
What can go wrong here? Make sure all the consumer and token keys are pasted here as is, without any additional space character. If you don’t see any output in the console after running the code above, that’s a good sign.
Note that I saved the list as a file in my hard drive. That will save us some time later on, but you could also just re-run the code in lines 22 to 27 before connecting to the API in the future.
To check that it worked, try running the line below:
If this displays LSEnews
then we’re good to go!
Some of the functions below will work with more than one token. If you want to save multiple tokens, see the instructions at the end of the file.
4.16.2 Collecting data from Twitter’s Streaming API
4.16.2.1 Collecting using keywords
Collecting tweets filtering by keyword:
Note the options:
file.name
indicates the file in your disk where the tweets will be downloaded
track
is the keyword(s) mentioned in the tweets we want to capture.timeout
is the number of seconds that the connection will remain open
oauth
is the OAuth token we are using
Once it has finished, we can open it in R as a data frame with the parseTweets
function
If we want, we could also export it to a csv file to be opened later with Excel
And this is how we would capture tweets mentioning multiple keywords:
Note that here I choose a different option, tweets
, which indicates how many tweets (approximately) the function should capture before we close the connection to the Twitter API.
4.16.2.2 Collecting using geolocation
This second example shows how to collect tweets filtering by location instead. In other words, we can set a geographical box and collect only the tweets that are coming from that area.
For example, imagine we want to collect tweets from the United States. The way to do it is to find two pairs of coordinates (longitude = West/East and latitude = North/South) that indicate the southwest corner AND the northeast corner. Note the reverse order: it’s not (lat, long), but (long, lat).
In the case of the US, it would be approx. (-125,25) and (-66,50). How to find these coordinates? Simply click on point in Google Maps and copy paste the coordinates.
In the case of Europe it would be approx. the coordinates below.
We can do as before and open the tweets in R
And use the maps library to see where most tweets are coming from. Note that there are two types of geographic information on tweets: lat
/lon
(from geolocated tweets) and place_lat
and place_lon
(from tweets with place information). We will work with whatever is available.
We can also prepare a map of the exact locations of the tweets.
And try to do the same for Europe:
4.16.2.3 Extract a network of retweets (skip)
And here’s how to extract the edges of a network of retweets (at least one possible way of doing it):
4.16.2.4 Collecting a random sample
Finally, it’s also possible to collect a random sample of tweets. That’s what the “sampleStream” function does:
Here I’m collecting 30 seconds of tweets. And once again, to open the tweets in R…
What is the most retweeted tweet?
What are the most popular hashtags at the moment? We’ll use regular expressions to extract hashtags.
And who are the most frequently mentioned users?
How many tweets mention Justin Bieber?
These are toy examples, but for large files with tweets in JSON format, there might be faster ways to parse the data. For example, the ndjson
package offers a robust and fast way to parse JSON data: