Skip to content

Commit

Permalink
Implement Analyst Opinion
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjoyghosh committed Mar 7, 2015
1 parent 6f02d8a commit 6ae2c7a
Showing 1 changed file with 116 additions and 65 deletions.
181 changes: 116 additions & 65 deletions yahoo/finance/yahoo.finance.analystopinion.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<author>Gang Liang</author>
<description>
Yahoo Finance - Analyst Estimate by Stock Symbol
</description>
<sampleQuery>
SELECT * FROM {table} WHERE symbol='PRLB'
</sampleQuery>
<author>Sanjoy Ghosh</author>
<description>Returns Analyst Opinions from the Yahoo Analyst Opinion page</description>
<documentationURL><!-- url for API documentation --></documentationURL>
<apiKeyURL><!-- url for getting an API key if needed --></apiKeyURL>
<sampleQuery>select * from {table} where symbol = 'aapl'</sampleQuery>
</meta>
<bindings>
<select itemPath="" produces="XML">
Expand All @@ -20,68 +18,121 @@
</inputs>
<execute>
<![CDATA[
function getelement(row)
{
if(row.hasOwnProperty("p")) return(row.p.text());
return(row.font.text());
}
// Setup Query from finance.yahoo.com
var url="http://finance.yahoo.com/q/ae?s=" + symbol;
var url="https://finance.yahoo.com/q/ao?s=" + symbol + "+Analyst+Opinion";
var restquery = y.rest( url );
var rawresult = restquery.accept( "text/html" ).get().response;
var aequery = y.xpath(
rawresult,
"//table[@class='yfnc_tableout1']/tr[count(td)=0]/parent::*|" +
"//table[@class='yfnc_tableout1']/tr/td/table"
);
// Process Results
var aedata = <results symbol={symbol}></results>;
var i = 0;
while(i < aequery.length())
{
var table = aequery[i];
var thead = table.tr[0];
var tname = thead.th[0].strong.text().toString().replace(/ /g, "");
var fname1 = thead.th[1].p.text().toString().replace(/\n.*/, "");
var fname2 = thead.th[2].p.text().toString().replace(/\n.*/, "");
var fname3 = thead.th[3].p.text().toString().replace(/\n.*/, "");
var fname4 = thead.th[4].p.text().toString().replace(/\n.*/, "");
fname1 = fname1.replace(/[\s\.]+/g, "").replace(/\&/,"");
fname2 = fname2.replace(/[\s\.]+/g, "").replace(/\&/,"");
fname3 = fname3.replace(/[\s\.]+/g, "").replace(/\&/,"");
fname4 = fname4.replace(/[\s\.]+/g, "").replace(/\&/,"");
var tblval = <{tname}></{tname}>;
var j = 1;
while(j < table.tr.length())
{
var row = table.tr[j].td;
var rname = row[0].p.text().toString().replace(/[\s\.]+/g, "");
rname = rname.replace(/\(.*\)/g,"").replace(/\%/,"").replace(/^(\d)/,"_$1");
rname = rname.replace(/\//, "");
var rval1 = getelement(row[1]);
var rval2 = getelement(row[2]);
var rval3 = getelement(row[3]);
var rval4 = getelement(row[4]);
tblval.appendChild(<{rname}>
<{fname1}>{rval1}</{fname1}>
<{fname2}>{rval2}</{fname2}>
<{fname3}>{rval3}</{fname3}>
<{fname4}>{rval4}</{fname4}>
</{rname}>);
j = j + 1;
}
aedata.appendChild(tblval);
i = i + 1;
}
// Return aedata strucuture
var aedata = <analystOpinion symbol={symbol}></analystOpinion>;
var thisWeekQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1 equaltable']//td/p[text() = 'Mean Recommendation (this week):']/../../td[2]/p");
var thisWeek = thisWeekQuery.text();
var lastWeekQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1 equaltable']//td/p[text() = 'Mean Recommendation (last week):']/../../td[2]/p");
var lastWeek = lastWeekQuery.text();
var changeQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1 equaltable']//td/p[text() = 'Change:']/../../td[2]/p");
var change = changeQuery.text();
var recommendationSummary = <recommendationSummary thisWeek={thisWeek} lastWeek={lastWeek} change={change}></recommendationSummary>;
aedata.appendChild(recommendationSummary);
var meanQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1 equaltable']//td/p[text() = 'Mean Target:']/../../td[2]/p");
var mean = meanQuery.text();
var medianQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1 equaltable']//td/p[text() = 'Median Target:']/../../td[2]/p");
var median = medianQuery.text();
var highQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1 equaltable']//td/p[text() = 'High Target:']/../../td[2]/p");
var high = highQuery.text();
var lowQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1 equaltable']//td/p[text() = 'Low Target:']/../../td[2]/p");
var low = lowQuery.text();
var numOfBrokersQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1 equaltable']//td/p[text() = 'No. of Brokers:']/../../td[2]/p");
var numOfBrokers = numOfBrokersQuery.text();
var priceTargetSummary = <priceTargetSummary mean={mean} median={median} high={high} low={low} numOfBrokers={numOfBrokers}></priceTargetSummary>;
aedata.appendChild(priceTargetSummary);
var strongBuyCurrentMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Strong Buy']/../../td[1]/p");
var strongBuyCurrentMonth = strongBuyCurrentMonthQuery[0].text();
var strongBuyLastMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Strong Buy']/../../td[2]/p");
var strongBuyLastMonth = strongBuyLastMonthQuery[0].text();
var strongBuyTwoMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Strong Buy']/../../td[3]/p");
var strongBuyTwoMonthsAgo = strongBuyTwoMonthsAgoQuery[0].text();
var strongBuyThreeMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Strong Buy']/../../td[4]/p");
var strongBuyThreeMonthsAgo = strongBuyThreeMonthsAgoQuery[0].text();
var buyCurrentMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Buy']/../../td[1]/p");
var buyCurrentMonth = buyCurrentMonthQuery[0].text();
var buyLastMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Buy']/../../td[2]/p");
var buyLastMonth = buyLastMonthQuery[0].text();
var buyTwoMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Buy']/../../td[3]/p");
var buyTwoMonthsAgo = buyTwoMonthsAgoQuery[0].text();
var buyThreeMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Buy']/../../td[4]/p");
var buyThreeMonthsAgo = buyThreeMonthsAgoQuery[0].text();
var holdCurrentMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Hold']/../../td[1]/p");
var holdCurrentMonth = holdCurrentMonthQuery[0].text();
var holdLastMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Hold']/../../td[2]/p");
var holdLastMonth = holdLastMonthQuery[0].text();
var holdTwoMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Hold']/../../td[3]/p");
var holdTwoMonthsAgo = holdTwoMonthsAgoQuery[0].text();
var holdThreeMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Hold']/../../td[4]/p");
var holdThreeMonthsAgo = holdThreeMonthsAgoQuery[0].text();
var underperformCurrentMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Underperform']/../../td[1]/p");
var underperformCurrentMonth = underperformCurrentMonthQuery[0].text();
var underperformLastMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Underperform']/../../td[2]/p");
var underperformLastMonth = underperformLastMonthQuery[0].text();
var underperformTwoMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Underperform']/../../td[3]/p");
var underperformTwoMonthsAgo = underperformTwoMonthsAgoQuery[0].text();
var underperformThreeMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Underperform']/../../td[4]/p");
var underperformThreeMonthsAgo = underperformThreeMonthsAgoQuery[0].text();
var sellCurrentMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Sell']/../../td[1]/p");
var sellCurrentMonth = sellCurrentMonthQuery[0].text();
var sellLastMonthQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Sell']/../../td[2]/p");
var sellLastMonth = sellLastMonthQuery[0].text();
var sellTwoMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Sell']/../../td[3]/p");
var sellTwoMonthsAgo = sellTwoMonthsAgoQuery[0].text();
var sellThreeMonthsAgoQuery = y.xpath(rawresult,"//table[@class='yfnc_datamodoutline1']//th/p[text() = 'Sell']/../../td[4]/p");
var sellThreeMonthsAgo = sellThreeMonthsAgoQuery[0].text();
var recommendationTrends = <recommendationTrends>
<currentMonth strongBuy={strongBuyCurrentMonth} buy={buyCurrentMonth} hold={holdCurrentMonth} underperform={underperformCurrentMonth} sell={sellCurrentMonth}></currentMonth>
<lastMonth strongBuy={strongBuyLastMonth} buy={buyLastMonth} hold={holdLastMonth} underperform={underperformLastMonth} sell={sellLastMonth}></lastMonth>
<twoMonthsAgo strongBuy={strongBuyTwoMonthsAgo} buy={buyTwoMonthsAgo} hold={holdTwoMonthsAgo} underperform={underperformTwoMonthsAgo} sell={sellTwoMonthsAgo}></twoMonthsAgo>
<threeMonthsAgo strongBuy={strongBuyThreeMonthsAgo} buy={buyThreeMonthsAgo} hold={holdThreeMonthsAgo} underperform={underperformThreeMonthsAgo} sell={sellThreeMonthsAgo}></threeMonthsAgo>
</recommendationTrends>;
aedata.appendChild(recommendationTrends);
response.object = aedata;
]]>
</execute>
</select>
</bindings>
</table>

</table>

0 comments on commit 6ae2c7a

Please sign in to comment.