Rescale.gtrends = function(df.t1,df.t2){
  df.t1 = df.t1$interest_over_time
  df.t2 = df.t2$interest_over_time
  # In order to rescale, we look at the overlapping time span and try to find the best mutliplicative scaling factor, using a linear regression, without constant. Not sure if there are better ways to do this
  match1 = df.t1[match(df.t2$date,df.t1$date),]
  match1 = match1[!is.na(match1$date),]
  match2 = df.t2[match(df.t1$date,df.t2$date),]
  match2 = match2[!is.na(match2$date),]
  rescale.factor = lm(data=match1,match2$hits ~ hits+0) 
  df.t1$hits = round(predict(rescale.factor,newdata = df.t1),1) 
  df.t1=df.t1[df.t1$date<min(df.t2$date),]
  hits = as.numeric(c(df.t1$hits,df.t2$hits))
  return(hits)}

pft_ask_google =function(keyword,country_of_interest="DE",from="2010-07-31",to="2017-07-31",status= 1,prefix="g.trends.",gprop = "web") {
  
  # Splitting the time span into two spans, each < 5 years
  time.span1 =  paste(as.Date(from), as.Date(from)+(5*365-1))
  time.span1.dates = ISOweek(seq(from=as.Date(from),to=as.Date(from)+(5*365-1), by=7))
  time.span2 = paste(as.Date(to)-(5*365-1), as.Date(to))
  time.span2.dates = ISOweek(seq(from=as.Date(as.Date(to)-(5*365-1)),to=as.Date(to), by=7))
  time.span.dates.combined = unique(c(time.span1.dates,time.span2.dates))
  
  
  google.input.data = as.data.frame(
    matrix(nrow = length(time.span.dates.combined),
           ncol = 1+length(keyword)))
  google.input.data[,1] =  time.span.dates.combined
  
  
  for(p in 1:length(keyword)){
    if(status==1){cat("asking Google for statistics for",keyword[p]," - ",round(p/length(keyword),3)*100,"%" ,"\n")}
    tryCatch({ 
      google.temp.t1 = gtrends(keyword = keyword[p],
                               geo = country_of_interest,
                               time= time.span1 ,gprop = gprop) 
      # Sys.sleep(0.1) 
      google.temp.t2 = gtrends(keyword = keyword[p],
                               geo = country_of_interest,
                               time= time.span2 ,gprop = gprop) 
      
      # Rescaling the older data set to match (more or less) with the more recent data set
      hits = Rescale.gtrends(df.t1=google.temp.t1,df.t2=google.temp.t2)
      google.input.data[,p+1] = as.numeric(hits)
      error.rate <<- 0
    },  error=function(e) { 
      if(status==1){cat("\n Uups...Something went wrong with",keyword[p],"\n")  }
      Sys.sleep(0.5)
    })
    
  }
  
  names(google.input.data) = c("date",paste(prefix,gsub(" ","\\.",keyword),sep=""))
  return(google.input.data)
}