For each search_tweets() call how many time API call to twitter is made, does it depend on number of tweets fetched?

358 views Asked by At

I'm new to R programming or twitter API's.

When I'm running the following piece of code:

#https://cran.r-project.org/web/packages/rtweet/index.html
#http://127.0.0.1:1410

#Twitter Data Analysis
twitter_packages <- c("rtweet","httpuv")
### checking if packages are already installed and installing if not
check.install.load.Package<-function(package_name){
  if(!package_name%in%installed.packages()){
    install.packages(package_name)

  }
  library(package_name,character.only = TRUE)
}
for(package in twitter_packages){
  check.install.load.Package(package)
}



## whatever name you assigned to your created app
#appname <- "Tech Dope"
appname <- "RTestApplicationSub"

## api key (example below is not a real key)
#key <- "XXXXXXXXXXXXX"
key <- "XXXXXXXXXXXXX"

## api secret (example below is not a real key)
#secret <- "XXXXXXXXXXXXX"
secret <- "XXXXXXXXXXXXX"

## create token named "twitter_token"
twitter_token <- create_token(
  app = appname,
  consumer_key = key,
  consumer_secret = secret)


india_india <- get_trends("India")

for(india in as.vector(india_india$trend)){
  gc()

  ## create token named "twitter_token"
  # twitter_token <- create_token(
  #   app = appname,
  #   consumer_key = key,
  #   consumer_secret = secret)

  fetchedTweets=search_tweets(india,10000,include_rts = FALSE,type = "mixed")
  write.csv(fetchedTweets,paste0("C:\\Users\\Subhrajit\\Documents\\R Scripts\\MY_DATA_SETS\\TEST_DATA_TWITTER\\",india,".csv"), row.names = FALSE)
}

fetchedTweets=search_tweets("rohingya india",10000,include_rts = FALSE,type = "mixed")
write.csv(fetchedTweets,paste0("C:\\Users\\Subhrajit\\Documents\\R Scripts\\MY_DATA_SETS\\TEST_DATA_TWITTER\\rohingya.csv"), row.names = FALSE)
class(fetchedTweets)

#write.csv(fetchedTweets,"C:\\Users\\Subhrajit\\Documents\\R Scripts\\MY_DATA_SETS\\TEST_DATA_TWITTER\\Aadhar.csv")

I'm getting this error:

Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Finished collecting tweets!
Searching for tweets...
Error in x[[1]] : subscript out of bounds
In addition: Warning messages:
1: rate limit exceeded. 
2: rate limit exceeded. 

Now as per twitter API documentation API call's are limited to Requests / 15-min window (user auth) 180 Requests / 15-min window (app auth) 450

But in my case, I'm getting "rate limit exceeded" after search_tweets() is called 6 times.

I could avoid the error by re-writing the loop as:

i=1;
for(india in as.vector(india_india$trend)){
  gc()
  i<-i+1
  if(i%%5==0){
    Sys.sleep(600)
  }
  ## create token named "twitter_token"
  # twitter_token <- create_token(
  #   app = appname,
  #   consumer_key = key,
  #   consumer_secret = secret)

  fetchedTweets=search_tweets(india,10000,include_rts = FALSE,type = "mixed")
  write.csv(fetchedTweets,paste0("C:\\Users\\Subhrajit\\Documents\\R Scripts\\MY_DATA_SETS\\TEST_DATA_TWITTER\\",india,".csv"), row.names = FALSE)
}

My Questions are as follows: 1) Is for each search_tweets() call how many time API call to twitter is made? 2) Does it depend on number of tweets fetched?

0

There are 0 answers