Skip to content

Instantly share code, notes, and snippets.

@wesslen
Last active January 10, 2018 19:04
Show Gist options
  • Save wesslen/2c4808bc21f59d30aa63713144944f4e to your computer and use it in GitHub Desktop.
Save wesslen/2c4808bc21f59d30aa63713144944f4e to your computer and use it in GitHub Desktop.
Code to run Twitter Bumper Sticker Experiment using twitteR and tweetscores
# install required packages (only need to run once)
# devtools::install_github("pablobarbera/twitter_ideology/pkg/tweetscores")
# install.packages("twitterR")
library(tweetscores); library(twitteR)
# take one full day 1% streaming data for Sept 28, 2017 -- 2.58MM unique users for 3,423,287 tweets
# for an example of how to pull 1% streaming using streamR package,
# see https://github.com/wesslen/summer2017-socialmedia/blob/master/day1/twitter-streaming.Rmd
# the id file only needs to include the user profile (actor.id) of the users you want to ping
id <- readr::read_csv("./data/userid20170928.csv")
id$ids <- as.character(id$ids)
# point to your API credential file
# for info to set up, see README of https://github.com/pablobarbera/twitter_ideology
credentials <- "~/TwitterSample/credentials/"
#initialize columns/formats
df <- getUsersBatch(ids=id$ids[1], # choose first one to initialize the dataset
oauth_folder= credentials,
include_entities = TRUE)
#initialize a timestamp to show when the pass completed
df$timestamp <- Sys.time()
# remove all entries to initialize (empty dataframe with correct column formatting)
df <- df[0,]
# set stop time for the experiment
stopTime <- "2017-10-14 00:00:00 EDT"
# file name to save results
fileName <- "~/Twitter-Profile-Experiment/data/experiment.csv"
# while loop to rerun after each pass
while(Sys.time() < stopTime){
userdata <- getUsersBatch(ids = id$ids,
oauth_folder = credentials,
include_entities = TRUE)
userdata$timestamp <- Sys.time() # mark after the users batch completes
df <- rbind(df, userdata) # append the new pass with the previous passes
write_csv(df, fileName) # save the file as a CSV
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment