0

So, I've figured out how to be able to get more than 100 tweets, thanks to How to retrieve more than 100 results using Twitter4j

However, when do I make the script stop and print stop when maximum results have been reached? For example, I set

int numberOfTweets = 512; 

And, it finds just 82 tweets matching my query.

However, because of:

while (tweets.size () < numberOfTweets)

it still continues to keep on querying over and over until I max out my rate limit of 180 requests per 15 seconds.

I'm really a novice at java, so I would really appreciate if you could show me how to resolve this by modifying the first answer script at How to retrieve more than 100 results using Twitter4j

Thanks in advance!

Community
  • 1
  • 1

1 Answers1

0

You only need to modify things in the try{} block. One solution is to check whether the ID of the last tweet you found on the previous loop(previousLastID) in the while is the same as the ID of the last tweet (lastID) in the new batch collected (newTweets). If it is, it means the new batch's elements already exist in the previous array, and that that we have reached the end of possible tweets for this hastag.

try {
      QueryResult result = twitter.search(query);
      List<Status> newTweets = result.getTweets();
      long previousLastID = lastID;
      for (Status t: newTweets) 
        if (t.getId() < lastID) lastID = t.getId();
      if (previousLastID == lastID) {
        println("Last batch (" + tweets.size() + " tweets) was the same as first. Stopping the Gathering process");
        break;
      }
Petros Koutsolampros
  • 2,790
  • 1
  • 14
  • 20