Skip to content
Browse files

Add --csv option to stream commands

  • Loading branch information...
1 parent a384c44 commit dfa6f87697e4f365a0fa80aaf712fa7d0e94ac4c @sferik sferik committed May 1, 2012
Showing with 55 additions and 18 deletions.
  1. +19 −10 lib/t/printable.rb
  2. +33 −5 lib/t/stream.rb
  3. +3 −3 spec/cli_spec.rb
View
29 lib/t/printable.rb
@@ -17,7 +17,7 @@ def self.included(base)
def build_long_list(list)
created_at = list.created_at > 6.months.ago ? list.created_at.strftime("%b %e %H:%M") : list.created_at.strftime("%b %e %Y")
- [list.id, created_at, list.full_name, number_with_delimiter(list.member_count), number_with_delimiter(list.subscriber_count), list.mode, list.description]
+ [list.id, created_at, "@#{list.user.screen_name}", list.slug, number_with_delimiter(list.member_count), number_with_delimiter(list.subscriber_count), list.mode, list.description]
end
def build_long_status(status)
@@ -55,6 +55,14 @@ def print_in_columns(array)
puts
end
+ def list_headings
+ ["ID", "Created at", "Screen name", "Slug", "Members", "Subscribers", "Mode", "Description"]
+ end
+
+ def status_headings
+ ["ID", "Posted at", "Screen name", "Text"]
+ end
+
def print_lists(lists)
lists = lists.sort_by{|list| list.slug.downcase} unless options['unsorted']
if options['posted']
@@ -68,7 +76,7 @@ def print_lists(lists)
end
lists.reverse! if options['reverse']
if options['csv']
- say ["ID", "Created at", "Screen name", "Slug", "Members", "Subscribers", "Mode", "Description"].to_csv unless lists.empty?
+ say list_headings.to_csv unless lists.empty?
lists.each do |list|
print_csv_list(list)
end
@@ -77,8 +85,7 @@ def print_lists(lists)
build_long_list(list)
end
if STDOUT.tty?
- headings = ["ID", "Created at", "Slug", "Members", "Subscribers", "Mode", "Description"]
- array.unshift(headings) unless lists.empty?
+ array.unshift(list_headings) unless lists.empty?
print_table(array, :truncate => true)
else
print_table(array)
@@ -108,7 +115,7 @@ def print_status(status)
def print_statuses(statuses)
statuses.reverse! if options['reverse'] || options['stream']
if options['csv']
- say ["ID", "Posted at", "Screen name", "Text"].to_csv unless statuses.empty?
+ say status_headings.to_csv unless statuses.empty?
statuses.each do |status|
print_csv_status(status)
end
@@ -117,8 +124,7 @@ def print_statuses(statuses)
build_long_status(status)
end
if STDOUT.tty?
- headings = ["ID", "Posted at", "Screen name", "Text"]
- array.unshift(headings) unless statuses.empty?
+ array.unshift(status_headings) unless statuses.empty?
print_table(array, :truncate => true)
else
print_table(array)
@@ -130,6 +136,10 @@ def print_statuses(statuses)
end
end
+ def user_headings
+ ["ID", "Since", "Tweets", "Favorites", "Listed", "Following", "Followers", "Screen name", "Name"]
+ end
+
def print_users(users)
users = users.sort_by{|user| user.screen_name.downcase} unless options['unsorted']
if options['posted']
@@ -147,7 +157,7 @@ def print_users(users)
end
users.reverse! if options['reverse']
if options['csv']
- say ["ID", "Since", "Tweets", "Favorites", "Listed", "Following", "Followers", "Screen name", "Name"].to_csv unless users.empty?
+ say user_headings.to_csv unless users.empty?
users.each do |user|
print_csv_user(user)
end
@@ -156,8 +166,7 @@ def print_users(users)
build_long_user(user)
end
if STDOUT.tty?
- headings = ["ID", "Since", "Tweets", "Favorites", "Listed", "Following", "Followers", "Screen name", "Name"]
- array.unshift(headings) unless users.empty?
+ array.unshift(user_headings) unless users.empty?
print_table(array, :truncate => true)
else
print_table(array)
View
38 lib/t/stream.rb
@@ -15,9 +15,17 @@ def initialize(*)
end
desc "all", "Stream a random sample of all Tweets (Control-C to stop)"
+ method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
def all
+ if options['csv']
+ say status_headings.to_csv
+ end
client.on_timeline_status do |status|
- print_status(status)
+ if options['csv']
+ print_csv_status(status)
+ else
+ print_status(status)
+ end
end
Signal.trap("TERM") do
client.stop
@@ -39,15 +47,21 @@ def matrix
end
desc "search KEYWORD [KEYWORD...]", "Stream Tweets that contain specified keywords, joined with logical ORs (Control-C to stop)"
+ method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
def search(keyword, *keywords)
keywords.unshift(keyword)
client.on_inited do
- search= T::Search.new
+ search = T::Search.new
+ search.options = search.options.merge(options)
search.options = search.options.merge(:reverse => true)
search.all(keywords.join(' OR '))
end
client.on_timeline_status do |status|
- print_status(status)
+ if options['csv']
+ print_csv_status(status)
+ else
+ print_status(status)
+ end
end
Signal.trap("TERM") do
client.stop
@@ -57,14 +71,20 @@ def search(keyword, *keywords)
end
desc "timeline", "Stream your timeline (Control-C to stop)"
+ method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
def timeline
client.on_inited do
cli = T::CLI.new
+ cli.options = cli.options.merge(options)
cli.options = cli.options.merge(:reverse => true)
cli.timeline
end
client.on_timeline_status do |status|
- print_status(status)
+ if options['csv']
+ print_csv_status(status)
+ else
+ print_status(status)
+ end
end
Signal.trap("TERM") do
client.stop
@@ -74,10 +94,18 @@ def timeline
end
desc "users SCREEN_NAME [SCREEN_NAME...]", "Stream Tweets either from or in reply to specified users (Control-C to stop)"
+ method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
def users(screen_name, *screen_names)
+ if options['csv']
+ say status_headings.to_csv
+ end
screen_names.unshift(screen_name)
client.on_timeline_status do |status|
- print_status(status)
+ if options['csv']
+ print_csv_status(status)
+ else
+ print_status(status)
+ end
end
Signal.trap("TERM") do
client.stop
View
6 spec/cli_spec.rb
@@ -1596,9 +1596,9 @@
it "should output in long format" do
@cli.lists
$stdout.string.should == <<-eos
-ID Created at Slug Members Subscribers Mode ...
-21718825 Sep 14 2010 @sferik/code-for-america 26 5 publi...
- 8863586 Mar 15 2010 @sferik/presidents 2 1 publi...
+ID Created at Screen name Slug Members Subscribers ...
+21718825 Sep 14 2010 @sferik code-for-america 26 5 ...
+ 8863586 Mar 15 2010 @sferik presidents 2 1 ...
eos
end
end

0 comments on commit dfa6f87

Please sign in to comment.
Something went wrong with that request. Please try again.