Twitter unfollowers with R and Rook – Revisited

[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.

Some time ago I wrote a post called Twitter unfollowers with R and Rook where I used R and Twitter to get a list of the people that we follow…but that doesn’t follow us back…

Right now…that post is obsolete as Twitter changed its API to API 1.1 which means that OAuth authentication must be used…

So…of course…we’re going to use that for our new version of Twitter Unfollowers -;)

Getting Twitter Auth
library("ROAuth")
library("twitteR")

setwd("C:/Blag/R_Scripts/Important_Scripts")

options(RCurlOptions = list( capath = system.file("CurlSSL", "cacert.pem", package = "RCurl"), ssl.verifypeer = FALSE))

reqURL<-"https://api.twitter.com/oauth/request_token"

accessURL<-"http://api.twitter.com/oauth/access_token"

authURL<-"http://api.twitter.com/oauth/authorize"

consumerKey<-"Your own Consumer Key"

consumerSecret<-"Your own Consumer Secret"

download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")

credentials<-OAuthFactory$new(consumerKey=consumerKey,
                              consumerSecret=consumerSecret,
                              requestURL=reqURL,
                              accessURL=accessURL,
                              authURL=authURL)

credentials$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))

save(credentials, file="credentials.RData")

What are we doing here is simply get Authorization from Twitter and save that information as an RData file that we can use later on. This script is needed to be executed only once...and will ask you to enter a PIN number to validate the connection...

After that...we're ready to go -;)

Twitter Unfollowers
require("Rook")
library("ROAuth")
library("twitteR")

setwd("C:/Blag/R_Scripts/Important_Scripts")
load("credentials.RData")
registerTwitterOAuth(credentials)
Get_Screen_Name<-function(p_userid){
  SomeUser<-getUser(p_userid,cainfo="cacert.pem")
  SomeUser<-SomeUser$screenName
  return(SomeUser)
}

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"]] User<-userFactory$new(screenName=UserName) followers<-User$getFollowerIDs(n=NULL,cainfo="cacert.pem") following<-User$getFriendIDs(n=NULL,cainfo="cacert.pem") for(i in 1:length(following)) { Match<-following[i] %in% followers if(Match == TRUE){ } else{ Bad_Person<-Get_Screen_Name(following[i]) res$write(paste(' ',Bad_Person,sep=' ')) res$write('
') } } } res$finish() } server = Rhttpd$new() server$add(app = newapp, name = "Twitter_Rook") server$start() server$browse("Twitter_Rook")

Thanks to this...I was able to shrink the code from 85 lines of the previous version to only 51 lines of code...nice achievement if you ask me -;)

So...when we execute the code...we will have this...

Of course...I don't expect them all to follow me back as they are mostly companies...but for people...follow me back or get unfollowed -: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)