0

I want my app to search tweets with a specific #tag on Twitter every few minutes, like this:

results = client.search("#mypopulartag")

However, I don't want to do a full search each time. In building the app, I've encountered the Twitter::TooManyRequests error, because it returns a lot of results (presumably the Twitter gem makes as many requests to Twitter as needed for one client.search() call).

I don't need it to search super deep each time. Can I pass in the max_id parameter to the client.search method, so I don't waste API calls?

bevanb
  • 8,201
  • 10
  • 53
  • 90

1 Answers1

0

Yes, if you keep track of the id of the most recent tweet that you've processed, you can get all tweets since then with something like this (using gem version 5.13):

  client.search(
    "#mypopulartag",
    result_type: 'recent',
    since_id: since_id   # your last processed id
  ).take(15)

Just keep in mind that if there are, say, 60 results, you'll need to execute more client.search calls to get all the tweets. For those calls, you'd want to also specify a max_id equal to the last tweet id that was processed in the current search.

keyzee
  • 441
  • 3
  • 7
  • Ah, I see. But why would I have to execute more `client.search` calls if there are many (~60) new results since my last search? My understanding is that `client.search` (with the `.take(15)` omitted) makes as many requests as necessary to the Twitter search API- is that incorrect? – bevanb Feb 22 '15 at 16:08
  • The reason I think the gem makes many requests to Twitter for one `client.search` call is because when I do `client.search("#nike")` or any other very popular tag, it takes forever and returns a bunch of results, and if I make another request immediately after, it gives me `Twitter::Error::TooManyRequests`. – bevanb Feb 22 '15 at 16:17
  • Just tried `client.search("#tag", result_type: "recent", since_id: id_of_my_last_saved_tweet)` and it returned all 4500 results (only about 20 tweets were made since my last query. I also tried passing a max_id, but it also did not seem to have an effect. Any ideas why those parameters do not seem to be working? Thanks. – bevanb Feb 22 '15 at 17:35
  • What version of the gem are you using? – keyzee Feb 22 '15 at 18:03
  • Here's a sample that works for me: `#get the id of the third tweet` `three_tweets_ago = client.search("#hashtag", result_type: "recent").take(10)[2].id` `client.search("#hashtag", result_type: "recent", since_id: three_tweets_ago).count` `# result is 3, as expected` – keyzee Feb 22 '15 at 19:36