Twitter Battle

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

I love to have fun with R and Twitter…there are a lot of cool things that you can do with it…so I just thought of having a small Twitter Battle application…something that will grab the number of followers and lists from two users…apply some crappy algorithms and determine the Twitter importance between those users…using our .RData file that holds the Twitter OAuth info…

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

setwd("C:/Blag/R_Scripts/Important_Scripts")
load("credentials.RData")

Get_Percentages<-function(p_one,p_two,flag){
      if(p_one > p_two){
        if(flag == 0){
          OneFollowPercent<-100
          TwoFollowPercent<-round((p_two * 100) / p_one)
        }else{
          TwoFollowPercent<-100 - (round((p_two * 100) / p_one))
          if(TwoFollowPercent <= 49){
            OneFollowPercent<-50 + TwoFollowPercent
            TwoFollowPercent<-100 - OneFollowPercent
            flag<-0
          }else{
            OneFollowPercent<-50 + TwoFollowPercent
            TwoFollowPercent<-100 - TwoFollowPercent
            TwoFollowPercent<-100 - round((round(TwoFollowPercent * 100) / OneFollowPercent))
            OneFollowPercent<-TwoFollowPercent
            TwoFollowPercent<-100 - OneFollowPercent
            flag<-1
          }
        }
      }
      if(p_one < p_two){
        if(flag == 0){
          OneFollowPercent<-round((p_one * 100) / p_two)
          TwoFollowPercent<-100
        }else{
          OneFollowPercent<-100 - (round((p_one * 100) / p_two))
          if(OneFollowPercent <= 49){
            TwoFollowPercent<-50 + OneFollowPercent
            OneFollowPercent<-100 - TwoFollowPercent
            flag<-0
          }else{
            TwoFollowPercent<-50 + OneFollowPercent
            OneFollowPercent<-100 - OneFollowPercent
            OneFollowPercent<-100 - round((round(OneFollowPercent * 100) / TwoFollowPercent))
            TwoFollowPercent<-OneFollowPercent
            OneFollowPercent<-100 - TwoFollowPercent
            flag<-1
          }
        }
      }
      if(p_one == p_two){
        OneFollowPercent<- 50
        TwoFollowPercent<- 50
      }
      percents<-c(OneFollowPercent,TwoFollowPercent,flag)
      return(percents)
}

newapp<-function(env){
  req<-Rook::Request$new(env)
  res<-Rook::Response$new()
  res$write('
\n') res$write('Enter your Twitter username: ') res$write('
') res$write('Enter your his/her Twitter username: ') res$write('
') res$write('') res$write('
') if (!is.null(req$POST())) { YourUserName = paste("@",req$POST()[["YourUserName"]],sep="") HisHerUserName = paste("@",req$POST()[["HisHerUserName"]],sep="") reg<-registerTwitterOAuth(credentials) GetYourUser<-getUser(YourUserName,cainfo="cacert.pem") GetHisHerUser<-getUser(HisHerUserName,cainfo="cacert.pem") GetYourFollowers<-GetYourUser$followersCount GetYourLists<-GetYourUser$listedCount GetHisHerFollowers<-GetHisHerUser$followersCount GetHisHerLists<-GetHisHerUser$listedCount FollowPercents<-Get_Percentages(GetYourFollowers,GetHisHerFollowers,0) ListPercents<-Get_Percentages(GetYourLists,GetHisHerLists,0) YourPercents<-FollowPercents[1] + ListPercents[1] HisHerPercents<-FollowPercents[2] + ListPercents[2] FinalPercents<-Get_Percentages(YourPercents,HisHerPercents,1) YourPercents<-FinalPercents[1] HisHerPercents<-FinalPercents[2] if(FinalPercents[3] == 0){ DiffPercent<-abs(YourPercents - 50) }else{ DiffPercent<-FinalPercents[1] } if(YourPercents > HisHerPercents){ message<-paste(req$POST()[["YourUserName"]],"is",DiffPercent, " % more important on Twitter than",req$POST()[["HisHerUserName"]],sep=" ") res$write(paste('

',message,'

')) } if(YourPercents < HisHerPercents){ message<-paste(req$POST()[["YourUserName"]],"is",DiffPercent, " % less important on Twitter than",req$POST()[["HisHerUserName"]],sep=" ") res$write(paste('

',message,'

')) } if(YourPercents == HisHerPercents){ message<-paste(req$POST()[["YourUserName"]], "is equally important on Twitter as",req$POST()[["HisHerUserName"]],sep=" ") res$write(paste('

',message,'

')) } pieValues<-c(YourPercents,HisHerPercents) pieNames<-c(paste(req$POST()[["YourUserName"]],YourPercents,"%"), paste(req$POST()[["HisHerUserName"]],HisHerPercents,"%")) png("Twitter_Battle.png",width=1000,height=700) pie(pieValues,labels=pieNames,main="Twitter Battle") dev.off() res$write("
") res$write(paste("", sep = "")) res$write("
") } res$finish() } server = Rhttpd$new() server$add(app = newapp, name = "Twitter_Battle") server$add(app = File$new("C:/Blag/R_Scripts/Important_Scripts"), name = "pic") server$start() server$browse("Twitter_Battle")

When we execute it…we’re going to have some interesting results…

Have fun with it -;)
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)