Module: Chatterbot::Search

Included in:
Bot
Defined in:
lib/chatterbot/search.rb

Overview

handle Twitter searches

Constant Summary collapse

MAX_SEARCH_TWEETS =
1000

Instance Method Summary collapse

Instance Method Details

#exclude_retweetsObject

modify a query string to exclude retweets from searches



14
15
16
# File 'lib/chatterbot/search.rb', line 14

def exclude_retweets
  @skip_retweets = true
end

#include_retweetsObject



18
19
20
# File 'lib/chatterbot/search.rb', line 18

def include_retweets
  @skip_retweets = false
end

#search(queries, opts = {}, &block) ⇒ Object

internal search code



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chatterbot/search.rb', line 27

def search(queries, opts = {}, &block)
  debug "check for tweets since #{since_id}"

  max_tweets = opts.delete(:limit) || MAX_SEARCH_TWEETS
  
  if queries.is_a?(String)
    queries = [queries]
  end

  #
  # search twitter
  #
  queries.each { |query|
    debug "search: #{query} #{default_opts.merge(opts)}"
    @current_tweet = nil
    client.search( query, default_opts.merge(opts) ).take(max_tweets).each { |s|
      update_since_id(s)
      debug s.text
      if has_whitelist? && !on_whitelist?(s)
        debug "skipping because user not on whitelist"
      elsif block_given? && !on_blacklist?(s) && !skip_me?(s) && !skippable_retweet?(s)
        @current_tweet = s
        yield s
      end
    }
    @current_tweet = nil
  }
end

#skippable_retweet?(t) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/chatterbot/search.rb', line 22

def skippable_retweet?(t)
  @skip_retweets && t.retweeted_status?
end