Class: T::Search

Inherits:
Thor
  • Object
show all
Includes:
Collectable, Printable, Requestable, Utils
Defined in:
lib/t/search.rb

Constant Summary collapse

DEFAULT_NUM_RESULTS =
20
MAX_NUM_RESULTS =
200
MAX_SEARCH_RESULTS =
100

Constants included from Printable

Printable::LIST_HEADINGS, Printable::MONTH_IN_SECONDS, Printable::TWEET_HEADINGS, Printable::USER_HEADINGS

Constants included from Collectable

Collectable::MAX_PAGE

Instance Method Summary collapse

Methods included from Collectable

#collect_with_count, #collect_with_max_id, #collect_with_page

Constructor Details

#initializeSearch

Returns a new instance of Search.



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

def initialize(*)
  @rcfile = T::RCFile.instance
  super
end

Instance Method Details

#all(query) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/t/search.rb', line 33

def all(query)
  count = options['number'] || DEFAULT_NUM_RESULTS
  opts = {count: MAX_SEARCH_RESULTS}
  opts[:include_entities] = !!options['decode_uris']
  tweets = client.search(query, opts).take(count)
  tweets.reverse! if options['reverse']
  if options['csv']
    require 'csv'
    say TWEET_HEADINGS.to_csv unless tweets.empty?
    tweets.each do |tweet|
      say [tweet.id, csv_formatted_time(tweet), tweet.user.screen_name, decode_full_text(tweet, options['decode_uris'])].to_csv
    end
  elsif options['long']
    array = tweets.collect do |tweet|
      [tweet.id, ls_formatted_time(tweet), "@#{tweet.user.screen_name}", decode_full_text(tweet, options['decode_uris']).gsub(/\n+/, ' ')]
    end
    format = options['format'] || Array.new(TWEET_HEADINGS.size) { '%s' }
    print_table_with_headings(array, TWEET_HEADINGS, format)
  else
    say unless tweets.empty?
    tweets.each do |tweet|
      print_message(tweet.user.screen_name, decode_full_text(tweet, options['decode_uris']))
    end
  end
end

#favorites(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/t/search.rb', line 65

def favorites(*args)
  query = args.pop
  user = args.pop
  opts = {count: MAX_NUM_RESULTS}
  opts[:include_entities] = !!options['decode_uris']
  if user
    require 't/core_ext/string'
    user = options['id'] ? user.to_i : user.strip_ats
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      client.favorites(user, opts)
    end
  else
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      client.favorites(opts)
    end
  end
  tweets = tweets.select do |tweet|
    /#{query}/i.match(tweet.full_text)
  end
  print_tweets(tweets)
end

#list(user_list, query) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/t/search.rb', line 96

def list(user_list, query)
  owner, list_name = extract_owner(user_list, options)
  opts = {count: MAX_NUM_RESULTS}
  opts[:include_entities] = !!options['decode_uris']
  tweets = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    client.list_timeline(owner, list_name, opts)
  end
  tweets = tweets.select do |tweet|
    /#{query}/i.match(tweet.full_text)
  end
  print_tweets(tweets)
end

#mentions(query) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/t/search.rb', line 115

def mentions(query)
  opts = {count: MAX_NUM_RESULTS}
  opts[:include_entities] = !!options['decode_uris']
  tweets = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    client.mentions(opts)
  end
  tweets = tweets.select do |tweet|
    /#{query}/i.match(tweet.full_text)
  end
  print_tweets(tweets)
end

#retweets(*args) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/t/search.rb', line 135

def retweets(*args)
  query = args.pop
  user = args.pop
  opts = {count: MAX_NUM_RESULTS}
  opts[:include_entities] = !!options['decode_uris']
  if user
    require 't/core_ext/string'
    user = options['id'] ? user.to_i : user.strip_ats
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      client.retweeted_by_user(user, opts)
    end
  else
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      client.retweeted_by_me(opts)
    end
  end
  tweets = tweets.select do |tweet|
    /#{query}/i.match(tweet.full_text)
  end
  print_tweets(tweets)
end

#timeline(*args) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/t/search.rb', line 169

def timeline(*args)
  query = args.pop
  user = args.pop
  opts = {count: MAX_NUM_RESULTS}
  opts[:exclude_replies] = true if options['exclude'] == 'replies'
  opts[:include_entities] = !!options['decode_uris']
  opts[:include_rts] = false if options['exclude'] == 'retweets'
  opts[:max_id] = options['max_id'] if options['max_id']
  opts[:since_id] = options['since_id'] if options['since_id']
  if user
    require 't/core_ext/string'
    user = options['id'] ? user.to_i : user.strip_ats
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      client.user_timeline(user, opts)
    end
  else
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      client.home_timeline(opts)
    end
  end
  tweets = tweets.select do |tweet|
    /#{query}/i.match(tweet.full_text)
  end
  print_tweets(tweets)
end

#users(query) ⇒ Object



205
206
207
208
209
210
# File 'lib/t/search.rb', line 205

def users(query)
  users = collect_with_page do |page|
    client.user_search(query, page: page)
  end
  print_users(users)
end