stream.rb
3.97 KB
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
module Twurl
class Stream
SUPPORTED_COMMANDS = %w(authorize accounts alias set)
DEFAULT_COMMAND = 'request'
PATH_PATTERN = /^\/\w+/
PROTOCOL_PATTERN = /^\w+:\/\//
README = File.dirname(__FILE__) + '/../../README'
@output ||= STDOUT
class NoPathFound < Exception
end
class << self
attr_accessor :output
def run(page, author_id, tags, config_file_path, proxy=nil)
begin
@@file_path = config_file_path
Twurl.options = Options.new
Twurl.options.command = 'request' # Not necessary anymore
Twurl.options.data = {"track"=>tags}
# Twurl.options.proxy = 'http://161.148.1.167:3128' # Use for production mode at SERPRO
Twurl.options.proxy = proxy unless proxy.nil? or proxy == ''
Twurl.options.trace = false
Twurl.options.headers = {}
Twurl.options.subcommands=[]
Twurl.options.upload = {}
Twurl.options.upload['file'] = []
Twurl.options.path="/1.1/statuses/filter.json"
Twurl.options.host="stream.twitter.com"
Twurl.options.read_timeout= 0
Twurl.options.page = page
Twurl.options.author_id = author_id
rescue NoPathFound => e
exit
end
dispatch(Twurl.options)
end
def file_path
@@file_path
end
def dispatch(options)
client = OAuthClient.load_from_options(options)
controller = RequestController
controller.dispatch(client, options)
rescue Twurl::Exception => exception
abort(exception.message)
end
def output
if Twurl.options && Twurl.options.output
Twurl.options.output
else
@output
end
end
def print(*args, &block)
output.print(*args, &block)
output.flush if output.respond_to?(:flush)
end
def puts(*args, &block)
output.puts(*args, &block)
output.flush if output.respond_to?(:flush)
end
def prompt_for(label)
system "stty -echo"
CLI.print "#{label}: "
result = STDIN.gets.chomp
CLI.puts
result
rescue Interrupt
exit
ensure
system "stty echo"
end
private
def extract_command!(arguments)
if SUPPORTED_COMMANDS.include?(arguments.first)
arguments.shift
else
DEFAULT_COMMAND
end
end
def extract_path!(arguments)
path = nil
arguments.each_with_index do |argument, index|
if argument[PATH_PATTERN]
path_with_params = arguments.slice!(index)
path, params = path_with_params.split("?", 2)
if params
path += "?" + escape_params(params)
end
break
end
end
path
end
def escape_params(params)
split_params = params.split("&").map do |param|
key, value = param.split('=', 2)
CGI::escape(key) + '=' + CGI::escape(value)
end
split_params.join("&")
end
end
end
class Options < OpenStruct
DEFAULT_REQUEST_METHOD = 'get'
DEFAULT_HOST = 'api.twitter.com'
DEFAULT_PROTOCOL = 'https'
def oauth_client_options
OAuthClient::OAUTH_CLIENT_OPTIONS.inject({}) do |options, option|
options[option] = send(option)
options
end
end
def base_url
"#{protocol}://#{host}"
end
def ssl?
protocol == 'https'
end
def debug_output_io
super || STDERR
end
def request_method
super || (data.empty? ? DEFAULT_REQUEST_METHOD : 'post')
end
def protocol
super || DEFAULT_PROTOCOL
end
def host
super || DEFAULT_HOST
end
def proxy
super || nil
end
end
end