Class: TwitterSearchWatcher
- Inherits:
-
Object
- Object
- TwitterSearchWatcher
- Defined in:
- lib/twitter-search-watcher.rb
Constant Summary collapse
- TWITTER_SEARCH_URL =
'http://search.twitter.com/search.json'- DEFAULT_USER_AGENT =
'TwitterSearchWatcher RubyGem http://github.com/devfu/twitter-search-watcher'- QUERY_STRING_ATTRIBUTES =
[ :q, :to, :from, :since_id, :page, :max_id, :rpp ]
Instance Attribute Summary collapse
-
#check_every ⇒ Object
The number of seconds to wait between Twitter calls.
-
#from ⇒ Object
The username of someone you want to search replies from.
-
#max_id ⇒ Object
Used for pagination, so you can get page=3 where the max_id of the first page was 1234.
-
#max_pages ⇒ Object
The maximum number of pages to check for tweets.
-
#page ⇒ Object
Get a particular page of Twitter search results (pagination).
-
#q ⇒ Object
A string you want to search twitter for.
-
#rpp ⇒ Object
Number of results per page (max 100).
-
#since_id ⇒ Object
Only get tweets with ID’s greater than this ID (useful for only getting new tweets).
-
#to ⇒ Object
The username of someone you want to search replies to.
-
#user_agent ⇒ Object
The User-Agent header value to send along with all Twitter Search API requests.
Class Method Summary collapse
-
.watch!(search_string, options = nil, &block) ⇒ Object
Instantiates a new TwitterSearchWatcher given the search_string and options and then calls #watch on the instance using the block given.
Instance Method Summary collapse
-
#initialize(search_string = nil, options = nil) ⇒ TwitterSearchWatcher
constructor
Create a new TwitterSearchWatcher.
-
#search!(additional_parameters = nil) ⇒ Object
Performs a search.
-
#search_more!(response, additional_parameters = nil) ⇒ Object
Performs a search, given the response from another search.
-
#search_newer!(response = nil, additional_parameters = nil) ⇒ Object
Performs a search, given the response from another search.
-
#search_url(additional_parameters = nil) ⇒ Object
Returns the URL we’ll use to call the Twitter Search API.
-
#watch!(additional_parameters = nil, &block) ⇒ Object
Starts watching this search in a loop.
Constructor Details
#initialize(search_string = nil, options = nil) ⇒ TwitterSearchWatcher
Create a new TwitterSearchWatcher
TwitterSearchWatcher.new 'string to search'
TwitterSearchWatcher.new 'string to search', :check_every => 60
TwitterSearchWatcher.new :to => 'barackobama', :from => 'SenJohnMcCain'
57 58 59 60 61 62 63 64 65 |
# File 'lib/twitter-search-watcher.rb', line 57 def initialize search_string = nil, = nil if search_string.is_a? Hash = search_string else self.q = search_string end .each {|k,v| send "#{k}=", v } if end |
Instance Attribute Details
#check_every ⇒ Object
The number of seconds to wait between Twitter calls. Default: 60 (seconds)
35 36 37 |
# File 'lib/twitter-search-watcher.rb', line 35 def check_every @check_every end |
#from ⇒ Object
The username of someone you want to search replies from
19 20 21 |
# File 'lib/twitter-search-watcher.rb', line 19 def from @from end |
#max_id ⇒ Object
Used for pagination, so you can get page=3 where the max_id of the first page was 1234
26 27 28 |
# File 'lib/twitter-search-watcher.rb', line 26 def max_id @max_id end |
#max_pages ⇒ Object
The maximum number of pages to check for tweets
If nil, we’ll check until there are no more pages (when :next_page isn’t present)
40 41 42 |
# File 'lib/twitter-search-watcher.rb', line 40 def max_pages @max_pages end |
#page ⇒ Object
Get a particular page of Twitter search results (pagination). Typically used in conjunction with :max_id
23 24 25 |
# File 'lib/twitter-search-watcher.rb', line 23 def page @page end |
#q ⇒ Object
A string you want to search twitter for
13 14 15 |
# File 'lib/twitter-search-watcher.rb', line 13 def q @q end |
#rpp ⇒ Object
Number of results per page (max 100)
32 33 34 |
# File 'lib/twitter-search-watcher.rb', line 32 def rpp @rpp end |
#since_id ⇒ Object
Only get tweets with ID’s greater than this ID (useful for only getting new tweets)
29 30 31 |
# File 'lib/twitter-search-watcher.rb', line 29 def since_id @since_id end |
#to ⇒ Object
The username of someone you want to search replies to
16 17 18 |
# File 'lib/twitter-search-watcher.rb', line 16 def to @to end |
#user_agent ⇒ Object
The User-Agent header value to send along with all Twitter Search API requests
10 11 12 |
# File 'lib/twitter-search-watcher.rb', line 10 def user_agent @user_agent end |
Class Method Details
.watch!(search_string, options = nil, &block) ⇒ Object
Instantiates a new TwitterSearchWatcher given the search_string and options and then calls #watch on the instance using the block given.
119 120 121 122 |
# File 'lib/twitter-search-watcher.rb', line 119 def self.watch! search_string, = nil, &block watcher = TwitterSearchWatcher.new search_string, watcher.watch! &block end |
Instance Method Details
#search!(additional_parameters = nil) ⇒ Object
Performs a search. Accepts the same parameters as #search_url
89 90 91 |
# File 'lib/twitter-search-watcher.rb', line 89 def search! additional_parameters = nil JSON.parse open( search_url(additional_parameters), 'User-Agent' => user_agent ).read end |
#search_more!(response, additional_parameters = nil) ⇒ Object
Performs a search, given the response from another search.
If the response given is paginated (ie. there are additional tweets available on additional pages), this will return the next page. Else, this will return nil.
Accepts additional parameters (same as #search_url)
113 114 115 |
# File 'lib/twitter-search-watcher.rb', line 113 def search_more! response, additional_parameters = nil search!( (additional_parameters || {}).merge( :page => (response['page'] + 1), :max_id => response['max_id'] ) ) if response['next_page'] end |
#search_newer!(response = nil, additional_parameters = nil) ⇒ Object
Performs a search, given the response from another search.
If a response if given, the search will only return tweets newer than the given response’s tweets. If a response is not given, this performs a normal search.
Accepts additional parameters (same as #search_url)
99 100 101 102 103 104 105 |
# File 'lib/twitter-search-watcher.rb', line 99 def search_newer! response = nil, additional_parameters = nil if response search!( (additional_parameters || {}).merge( :since_id => response['max_id'] ) ) else search! additional_parameters end end |
#search_url(additional_parameters = nil) ⇒ Object
Returns the URL we’ll use to call the Twitter Search API.
Without parameters, it’ll generate a URL just from this TwitterSearchWatcher instance.
With parameters, it’ll override the TwitterSearchWatcher instance’s options with whatever you pass, eg.
>> TwitterSearchWatcher.new( 'foo', :rpp => 15 ).search_url
=> "http://search.twitter.com/search.json?q=foo&rpp=15"
>> TwitterSearchWatcher.new( 'foo', :rpp => 15 ).search_url( :rpp => 99 )
=> "http://search.twitter.com/search.json?q=foo&rpp=99"
80 81 82 |
# File 'lib/twitter-search-watcher.rb', line 80 def search_url additional_parameters = nil TWITTER_SEARCH_URL + build_query_string(additional_parameters) end |
#watch!(additional_parameters = nil, &block) ⇒ Object
Starts watching this search in a loop. It will wait #check_every seconds between new requests (except requests to get additional pages). Every time a new tweet is found, that tweet is passed to the block given.
TwitterSearchWatcher.new('foo').watch! {|tweet| puts "got tweet: #{ tweet.text }" }
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/twitter-search-watcher.rb', line 130 def watch! additional_parameters = nil, &block @max_id_found_so_far = 0 trap('INT'){ puts "\nexiting ..."; exit } puts "Watching for tweets: #{ search_url(additional_parameters) }" loop do @last_response = search_newer!(@last_response, additional_parameters) call_tweet_callbacks(@last_response, block) update_max_id @last_response # this is kindof icky ... but it works if @last_response['next_page'] response = @last_response num_pages_searched = 0 while (response = search_more!(response, additional_parameters)) && (num_pages_searched <= max_pages if max_pages) num_pages_searched += 1 call_tweet_callbacks(response, block) update_max_id response end end sleep check_every end end |