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 Utils

Utils::DISTANCE_THRESHOLDS

Constants included from RequestableAPI

RequestableAPI::BASE_URL, RequestableAPI::BASE_URL_UPLOAD, RequestableAPI::BASE_URL_V1, RequestableAPI::FORM_HEADERS, RequestableAPI::JSON_HEADERS, RequestableAPI::MAX_PAGE, RequestableAPI::V2_LIST_FIELDS, RequestableAPI::V2_PLACE_FIELDS, RequestableAPI::V2_TWEET_EXPANSIONS, RequestableAPI::V2_TWEET_FIELDS, RequestableAPI::V2_USER_EXPANSIONS, RequestableAPI::V2_USER_FIELDS

Constants included from Printable

Printable::LISTS_SORT_MAP, Printable::LIST_HEADINGS, Printable::MONTH_IN_SECONDS, Printable::TWEET_HEADINGS, Printable::USERS_SORT_MAP, Printable::USER_HEADINGS

Constants included from Collectable

Collectable::MAX_PAGE

Instance Method Summary collapse

Methods included from RequestableAPI

#setup_requestable_api!

Methods included from RequestableAPI::AccountEndpoints

#x_before_request, #x_filter, #x_sample, #x_settings, #x_trend_locations, #x_trends, #x_update_profile, #x_update_profile_background_image, #x_update_profile_image

Methods included from RequestableAPI::ListEndpoints

#x_add_list_members, #x_create_list, #x_destroy_list, #x_list, #x_list_member?, #x_list_members, #x_list_timeline, #x_lists, #x_remove_list_members

Methods included from RequestableAPI::DMEndpoints

#x_create_direct_message_event, #x_destroy_direct_message, #x_direct_message, #x_direct_messages_received, #x_direct_messages_sent

Methods included from RequestableAPI::Mutations

#x_block, #x_destroy_status, #x_favorite, #x_follow, #x_mute, #x_muted_ids, #x_report_spam, #x_retweet, #x_unblock, #x_unfavorite, #x_unfollow, #x_unmute

Methods included from RequestableAPI::TweetEndpoints

#x_favorites, #x_home_timeline, #x_mentions, #x_retweeted_by_me, #x_retweeted_by_user, #x_retweeters_ids, #x_retweets_of_me, #x_search, #x_status, #x_update, #x_update_with_media, #x_user_timeline

Methods included from RequestableAPI::UserEndpoints

#x_follower_ids, #x_friend_ids, #x_friendship?, #x_user, #x_user_search, #x_users, #x_verify_credentials

Methods included from Collectable

#collect_with_count, #collect_with_max_id, #collect_with_page

Constructor Details

#initializeSearch



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

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

Instance Method Details

#all(query) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/t/search.rb', line 32

def all(query)
  count = options["number"] || DEFAULT_NUM_RESULTS
  opts = {count: MAX_SEARCH_RESULTS, include_entities: !!options["decode_uris"]}
  tweets = x_search(query, opts).take(count)
  tweets.reverse! if options["reverse"]
  print_search_results(tweets)
end

#favorites(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/t/search.rb', line 46

def favorites(*args)
  query = args.pop
  user = args.pop
  opts = {count: MAX_NUM_RESULTS}
  opts[:include_entities] = !!options["decode_uris"]
  if user
    user = options["id"] ? user.to_i : user.tr("@", "")
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      x_favorites(user, opts)
    end
  else
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      x_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



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/t/search.rb', line 76

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?
    x_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



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

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?
    x_mentions(opts)
  end
  tweets = tweets.select do |tweet|
    /#{query}/i.match(tweet["full_text"])
  end
  print_tweets(tweets)
end

#retweets(*args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/t/search.rb', line 115

def retweets(*args)
  query = args.pop
  user = args.pop
  opts = {count: MAX_NUM_RESULTS}
  opts[:include_entities] = !!options["decode_uris"]
  if user
    user = options["id"] ? user.to_i : user.tr("@", "")
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      x_retweeted_by_user(user, opts)
    end
  else
    tweets = collect_with_max_id do |max_id|
      opts[:max_id] = max_id unless max_id.nil?
      x_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



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/t/search.rb', line 148

def timeline(*args)
  query = args.pop
  user = args.pop
  opts = build_timeline_opts.merge(count: MAX_NUM_RESULTS)
  user = resolve_user_input(user) if user
  tweets = collect_with_max_id do |max_id|
    opts[:max_id] = max_id unless max_id.nil?
    user ? x_user_timeline(user, opts) : x_home_timeline(opts)
  end
  tweets = tweets.select { |tweet| /#{query}/i.match(tweet["full_text"]) }
  print_tweets(tweets)
end

#users(query) ⇒ Object



169
170
171
172
173
174
# File 'lib/t/search.rb', line 169

def users(query)
  users = collect_with_page do |page|
    x_user_search(query, page:)
  end
  print_users(users)
end