-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_by_ticker.rb
executable file
·327 lines (274 loc) · 7.5 KB
/
fetch_by_ticker.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env ruby
# encoding: UTF-8
require 'rubygems'
require 'net/http'
require 'net/https'
require 'uri'
require 'pp'
require 'erb'
require 'optparse'
require 'ostruct'
require 'logger'
require 'nokogiri'
require 'fileutils'
require 'date'
require 'yaml'
require 'csv'
require 'cgi'
### constants
CONFIG = 'config.yaml'
USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'
STREETEVENTS = 'http://www.streetevents.com'
STREETEVENTS_HTTPS = 'https://www.streetevents.com'
### functions
# Load in the YAML configuration file, check for errors, and return as hash 'cfg'
#
# Ex config.yaml:
#
# ---
# login:
# username: xxx
# password: xxx
#
def load_config
cfg = File.open(CONFIG) { |yf| YAML::load( yf ) } if File.exists?(CONFIG)
# => Ensure loaded data is a hash. ie: YAML load was OK
if cfg.class != Hash
raise "ERROR: Configuration - invalid format or parsing error."
else
if cfg['login'].nil?
raise "ERROR: Configuration: login not defined."
end
end
return cfg
end
def view_response(resp)
if @options[:debug]
puts '------------------'
puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
puts '------------------'
puts "\n"
else
pp resp
end
end
def show_body(resp)
resp.body.each_line { |line| puts line }
end
def get_cookie(resp)
cookie = ''
resp.response['set-cookie'].split(' ').each do |c|
cookie += c if c =~ /^SE%/
end
return cookie.chomp(';')
end
def get_response(path)
uri = URI.parse(STREETEVENTS)
http = Net::HTTP.new uri.host, uri.port
resp = http.request_get(path, {'User-Agent' => USERAGENT, 'Cookie' => @cookie})
show_body(resp) if @debug
return http, resp
end
def logged_in?(resp)
resp.response['set-cookie'].split(' ').each_with_object([]){|c, ary| ary << c if c =~ /^SE%/}.length > 1
end
def login
uri = URI.parse(STREETEVENTS_HTTPS)
http = Net::HTTP.new uri.host, uri.port
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
path = '/cookieTest.aspx'
resp = http.request_get(path, {'User-Agent' => USERAGENT})
cookie = get_cookie(resp)
pp cookie if @debug
headers = {
'User-Agent' => USERAGENT,
'Cookie' => cookie,
'Referer' => STREETEVENTS_HTTPS + '/cookieTest.aspx',
'Content-Type' => 'application/x-www-form-urlencoded'
}
data = "Destinations=&JavascriptURL=&CookieTest=OK"
resp = http.post('/Login.aspx', data, headers)
cookie = get_cookie(resp)
pp cookie if @debug
viewstate = ''
resp.body.each_line do |line|
if line =~ /__VIEWSTATE/
viewstate = /value=\"(.*)\"/.match(line)[1]
break
end
end
#puts viewstate
data = "__VIEWSTATE=#{ERB::Util.url_encode(viewstate)}&" +
"Destinations=&" +
"uname=#{@username}&" +
"pwd=#{@password}&" +
"post=true"
headers = {
'User-Agent' => USERAGENT,
'Cookie' => cookie,
'Referer' => STREETEVENTS_HTTPS + '/Login.aspx',
'Content-Type' => 'application/x-www-form-urlencoded'
}
resp = http.post('/login.aspx', data, headers)
cookie = get_cookie(resp)
pp cookie if @debug
unless logged_in?(resp)
puts '* Incorrect Username or Password'
exit 1
end
puts "* Logged in as #{@username}"
@cookie = cookie
end
def create_dir(ticker, company)
dir = "#{ticker}_#{company}"
FileUtils.mkdir dir unless File.directory? dir
dir
end
def search(ticker)
path = "/capsule/TranscriptList.aspx?forceSearch=false&s=#{ticker.downcase}&st=1&r=0&d=1&silo=64&func="
http,resp = get_response(path)
doc = Nokogiri::HTML(resp.body)
tables = doc.css('table')
# the page has 2 tables
# if table #1 has 2 tr>td>b, the text of the second one will be 'No Matches Were Found.'
# if table #1 has just 1 tr>td>b, ticker search returns 1 or more results
if tables[0].css('tr td b').length == 1
doc.css('table')[1].css('tbody tr').each do |tr|
real_ticker = tr.css('td')[0].text.gsub(/[[:space:]]/,'')
a = tr.css('a')[0]
company = a.text.gsub(/\W/,'_').strip
path = a['href']
transcript(path, real_ticker, company)
end
else
out("no matches found for ticker: #{ticker}")
end
end
def transcript(path, ticker, company)
http,resp = get_response(path)
doc = Nokogiri::HTML(resp.body)
if doc.css('#gridTransList thead tr')[1].text.strip.include? 'no data available'
out("no transcript found for #{ticker} - #{company}")
else
options = doc.css("#gridTransList_ctl00_ddlPages option")
pages = options.nil? ? 1 : options.length
(1..pages).to_a.each do |page|
fetch(http, resp, ticker, company, path, page)
end
end
end
def fetch(http,resp, ticker, company, path, page)
# get cid from path
cid = CGI::parse(path.split('?')[1])['cid'][0]
viewstate = ''
resp.body.each_line do |line|
if line =~ /_VIEWSTATE/
viewstate = /value=\"(.*)\"/.match(line)[1]
break
end
end
#pp viewstate
form_enum = {
'__EVENTARGUMENT' => '',
'__EVENTTARGET' => 'gridTransList$ctl00$ddlPages',
'__LASTFOCUS' => '',
'__VIEWSTATE' => viewstate,
'cid' => cid,
'gridTranscriptList$ctl00$ddlPages' => page,
'siteId' => '1',
}
data = URI.encode_www_form(form_enum)
headers = {
'User-Agent' => USERAGENT,
'Cookie' => @cookie,
'Referer' => STREETEVENTS + path,
'Content-Type' => 'application/x-www-form-urlencoded'
}
resp = http.post(path, data, headers)
resp.body.each_line do |line|
if line =~ /text\.thomsonone\.com/
#puts line
line.scan(/javascript:DownloadDocument\('\S+'\)/).each do |s|
url = /javascript:DownloadDocument\('(.*)'\)/.match(s)[1].gsub('amp;','')
if @dl
download(ticker, company, url)
else
out("#{ticker} | #{company} | #{url}")
end
end
end
end
end
def out(line)
File.open(@output, 'a') do |f|
f.puts line
end
end
def download(ticker, company, url)
dir = create_dir(ticker, company)
# fetch text format only
return unless url =~ /format=Text$/
case RUBY_PLATFORM
when /linux/, /darwin/
`wget -P #{dir} -nc --content-disposition -t 3 "#{url}"`
else
system("wget.exe -P #{dir} -nc --content-disposition -t 3 \"#{url}\"")
end
# pause after each successful fetch
pause
end
def pause
count = rand(20..40)
puts "* sleep #{count} seconds"
sleep(count)
end
def usage
puts @opts
exit 1
end
### logger
#log_dt_format = "%Y-%m-%d %H:%M:%S"
#@log = Logger.new('parse.log')
#@log.datetime_format = log_dt_format
#@log.level = Logger::INFO
### options
options = OpenStruct.new
@opts = OptionParser.new
@opts.banner = "Usage: #{File.basename($0)} [options]"
@opts.on('-i', "--input CSV", String, 'Require: inpurt CSV file') do |i|
options.input = i if File.exist?(i)
end
@opts.on('-d', "--download", 'Download Trascripts immediately') do |d|
options.download = d
end
@opts.on('-v', "--debug", 'Show debug message') do |v|
options.debug = v
end
@opts.on_tail("-h", "--help", "Show this message") do
puts @opts
exit
end
@opts.parse! rescue usage
### main
csv = options.input
usage if csv.nil?
@debug = options.debug
@output = 'out.txt'
# download the files immediately?
@dl = options.download
# load config
cfg = load_config
@username = cfg['login']['username']
@password = cfg['login']['password']
# backup the previous output file if it exists
FileUtils.mv(@output, @output.sub(/\.txt$/,'_bak.txt')) if @dl.nil? and File.exist?(@output)
# login
login
CSV.foreach(csv, {:headers => true}) do |r|
ticker = r['of_ticker']
search(ticker)
end
puts "* done !"