Created
June 27, 2012 08:46
-
-
Save tts/3002512 to your computer and use it in GitHub Desktop.
Return tweets from Twitter users that are members of a given list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Return tweets from Twitter users that are members of a given list | |
# | |
# Example list: https://twitter.com/#!/niku_hooli/ylen-suomitop100-lista/ | |
# | |
# Tuija Sonkkila | |
# 2012-06-27 | |
# | |
library(RCurl) | |
library(RJSONIO) | |
library(twitteR) | |
owner <- "niku_hooli" | |
list.slug <- "ylen-suomitop100-lista" | |
# Function for returning a 'cursor amount' of member names, with the value of next_cursor | |
get_list_screen_names <- function(list.slug, owner, cursor) { | |
u <- paste("https://api.twitter.com/1/lists/members.json?slug=", list.slug, | |
"&owner_screen_name=", owner, "&cursor=", cursor, sep = "") | |
json <- getURL(u) | |
dat <- fromJSON(json) | |
m <- sapply(dat$users, function(d) d$screen_name) | |
cursor <- dat$next_cursor | |
output <- list(cursor, m) | |
return(output) | |
} | |
# Function for returning the last 200 tweets of a user | |
get_tweets <- function(user) { | |
u <- paste("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true", | |
"&screen_name=", user, "&count=200", sep = "") | |
json <- getURL(u) | |
dat <- fromJSON(json) | |
sapply(dat, function(d) d$text) | |
} | |
# Initialize vector for member names | |
p.all <- character(0) | |
# Cursor's initial value | |
cursor <- -1 | |
# At the end of the list, the cursor turns to 0 | |
while(cursor != 0) { | |
output <- get_list_screen_names(list.slug, user, cursor) | |
cursor <- output[[1]] | |
# Except the first element of the list, all others are member names | |
p <- output[[-1]] | |
# Append to previous names | |
p.all <- c(p, p.all) | |
} | |
# Initialize a character vector to store tweets | |
tw.all <- character(0) | |
# Get tweets by user | |
for (i in 1:length(p.all)) | |
{ | |
tw <- get_tweets(p.all[i]) | |
tw.all <- c(tw, tw.all) | |
} | |
# How many tweets have we got? | |
length(tw.all) | |
# In this example, 20337 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment