-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
stream.rb
162 lines (150 loc) · 5.45 KB
/
stream.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 't/cli'
require 't/printable'
require 't/rcfile'
require 't/search'
require 'thor'
require 'tweetstream'
module T
class Stream < Thor
include T::Printable
STATUS_HEADINGS_FORMATTING = [
"%-18s", # Add padding to maximum length of a Tweet ID
"%-12s", # Add padding to length of a timestamp formatted with ls_formatted_time
"%-20s", # Add padding to maximum length of a Twitter screen name
"%s", # Last element does not need special formatting
]
def initialize(*)
super
@rcfile = RCFile.instance
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."
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def all
client.on_inited do
if options['csv']
say STATUS_HEADINGS.to_csv
elsif options['long'] && STDOUT.tty?
say STATUS_HEADINGS.size.times.map do |index|
STATUS_HEADINGS_FORMATTING[index] % STATUS_HEADINGS[index]
end
end
end
client.on_timeline_status do |status|
if options['csv']
print_csv_status(status)
elsif options['long']
print_table build_long_status(status).each_with_index.map do |element, index|
STATUS_HEADINGS_FORMATTING[index] % element
end
else
print_status(status)
end
end
until_term
client.sample
end
desc "matrix", "Unfortunately, no one can be told what the Matrix is. You have to see it for yourself."
def matrix
client.on_timeline_status do |status|
say(status.text.gsub("\n", ''), [:bold, :green, :on_black])
end
until_term
client.sample
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."
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def search(keyword, *keywords)
keywords.unshift(keyword)
client.on_inited do
search = T::Search.new
search.options = search.options.merge(options)
search.options = search.options.merge(:reverse => true)
search.options = search.options.merge(:format => STATUS_HEADINGS_FORMATTING)
search.all(keywords.join(' OR '))
end
client.on_timeline_status do |status|
if options['csv']
print_csv_status(status)
elsif options['long']
print_table build_long_status(status).each_with_index.map do |element, index|
STATUS_HEADINGS_FORMATTING[index] % element
end
else
print_status(status)
end
end
until_term
client.track(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."
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long 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.options = cli.options.merge(:format => STATUS_HEADINGS_FORMATTING)
cli.timeline
end
client.on_timeline_status do |status|
if options['csv']
print_csv_status(status)
elsif options['long']
print_table build_long_status(status).each_with_index.map do |element, index|
STATUS_HEADINGS_FORMATTING[index] % element
end
else
print_status(status)
end
end
until_term
client.userstream
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."
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def users(screen_name, *screen_names)
screen_names.unshift(screen_name)
client.on_inited do
if options['csv']
say STATUS_HEADINGS.to_csv
elsif options['long'] && STDOUT.tty?
say STATUS_HEADINGS.size.times.map do |index|
STATUS_HEADINGS_FORMATTING[index] % STATUS_HEADINGS[index]
end
end
end
client.on_timeline_status do |status|
if options['csv']
print_csv_status(status)
elsif options['long']
print_table(build_long_status(status))
else
print_status(status)
end
end
until_term
client.follow(screen_names)
end
private
def client
return @client if @client
@rcfile.path = options['profile'] if options['profile']
@client = TweetStream::Client.new(
:consumer_key => @rcfile.active_consumer_key,
:consumer_secret => @rcfile.active_consumer_secret,
:oauth_token => @rcfile.active_token,
:oauth_token_secret => @rcfile.active_secret
)
end
def until_term
Signal.trap("TERM") do
client.stop
shutdown
end
end
end
end