Hello I am new to programming so please bear with me as this may be the stupidest question yet!
I am trying to pull data through JSON from yahoo finance pages with the following code. When I run the code in the YQL console it throws the following error.
THIS IS THE ERROR:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
yahoo:count="0" yahoo:created="2012-04-13T10:28:51Z" yahoo:lang="en-US">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<url execution-start-time="24" execution-stop-time="27"
execution-time="3" proxy="DEFAULT"><![CDATA[http://www.datatables.org/yahoo/finance/yahoo.finance.stock.xml]]></url>
<javascript><![CDATA[Error: double default label in the switch statement<javascript>:180: default:^]]></javascript>
<javascript><![CDATA[Error: invalid return<javascript>:191:return ^true;]]></javascript>
<javascript><![CDATA[Error: syntax error<javascript>:192: }^]]></javascript>
<javascript><![CDATA[Exception: Compilation produced 3 syntax errors. (<javascript>#1)]]></javascript>
<javascript execution-time="2" instructions-used="0" table-name="yahoo.finance.stock"/>
<user-time>32</user-time>
<service-time>3</service-time>
<build-version>26535</build-version>
</diagnostics>
<results/>
</query>
THIS IS THE CODE THAT THROWS THE ERROR
<?xml version="1.0" encoding="UTF-8"?>
-
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
-
<meta>
<author>Bob Cudmore</author>
<description>Yahoo Finance Stock Summary Information</description>
<sampleQuery>select * from {table} where symbol="yhoo"</sampleQuery>
<documentationURL>To Be Declared</documentationURL>
</meta>
-
<bindings>
-
<select produces="XML" itemPath="">
-
<urls>
<url />
</urls>
-
<inputs>
<key required="true" paramType="variable" type="xs:string" id="symbol" />
</inputs>
-
<execute>
<![CDATA[
// pad string with leading char String.prototype.pad = function (padchar, padlen)
{ s = this while (s.length < padlen)
{ s = padchar + s;
}
return s;
}
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);
}
String.prototype.toInt = function ()
{
// remove leading zeros because otherwise str can be interpreted as Octal
var str = this.replace(/^0+/, ''); return parseInt(str);
}
function getQuoteInfo()
{
// Get company name & market from Yahoo Quotes Summary page
var results = quoteQuery.results; elements = results.*.length(); if (elements == 0) return false;
stock.CompanyName = results.h1.text().toString().trim(); var marketSymbolStr = results.div.span.toString();
//var match = marketSymbolStr.match(/^[^:]+:\s+([^)]+)\)$/); matches stock symbol
var match = marketSymbolStr.match(/^\(([^:]+):/); if (match != null)
{
stock.CompanyName += <Market>{match[1]}</Market>;
}
return true;
}
function getHistoricalPrice()
{
// Get the Historical Price Range
var results = historicalQuery.results; elements = results.*.length();
if (elements < 6)
{
y.log("not enough elements");
return false;
}
startMonth = String(results.option[0].@value.toString().toInt() + 1) .pad("0", 2);
startDay = results.input[0].@value.toString().pad("0", 2);
startYear = results.input[1].@value.toString();
endMonth = String(results.option[1].@value.toString().toInt() + 1) .pad("0", 2);
endDay = results.input[2].@value.toString().pad("0", 2);
endYear = results.input[3].@value.toString();
startDate = startYear + "-" + startMonth + "-" + startDay;
endDate = endYear + "-" + endMonth + "-" + endDay;
stock.appendChild(<start>{startDate}</start>);
stock.appendChild(<end>{endDate}</end>);
stock.appendChild(<StartDate>{startDate}</StartDate>);
stock.appendChild(<EndDate>{endDate}</EndDate>);
return true;
}
function getExecutive(executives, tr)
{
var executive = <Executive></Executive>;
executive.node += <Name>{tr.td.strong.text()}</Name>;
var m = tr.td.p[0].text().toString().match(/^,\s*(\d+)/); if (m)
{
executive.node += <Age>{m[1]}</Age>;
}
var titles = tr.td.p[0].small.toString().split(/,\s*|\s+and\s+/i);
for (var i=0; i<titles.length; i++) executive.node += <Title>{titles[i]}</Title>;
var pay = tr.td[1].p.text().toString().replace(/\s+/g, "");
executive.node += <Pay>{pay}</Pay> var exercised = tr.td[2].p.text().toString().replace(/\s+/g, "");
executive.node += <Exercised>{exercised}</Exercised> executives.node += executive;
}
function getAddressInfo(stock, td)
{
var companyName = td.strong.text().toString().trim(); //stock.node += <Name>{companyName}</Name>;
var mapref = td.p.a[0].@href;
var m = mapref.match(/addr=([^&]+)&csz=([^&]+)&country=(.*)$/i);
if (m)
{
var address = m[1].replace(/%20/g, " ");
var country = m[3].replace(/%20/g, " ");
var csz = m[2].replace(/%20/g, " ");
var m2 = csz.match(/(.*?)\s+([a-z]+)\s+([-\d]+)$/i);
if (m2)
{
var city = m2[1];
var state = m2[2];
var zip = m2[3];
}
else
{
var city = csz;
}
var address = <Address> <Street>{address}</Street> <City>{city}</City> <State>{state}</State> <Zip>{zip}</Zip> <Country>{country}</Country> </Address>;
stock.node += address;
}
var firstPara = td.p[0].toString();
firstpara = firstPara.replace(/</, "<");
firstpara = firstPara.replace(/>/, ">");
var lines = firstPara.split(/\s*(<p>|<br\s*\/>)\s*/i);
for (var i=0; i<lines.length; i++)
{
var colonMatch = lines[i].match(/^([^:]+)\s*?!\/)\s*(.*?)\s*$/);
if (colonMatch)
{
var property = colonMatch[1].replace(/\s/, "");
var value = colonMatch[2];
var aMatch = value.match(/<a\b[^>]+>\s*([^<]+?)\s*<\/a>/i);
if (aMatch) value = aMatch[1];
stock.node += <{property}>{value}</{property}>;
}
}
}
function getProfileInfo()
{
// Get the Sector, Industry, Full Time Employees from Profile page
var results = profileQuery.results;
elements = results.*.length();
if (elements == 0) return false;
getAddressInfo(stock, results.td);
var executives = <Executives></Executives> for each (var tr in results.tr)
{
//Remove trailing colon, and strip whitespace
var property = tr.td[0].p.text() .toString().slice(0, -1) .replace(/\s+/g, "");
switch (property.toLowerCase())
{
case 'indexmembership':
var indexes = <Indexes></Indexes> for each (var a in tr.td[1].a)
{
var iSymbol = a.@href.match(/=([^&]+)/)[1];
iSymbol = iSymbol.replace(/%5E/gi, "^");
indexes.node += <Index symbol={iSymbol}>{a.text()}</Index>;
}
if (indexes.length() > 0) stock.appendChild(indexes);
continue;
break;
case 'fulltimeemployees':
//Strip commas
value = tr.td[1].*.text().toString().replace(/,/g, "");
break;
default: case 'sector': case 'industry':
//Convert whitespace to single space
value = tr.td[1].*.text().toString().replace(/\s+/g, " ");
break;
default: if (property.match(/^,/)) getExecutive(executives, tr);
continue;
break;
}
stock.appendChild(<{property}>{value}</{property}>);
}
if (executives.length() > 0) stock.appendChild(executives);
return true;
}
// Queue the queries
var url = "http://finance.yahoo.com/q/pr?s="+symbol;
var profileQuery = y.query( "select * from html " + "where url=@url and " + "xpath='//table[@class=\"yfnc_datamodoutline1\"]/tr/td/table/tr' "
+ "limit 4", "xpath='" + "//td[@class=\"yfnc_modtitlew2\"] | " + "//table[@class=\"yfnc_datamodoutline1\"]/tr/td/table/tr"
+ "'", {url:url});
var url = "http://finance.yahoo.com/q?s="+symbol;
var quoteQuery = y.query( "select * from html " + "where url=@url and " + "xpath='"
+ "//div[@id=\"yfi_investing_head\"]/h1 | " + "//div[@class=\"yfi_quote_summary\"]/div[1]'" , {url:url});
var url = "http://finance.yahoo.com/q/hp?s="+symbol;
var historicalQuery = y.query( "select * from html " + "where url=@url and " + "xpath='" + "//option[@selected=\"selected\"] | " + "//input[@maxlength=\"2\"] | "
+ "//input[@maxlength=\"4\"]'", {url:url});
var stock = <stock symbol={symbol}></stock>; getQuoteInfo(); getHistoricalPrice(); getProfileInfo(); response.object = stock ]]>
</execute>
I have attached the source XML file as a RAR file for ease of reading in dreamweaver or some other editor.
Any help would much appreciated.
Bob