Every 5 minutes get the google trend data

398 Views Asked by At

I would like to extract the google trend data through package "gtrendsR" every hour, I tried to use Sys.sleep() function to set the timer, however, it is failure for me to download it every hour. So, how do I correct my code in order to get the data every hour. Thanks a lot!

Sys.setlocale("LC_ALL", "English")
Keywords = c("google", "twitter")

for (k in Keywords) {
  res = NULL

  temp_1 <- gtrends(k, geo = "US",time = "all")
  temp_2 <- temp_1$interest_over_time
  res <- rbind(res, temp_2)
  rm(temp_1,temp_2)

  res <- select (res, c(date, hits))

  Sys.setlocale(category = "LC_ALL", locale = "cht")
  names(res)[2]<- k
  xfilepath = paste("C:/Users/Peter/Desktop/",k,".csv",sep="")
  write.csv(res, file = xfilepath, row.names=FALSE)

}
1

There are 1 best solutions below

0
Oliver On

Sys.sleep() should be working, and without an error code it is hard to tell you exactly why it is failing. I'll suggest an alternative method however.

The later package is a simple and nice utility package for executing code, well.. Later. It takes a function without any arguments, and runs it at a set delay. For example you could use:

library("gtrendsR")
library("later")
library("data.table") #for as.ITime
Sys.setlocale("LC_ALL", "English")
Keywords = c("google", "twitter")
#Set delay. Here for 5 seconds
delay <- as.ITime("00:00:05")
Interrupt <- FALSE

extractGoogle <- function(){
    for (k in Keywords) {
        res = NULL
        temp_1 <- gtrends(k, geo = "US",time = "all")
        temp_2 <- temp_1$interest_over_time
        res <- rbind(res, temp_2)
        rm(temp_1,temp_2)
        res <- select (res, c(date, hits))
        Sys.setlocale(category = "LC_ALL", locale = "cht")
        names(res)[2]<- k
        xfilepath = paste("C:/Users/Peter/Desktop/",k,".csv",sep="")
        write.csv(res, file = xfilepath, row.names=FALSE)
    }
    #Execute once again later
    if(isFALSE(Interrupt))
        later(extractGoogle, delay = delay)
}
#Run the function until Interrupt is set to TRUE or session closes
extractGoogle()

This allows you to set the delay manually, changing 'delay' to a number of seconds. as.ITime simple lets you specify the number of seconds in a simple format. The cycle can then be delayed further or broken by changing the global variables.