lib/t/cli/search.rb in t-0.3.1 vs lib/t/cli/search.rb in t-0.4.0
- old
+ new
@@ -1,21 +1,20 @@
require 'action_view'
require 'pager'
require 'retryable'
require 't/core_ext/enumerable'
require 't/rcfile'
+require 't/requestable'
require 'thor'
-require 'twitter'
module T
class CLI
class Search < Thor
include ActionView::Helpers::DateHelper
include Pager
+ include T::Requestable
- DEFAULT_HOST = 'api.twitter.com'
- DEFAULT_PROTOCOL = 'https'
DEFAULT_NUM_RESULTS = 20
MAX_PAGES = 16
MAX_NUM_RESULTS = 200
MAX_SCREEN_NAME_SIZE = 20
@@ -38,65 +37,87 @@
timeline.each do |status|
say "#{status.from_user.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
end
end
- desc "timeline QUERY", "Returns Tweets in your timeline that match a specified query."
- def timeline(query)
+ desc "favorites QUERY", "Returns Tweets you've favorited that mach a specified query."
+ def favorites(query)
timeline = 1.upto(MAX_PAGES).threaded_map do |page|
retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
- client.home_timeline(:page => page, :count => MAX_NUM_RESULTS).map do |status|
- status if /#{query}/i.match(status.text)
+ client.favorites(:page => page, :count => MAX_NUM_RESULTS).select do |status|
+ /#{query}/i.match(status.text)
end
end
end
page unless T.env.test?
timeline.flatten.compact.each do |status|
say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
end
end
- map %w(tl) => :timeline
+ map %w(faves) => :favorites
- desc "user SCREEN_NAME QUERY", "Returns Tweets in a user's timeline that match a specified query."
- def user(screen_name, query)
- screen_name = screen_name.strip_at
+ desc "mentions QUERY", "Returns Tweets mentioning you that mach a specified query."
+ def mentions(query)
timeline = 1.upto(MAX_PAGES).threaded_map do |page|
retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
- client.user_timeline(screen_name, :page => page, :count => MAX_NUM_RESULTS).map do |status|
- status if /#{query}/i.match(status.text)
+ client.mentions(:page => page, :count => MAX_NUM_RESULTS).select do |status|
+ /#{query}/i.match(status.text)
end
end
end
page unless T.env.test?
timeline.flatten.compact.each do |status|
say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
end
end
+ map %w(replies) => :mentions
- private
-
- def base_url
- "#{protocol}://#{host}"
+ desc "retweets QUERY", "Returns Tweets you've retweeted that mach a specified query."
+ def retweets(query)
+ timeline = 1.upto(MAX_PAGES).threaded_map do |page|
+ retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
+ client.retweeted_by(:page => page, :count => MAX_NUM_RESULTS).select do |status|
+ /#{query}/i.match(status.text)
+ end
+ end
+ end
+ page unless T.env.test?
+ timeline.flatten.compact.each do |status|
+ say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
+ end
end
+ map %w(rts) => :retweets
- def client
- return @client if @client
- @rcfile.path = parent_options['profile'] if parent_options['profile']
- @client = Twitter::Client.new(
- :endpoint => base_url,
- :consumer_key => @rcfile.default_consumer_key,
- :consumer_secret => @rcfile.default_consumer_secret,
- :oauth_token => @rcfile.default_token,
- :oauth_token_secret => @rcfile.default_secret
- )
+ desc "timeline QUERY", "Returns Tweets in your timeline that match a specified query."
+ def timeline(query)
+ timeline = 1.upto(MAX_PAGES).threaded_map do |page|
+ retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
+ client.home_timeline(:page => page, :count => MAX_NUM_RESULTS).select do |status|
+ /#{query}/i.match(status.text)
+ end
+ end
+ end
+ page unless T.env.test?
+ timeline.flatten.compact.each do |status|
+ say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
+ end
end
+ map %w(tl) => :timeline
- def host
- parent_options['host'] || DEFAULT_HOST
- end
-
- def protocol
- parent_options['no_ssl'] ? 'http' : DEFAULT_PROTOCOL
+ desc "user SCREEN_NAME QUERY", "Returns Tweets in a user's timeline that match a specified query."
+ def user(screen_name, query)
+ screen_name = screen_name.strip_at
+ timeline = 1.upto(MAX_PAGES).threaded_map do |page|
+ retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
+ client.user_timeline(screen_name, :page => page, :count => MAX_NUM_RESULTS).select do |status|
+ /#{query}/i.match(status.text)
+ end
+ end
+ end
+ page unless T.env.test?
+ timeline.flatten.compact.each do |status|
+ say "#{status.user.screen_name.rjust(MAX_SCREEN_NAME_SIZE)}: #{status.text} (#{time_ago_in_words(status.created_at)} ago)"
+ end
end
end
end
end