Twitter unfollowers with R and Rook

(This article was first published on Blag's bag of rants, and kindly contributed to R-bloggers)

In my last blog I'm following you in Twitter...are you following me back? I show you how to use the Twitter APIs to get a list of the people that you follow but doesn't follow you back.

This time, I want to extend the tool as I installed the Rook library on my RStudio -:)

So..what is Rook? Nothing more that just a nice Web Server for R...something that I was really missing in R when compared to Ruby (Sinatra, Camping) or Python (Bottle, Flask).

The idea here is that we request a Twitter username and then provide the list with the "Bad People". Here's the source code...

require("Rook")

Get_Twitter_Info<-function(p_source){
web_page<-readLines(p_source)
mypattern = '<id>([^<]*)</id>'
datalines = grep(mypattern,web_page,value=TRUE)
getexpr = function(s,g)substring(s,g,g+attr(g,'match.length')-1)
g_list = gregexpr(mypattern,datalines)
matches = mapply(getexpr,datalines,g_list)
result = gsub(mypattern,'\\1',matches)
names(result) = NULL
return(result)
}

Get_Screen_Name<-function(p_userid){
user_url<-paste("https://api.twitter.com/1/users/lookup.xml?user_id=",
p_userid,"&include_entities=false")
web_page<-readLines(user_url)
mypattern = '<screen_name>([^<]*)</screen_name>'
datalines = grep(mypattern,web_page,value=TRUE)
getexpr = function(s,g)substring(s,g,g+attr(g,'match.length')-1)
g_list = gregexpr(mypattern,datalines)
matches = mapply(getexpr,datalines,g_list)
screen_name = gsub(mypattern,'\\1',matches)
names(screen_name) = NULL
return(screen_name)
}

trim <- function(x){
x<-gsub(' ','',x)
return(x)
}

newapp<-function(env){
req<-Rook::Request$new(env)
res<-Rook::Response$new()
res$write('<form method="POST">\n')
res$write('Enter your Twitter username: <input type="text" name="UserName">\n')
res$write('<input type="submit" name="Get Bad People!">')
res$write('</form>')

People_Id<-""
Bad_People<-c()
Bad_Names<-c()
j<-0

if (!is.null(req$POST())) {
UserName = req$POST()[["UserName"]]

followers_link<-paste("https://api.twitter.com/1/followers/ids.xml?cursor=-1&screen_name=",UserName)
following_link<-paste("https://api.twitter.com/1/friends/ids.xml?cursor=-1&screen_name=",UserName)
followers_link<-trim(followers_link)
following_link<-trim(following_link)
followers<-Get_Twitter_Info(followers_link)
following<-Get_Twitter_Info(following_link)

for(i in 1:length(following)) {
j<-j+1
if(j>=100){
j<-0
People_Id<-substring(People_Id,2)
Bad_People<-Get_Screen_Name(People_Id)
Bad_Names<-append(Bad_Names,Bad_People)
People_Id<-""
}
Match<-following[i] %in% followers
if(Match == TRUE){
}
else{
following[i]<-trim(following[i])
People_Id<-paste(People_Id,following[i],sep=",")
}
}
}
for(i in 1:length(Bad_Names)) {
res$write(paste(' ',Bad_Names[i],sep=' '))
res$write('<BR>')
}
res$finish()
}

server = Rhttpd$new()
server$add(app = newapp, name = "Twitter_Rook")
server$start()
server$browse("Twitter_Rook")

When we run the code, the browser will open automatically showing us the application.



For my first use of Rook, I think the application looks pretty nice...and hope those that doesn't follow me back...start doing it -:P

Greetings,

Blag.


To leave a comment for the author, please follow the link and comment on his blog: Blag's bag of rants.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.