Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 58 lines (42 sloc) 1.384 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#This R (helper) function queries the Twitter Search API
#
#https://dev.twitter.com/docs/api/1/get/search
#
#by hashtags.
#
#It returns co-occurent user mentions for each tweet (id)
#and user as data.frame for up to 1.500 Tweets.
#
#NB: Provision of query without hashtag "#"

#dependencies
require(RJSONIO)
require(RCurl)

#function

hash.search.rt <- function (query) {
  
  url <- paste("http://search.twitter.com/search.json?q=%23",query,
               "&rpp=50&include_entities=true",sep="")
  
  my.data <- data.frame()
  df.tmp <- data.frame()
  
  for (k in 1:30) {
    url.d <- paste(url,"&page=",k,sep="")
    my.json <- fromJSON(getURL(url.d), nullValue = NA)
    
    if(length(my.json$results) == 0)
      break
    else
      
      for (i in seq_along(my.json$results)){
        id <- my.json$results[[i]]$id
        from_user <- my.json$results[[i]]$from_user
        user.Ment <- my.json$results[[i]]$entities$user_mentions
        
        user <- c()
        
        for (j in seq_along(user.Ment)) {
          
          user.tmp <- user.Ment[[j]]$screen_name
          user <- c(user, user.tmp)
          
        }
        id <- rep(id,times=length(user))
        from_user <- rep(from_user,times=length(user))
        
        df.tmp <- cbind(id, from_user, user)
        
        my.data <- rbind(my.data, df.tmp)
        
      }
  }
  return(my.data)
}
Something went wrong with that request. Please try again.