-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrensch.rb
95 lines (70 loc) · 1.8 KB
/
wrensch.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
require 'rubygems'
require 'mechanize'
require 'open-uri'
require 'hpricot'
require 'date'
#
# Jams stuff together
#
def camelCase(phrase)
phrase = phrase.scan(/[[:print:]]/).join
phrase.gsub!(/^[a-z]|\s+[a-z]/) { |a| a.upcase }
phrase.gsub!(/[\s]/, '')
phrase.gsub!(/[,)]/, '')
phrase.gsub!(/[(]/, '-')
return phrase
end
def fixInteger(num)
phrase.gsub!(/\(/, '-')
end
def fixDate(indate)
Date.parse("%s %s %s" % [indate[0..2],indate[3..4],indate[5..8]]).to_s
end
def yahooFinanceURLQuery(type,ticker)
'http://finance.yahoo.com/q/%s?s=%s' % [type,ticker]
end
#
#
# Get Financial Data from Yahoo-
#
# url: http://finance.yahoo.com/q/
# type: cf=cashflow, bs=balance sheet, is=income statement
# ticker
def getYahooFinancialData(type,ticker)
newTable = Array.new
@dateArray = Array.new
dom = '//*[@class="yfnc_tabledata1"]//tr'
page = Hpricot(open(yahooFinanceURLQuery(type,ticker)))
#page = Hpricot(open("is.html"))
(page/dom).each do |row|
newRow = Array.new
(row/'td|th').each do |element|
newRow << camelCase(element.inner_text.strip)
end
newRow.shift if newRow.first == ""
newTable << newRow if newRow.count > 2
end
reOrderTable = Array.new
##pp newTable if @debug
newTable.each do |param|
if param[0] == 'PeriodEnding' then
param.shift ## just the dates please.
param.each {|p| @dateArray << fixDate(p)}
next
end
index = 1 ## Indexes, really? really???
@dateArray.each do |mydate|
reOrderTable << [ticker,mydate,param[0],param[index]]
index = index + 1
end
end
pp reOrderTable
end
class TableThing
end
table = getYahooFinancialData('is','T')
##writeit('is.dat',table)
table = getYahooFinancialData('bs','T')
##writeit('bs.dat',table)
table = getYahooFinancialData('cf','T')
##writeit('cf.dat',table)