Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lookup Twitter followers in R

Tags:

r

twitter

I'd like to look up the profiles of a user's Twitter followers using R (followers > 100000). Although twitteR is a great package, it has problems when dealing with high levels of followers as one needs to implement a sleep routine to avoid exceeding the rate limits. I am a relative novice here and wondered how one might loop through the follower ID object, entering in follower ids in batches of 100 (as this is the max the Twitter API can process at a time)?

Edit: code added (twitteR) library(plyr) maxTwitterIds = 100 sleeptime = 500 # sec

user<-getUser("[username]")
followers<-zz$getFollowerIDs()
ids_matrix = matrix(zz, nrow = maxTwitterIds, ncol = length(zz) / maxTwitterIds)
followers<-zz$getFollowerIDs()
#note: for smaller lists of followers it is possible to use the command "lookupUsers(zz)     at this point
foll<-getTwitterInfoForListIds = function(id_list) {
    return(lapply(id_list, 

names <- sapply(foll,name)
sn<sapply(foll,screenName)
id<-sapply(foll,id)
verified<-sapply(foll,erified)
created<-sapply(foll,created)
statuses<-sapply(foll,statusesCount)
follower<-sapply(foll,followersCount)
friends<-sapply(foll,friendsCount)
favorites<-sapply(foll,favoritesCount)
location<-sapply(foll,location)
url<-sapply(foll,url)
description<-sapply(foll,description)
last_status<-sapply(foll,lastStatus)))
}
alldata = alply(, 2, function(id_set) {
    info = getTwitterInfoForListIds(id_set)
    Sys.sleep(sleeptime)   
    return(info)
})
like image 612
Mike Jensen Avatar asked Nov 13 '22 10:11

Mike Jensen


1 Answers

This can also be done using the newer rtweet package.

Per the example here: https://github.com/mkearney/rtweet

# Get followers 

# Retrieve a list of the accounts following a user.

## get user IDs of accounts following CNN 
cnn_flw <- get_followers("cnn", n = 75000)

# lookup data on those accounts 
cnn_flw_data <- lookup_users(cnn_flw$user_id) 

# Or if you really want ALL of their followers:
# how many total follows does cnn have? 
cnn <- lookup_users("cnn")
# get them all (this would take a little over 5 days) 
cnn_flw <- get_followers(   "cnn", n = cnn$followers_count, 
  retryonratelimit = TRUE )
like image 132
Jon Spring Avatar answered Nov 16 '22 17:11

Jon Spring