0

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?

served_raw
  • 92
  • 5
  • From what I understand, each request returns [up to 100 results](https://dev.twitter.com/rest/reference/get/search/tweets), and you got 180 request per 15 minute time frame (= max 18000). – lukeA Sep 17 '17 at 19:41
  • My question is **entirely different**. How many times **twitter API call** is made, with each **search_tweets** method(search_tweets is a method in [rtweet package of R](https://cran.r-project.org/web/packages/rtweet/index.)). – served_raw Sep 17 '17 at 19:55
  • It's not different. As I told you, a call returns up to 100 results. So if you call search_tweets with a maximum of 100 tweets, it's 1 request and you got 179 left in the time frame. Now, you ran several calls with up to 10000 results (= if the call yields 300 results, it's 3 requests). The number of requests is dependent on the number of results. It's also no matter if it's rtweet::search_tweets or twitteR::foobar - it all calls the Twitter API, so read the link. – lukeA Sep 17 '17 at 19:58
  • Now I got it. Thank you so much. :) – served_raw Sep 17 '17 at 20:09

0 Answers0