Twitter unfollowers with R and Rook

[This article was first published on Blag's bag of rants, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 = '([^<]*)'
  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 = '([^<]*)'
  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('
\n') res$write('Enter your Twitter username: \n') res$write('') res$write('
') 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('
') } 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 their blog: Blag's bag of rants.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)