Day69: Twitter API

Posted by csiu on May 4, 2017 | with: 100daysofcode
  by Celia Siu (in Portland)

The thing about going to conferences is there are a lot of people and a lot of interesting ideas. In today’s blog post, I’ll be referencing Hamdan Azhar to download twitter data using R. I will then later look at emoji data from these tweets.

I’ll also be going home today (and so this post will be short). Goodbye Portland, I had an excellent time!

Connect to twitter

# install.packages("twitteR")
library(twitteR)
library(dplyr)
#' Create API keys from https://apps.twitter.com
api_key <- 'XXX'
api_secret <- 'XXX'
access_token <- 'XXX'
access_token_secret <- 'XXX'
source("twitter_api_key.R") # To load true values

setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)

Pull tweets

#' Pull tweets
search_string <- '#csvconf'
tweets.raw <-
  searchTwitter(search_string,
                n = 1000,    # Max number of returned tweets
                lang = 'en', # Restrict tweets to given language
                since = '2017-05-02')
#' Remove retweets & convert twitteR lists to data.frames
df <-
  strip_retweets(tweets.raw, strip_manual = TRUE, strip_mt = TRUE) %>%
  twListToDF()

Now that we have the data, the next step is to clean the data and do analysis.