-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.xml
148 lines (138 loc) · 5.53 KB
/
test.xml
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
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<author>
Ryan Hoium
</author>
<description>
Yahoo Finance - Balance Sheet v0.1
</description>
<sampleQuery>
SELECT * FROM {table} WHERE symbol='T'
</sampleQuery>
</meta>
<bindings>
<select itemPath="" produces="XML">
<urls>
<url>
</url>
</urls>
<inputs>
<key id="symbol" type="xs:string" paramType="variable" required="true" />
<key id="reporttype" type="xs:string" paramType="variable" default="balancesheet" required="false" />
<key id="timeframe" type="xs:string" paramType="variable" default="quarterly" required="false" />
</inputs>
<execute>
<![CDATA[
//UTILITY: pad string with leading char
String.prototype.pad = function ( padchar, padlen )
{
s = this
while (s.length < padlen)
{
s = padchar + s;
}
return s;
}
//UTILITY: convert string to INTEGER
String.prototype.toInt = function ()
{
// remove leading 0's because otherwise
// str can be interpreted as Octal
var str = this.replace( /^0+/, '' );
// also the thousands comma was cousing trouble
str = str.trim()
str = str.replace( /\,/g, '' );
// replace M with 6 ZEROs, B with 9 ZEROs (UPDATE THIS - ACCOUNT FOR DECIMAL!!
//str = str.replace(/M/g,'000000');
//str = str.replace(/B/g,'000000000');
return parseInt( str );
}
//UTILITY: trim whitespace
String.prototype.trim = function ()
{
var str = this.replace( /^\s\s*/, "" ),
ws = /\s/,
i = str.length;
while ( ws.test( str.charAt( --i ) ) );
return str.slice( 0, i + 1 );
}
// Setup Query from finance.yahoo.com
var reporttype_code = "";
//reporttype = y.context.table;
if(reporttype == "incomestatement"){reporttype_code = "is";}
else if(reporttype == "balancesheet"){reporttype_code = "bs";}
else if(reporttype == "cashflow"){reporttype_code = "cf";}
var url="http://finance.yahoo.com/q/"+ reporttype_code + "?s=" + symbol + "&" + timeframe;
var restquery = y.rest( url );
var rawresult = restquery.accept( "text/html" ).get().response;
var query = y.xpath( rawresult, "//table[@class='Lh(1.7) W(100%) M(0)']/tbody/tr");
// Process Results
var outputdata = <{reporttype} symbol={symbol} timeframe={timeframe}></{reporttype}>;
if ( query.*.length() !== 0 )
{
var y = 0; var x = 0;
var numcolumns = 0;
var data_array = new Array();
// Read Column Headings
data_array[0] = new Array()
data_array[0][0] = "Headings"
for each ( var th in query[0].td)
{
data_array[x] = new Array();
data_array[x][y] = th.*.text().toString();
//outputdata.appendChild(<column x={x} y={y}>{data_array[x][y]}</column>);
x++;
}
numcolumns = x;
// Read Rows
y = 1;
var i = 1;
while ( i < query.length())
{
var row = query[i];
if(row.td.length() >= numcolumns){
x = 0;
// read each column in row
for each (var column in row.td){
if("spacer" in column)
{}
else{
var dataitem = column.*.text().toString();
dataitem = String(dataitem.replace(/\s+/g,""));
dataitem = String(dataitem.replace(/,/g,""));
dataitem = String(dataitem.replace(/\//g,"_"));
if(dataitem.match(/^[\(\)]/)){
dataitem = Number(dataitem.replace(/[\(\)]/g,"")) * -1000;
} else if(dataitem.match(/^[0-9]{1,}$/)){
dataitem = Number(dataitem) * 1000;
}
data_array[x][y] = dataitem;
//outputdata.appendChild(<row x={x} y={y}>{dataitem}</row>);
x++;
}
}
y++;
}
i++;
}
var numrows = y;
x = 0; y = 0;
for(x = 1; x < numcolumns; x++){
var period = data_array[x][0];
var financial_period = <statement period={period}></statement>;
for(y = 1; y < numrows; y++){
row_name = data_array[0][y];
data = data_array[x][y];
financial_period.appendChild(<{row_name} report_row={y}>{data}</{row_name}>);
}
outputdata.appendChild(financial_period);
}
}
// Return statsdata strucuture
response.object = outputdata;
]]>
</execute>
</select>
</bindings>
</table>