lunes, 2 de diciembre de 2013

Twitter Battle

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('<form method="POST">\n')
  res$write('Enter your Twitter username: <input type="text" name="YourUserName">')
  res$write('</BR>')
  res$write('Enter your his/her Twitter username: <input type="text" name="HisHerUserName">')
  res$write('</BR>')
  res$write('<input type="submit" name="Start the Battle!">')
  res$write('</form>')
    
  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('<H1>',message,'</H1>'))   
    }
    if(YourPercents < HisHerPercents){
      message<-paste(req$POST()[["YourUserName"]],"is",DiffPercent,
                     " % less important on Twitter than",req$POST()[["HisHerUserName"]],sep=" ")
      res$write(paste('<H1>',message,'</H1>'))
    }
    if(YourPercents == HisHerPercents){
      message<-paste(req$POST()[["YourUserName"]],
                     "is equally important on Twitter as",req$POST()[["HisHerUserName"]],sep=" ")
      res$write(paste('<H1>',message,'</H1>'))
    }
    
    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("<div align='center'>")
    res$write(paste("<img src='", server$full_url("pic"), "/", 
                    "Twitter_Battle.png'", "/>", sep = ""))
    
    res$write("</div>")
}
  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.

No hay comentarios: