lib/t/search.rb in t-0.6.4 vs lib/t/search.rb in t-0.7.0
- old
+ new
@@ -1,6 +1,9 @@
require 'action_view'
+require 'csv'
+# 'fastercsv' required on Ruby versions < 1.9
+require 'fastercsv' unless Array.new.respond_to?(:to_csv)
require 'retryable'
require 't/core_ext/enumerable'
require 't/printable'
require 't/rcfile'
require 't/requestable'
@@ -23,15 +26,22 @@
super
@rcfile = RCFile.instance
end
desc "all QUERY", "Returns the #{DEFAULT_NUM_RESULTS} most recent Tweets that match a specified query."
+ method_option :csv, :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
+ method_option :long, :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
method_option :number, :aliases => "-n", :type => :numeric, :default => DEFAULT_NUM_RESULTS
def all(query)
rpp = options['number'] || DEFAULT_NUM_RESULTS
statuses = client.search(query, :include_entities => false, :rpp => rpp)
- if options['long']
+ if options['csv']
+ say ["ID", "Posted at", "Screen name", "Text"].to_csv unless statuses.empty?
+ statuses.each do |status|
+ say [status.id, status.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), status.from_user, status.text].to_csv
+ end
+ elsif options['long']
array = statuses.map do |status|
created_at = status.created_at > 6.months.ago ? status.created_at.strftime("%b %e %H:%M") : status.created_at.strftime("%b %e %Y")
[status.id.to_s, created_at, "@#{status.from_user}", status.text.gsub(/\n+/, ' ')]
end
if STDOUT.tty?
@@ -45,71 +55,86 @@
end
end
end
desc "favorites QUERY", "Returns Tweets you've favorited that mach a specified query."
+ method_option :csv, :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
+ method_option :long, :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def favorites(query)
statuses = 1.upto(MAX_PAGES).threaded_map do |page|
retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
client.favorites(:page => page, :count => MAX_NUM_RESULTS).select do |status|
/#{query}/i.match(status.text)
end
end
end.flatten.compact
- print_status_list(statuses)
+ print_statuses(statuses)
end
map %w(faves) => :favorites
desc "mentions QUERY", "Returns Tweets mentioning you that mach a specified query."
+ method_option :csv, :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
+ method_option :long, :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def mentions(query)
statuses = 1.upto(MAX_PAGES).threaded_map do |page|
retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
client.mentions(:page => page, :count => MAX_NUM_RESULTS).select do |status|
/#{query}/i.match(status.text)
end
end
end.flatten.compact
- print_status_list(statuses)
+ print_statuses(statuses)
end
map %w(replies) => :mentions
desc "retweets QUERY", "Returns Tweets you've retweeted that mach a specified query."
+ method_option :csv, :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
+ method_option :long, :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def retweets(query)
statuses = 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.flatten.compact
- print_status_list(statuses)
+ print_statuses(statuses)
end
map %w(rts) => :retweets
desc "timeline QUERY", "Returns Tweets in your timeline that match a specified query."
+ method_option :csv, :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
+ method_option :long, :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def timeline(query)
statuses = 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.flatten.compact
- print_status_list(statuses)
+ print_statuses(statuses)
end
map %w(tl) => :timeline
- 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_ats
+ desc "user USER QUERY", "Returns Tweets in a user's timeline that match a specified query."
+ method_option :csv, :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
+ method_option :id, :aliases => "-i", :type => "boolean", :default => false, :desc => "Specify user via ID instead of screen name."
+ method_option :long, :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
+ def user(user, query)
+ user = if options['id']
+ user.to_i
+ else
+ user.strip_ats
+ end
statuses = 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|
+ client.user_timeline(user, :page => page, :count => MAX_NUM_RESULTS).select do |status|
/#{query}/i.match(status.text)
end
end
end.flatten.compact
- print_status_list(statuses)
+ print_statuses(statuses)
end
end
end