Return number of tweets in a Search

10 views
Skip to first unread message

Steve

unread,
Jan 12, 2011, 7:39:27 AM1/12/11
to Twitter Development Talk
Hi, I'm developing an application using the Twitter Ruby gem in Rails,
and was wondering whether there was a way to return the number of
tweets containing a certain hashtag?

I'm looking to display random tweets on a page which contain a
particular hashtag (I know there are issues with hashtag abuse, but
this is an internal application) when being pulled in from a Twitter
Search API call.

I can display a random tweet using the following code, but how would
you go about modifying it so that the maximum random is the number of
search results? So if there were 2500 results on one query but 30,000
on another, is there a way to change the random max dynamically based
on the number available?

>> search.hashtag("somehashtag").since_id("2011-01-01").until_date("2011-01-11").result_type("recent").per_page(1).page(rand(1500))

Setting the random as 1,000,000 and then looping through when you hit
an HTTP error probably isn't the best approach ;)

Thanks for any help!

Steve

@Red_Eyes

unread,
Jan 12, 2011, 8:04:22 PM1/12/11
to twitter-deve...@googlegroups.com
The maximum number of tweets you can return per page is 100. If you return all the pages available you can get to around 1,500 max. These limits are defined in the search API documentation. (http://dev.twitter.com/doc/get/search).
 
You may be better either looking at the streaming API and hope some of the Tweets you are looking for turn up, or use your random number generator to search "n" times and pull back 100 Tweets at a go. You would have to set an interval between searches otherwise you are likley to pull back the same Tweet more than once, especially if it is a seldom used hashtag.
 
Perhaps if you have a number of hashtags you are looking for, you could "round robin" through all of them with a suitable delay between each one (still checking for duplicates)... or possibly check for the same hashtag at the same time each day if you are looking for some sort of frequency indicator. Alternatively add some sort of randomness to the date range per your example.
 
Paul

Steve

unread,
Jan 13, 2011, 6:15:52 AM1/13/11
to Twitter Development Talk
The app is a competition system, so really I need to try and retrieve
all entries of a hashtag if possible (but if it's not possible, then
other comp systems mustn't either, surely?)

Thanks for the suggestions - might try and utilise DataSift for it.

Sarah - DataSift

unread,
Jan 20, 2011, 10:44:00 AM1/20/11
to Twitter Development Talk
Something that you might find interesting as a developer is this
recent announcement from DataSift:

Funding for DataSift Developers: http://bit.ly/hOYpNh

And

DataSift to Twitter open source code: http://bit.ly/gDo2wA

Enjoy!
Reply all
Reply to author
Forward
0 new messages