An API to count the number of tweets for a search

willf
@willf Will Fitzgerald

I'd find it useful to have a count API that would return the number of tweets for a particular query, especially with a time-range. I tried writing the following Ruby code, but it returns inconsistent results:

  1. require 'twitter'
  2. require 'date'
  3. def topic_search_count_by_day (search, date_string)
  4.   page = 0
  5.   count = 0
  6.   while true
  7.     begin
  8.       count += Twitter.search("search", :page=>page+=1, :rpp => 100, :result_type => "recent", :since=>Date.parse(date_string), :until=>Date.parse(date_string)+1).size
  9.     rescue => e
  10.       puts e.inspect
  11.       return count
  12.     end
  13.   end
  14. end

That is, page through the tweets until an error is reached (typically "Invalid query", which seems to appear at the nth+1 page of queries.

Is there a path to this API idea?

If not, is there some way to get consistent results with the search API?

Here are some examples of the inconsistent results:

  1. >> topic_search_count_by_day "obama :)", "2012-05-11"
  2. #<Twitter::Error::Forbidden: Invalid query>
  3. => 1589
  4. >> topic_search_count_by_day "obama :)", "2012-05-12"
  5. #<Twitter::Error::Forbidden: Invalid query>
  6. => 1486
  7. >> topic_search_count_by_day "romney :)", "2012-05-12"
  8. #<Twitter::Error::Forbidden: Invalid query>
  9. => 1486
  10. >> topic_search_count_by_day "fitzgerald :)", "2012-05-12"
  11. #<Twitter::Error::Forbidden: Invalid query>
  12. => 2486
  13. >> topic_search_count_by_day "asdfasdftritter :)", "2012-05-12"
  14. #<Twitter::Error::Forbidden: Invalid query>
  15. => 1686 # seems very unlinkely...
  16. <code>
1 year 48 weeks ago

Replies

matsubokkuri
@matsubokkuri Yuki Matsukura

Thank you for the code snippet. It's great help for me.

I fond minor mistake on your code.
Twitter.search("search"...
should be
Twitter.search(search...

1 year 11 weeks ago
Zyzz75
@Zyzz75 PoniniOfiecial

Thank you for the code snippet. It's great help

1 year 11 weeks ago
willf
@willf Will Fitzgerald

Oh, man, I can't believe I quoted "search" !

1 year 11 weeks ago