Skip to content

Instantly share code, notes, and snippets.

@tts
Created June 27, 2012 07:31
Show Gist options
  • Save tts/3002229 to your computer and use it in GitHub Desktop.
Save tts/3002229 to your computer and use it in GitHub Desktop.
Returning the screen names of Twitter list members by the cursor
# Returning the screen names of members of a Twitter list by the cursor
#
# Example list https://twitter.com/#!/niku_hooli/ylen-suomitop100-lista/
#
# Tuija Sonkkila
# 2012-06-27
#
# Twitter API https://dev.twitter.com/docs/api/1/get/lists/members
# Function adapted from http://lists.hexdump.org/pipermail/twitter-users-hexdump.org/2011-December/000015.html
library(RCurl)
library(RJSONIO)
library(twitteR)
owner <- "niku_hooli"
list.slug <- "ylen-suomitop100-lista"
# Return screen names, and the next_cursor value
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
# Put all in a list, and return
output <- list(cursor, m)
return(output)
}
# Initialize a character vector for the names
p.all <- character(0)
# Initial cursor value
cursor <- -1
# Loop over the membership list until next_cursor=0, ie there is no more
while(cursor != 0) {
output <- get_list_screen_names(list.slug, owner, cursor)
# next_cursor is the first element in the list returned by the function
cursor <- output[[1]]
# all the other elements are screen names
p <- output[[-1]]
# append to previous names
p.all <- c(p, p.all)
}
sort(p.all)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment