Skip to content

Instantly share code, notes, and snippets.

@tts tts/gist:3002512
Created Jun 27, 2012

Embed
What would you like to do?
Return tweets from Twitter users that are members of a given list
# 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
You can’t perform that action at this time.