In general, Lots of portals provides stock quote information.
For example, @finance.yahoo.com, one can give a valid stock ticker & gets the
stock information. From this point, if you want to know more about company
profile you have to click one link or one more web service call. If you want to know more about last year
statistics, you have to click or one more web service call. Couple of years back, after knowing
little bit of YQL, I did small POC. Basically given a stock ticker, system will display complete
snapshot of the company in one click. It is a fun ride however with all bug in YQL, It remained
as POC only.
Tons of code. But I will dump few important methods.
////
String stockSym = "AAPL";
SupplierSection StockInfo = new SupplierSection(stockSym);
StockInfo.createHttpClient();
StockInfo.crawlFinanceInfo(stockSym);//crawlRssInfo
StockInfo.crawlProfileInfo(stockSym);
StockInfo.crawlKeyStats(stockSym);
StockInfo.crawlRssInfo(stockSym);
StockInfo.crawlQuantInfo(stockSym);//crawlRssInfo
//StockInfo.printAllPublicInfo();
try {
//StockInfo.writeToXmlFile();
//StockInfo.writeToXmlFile1(stockSym);
StockInfo.writehtmlFile1(stockSym);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
///basic compnay info
public void crawlProfileInfo(String stockSym) {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
GetMethod httpGet = new GetMethod("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.stocks%20where%20symbol%3D%22"+stockSym+"%22&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
httpGet.setFollowRedirects( true );
System.out.println("executing request in crawlProfileInfo " );
int responseCode = httpClient.executeMethod(httpGet);
byte[] data = httpGet.getResponseBody();
if (responseCode >= 400) {
System.out.println("Failed to send request "+ responseCode);
}else{
String xmlString = new String( data, ENCODING );
//System.out.println("xml string "+ xmlString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(inStream);
String fName = "CompanyName";
profileInfo.put(fName,getFieldValue(doc,fName));
fName = "Sector";
profileInfo.put(fName,getFieldValue(doc,fName));
fName = "Industry";
profileInfo.put(fName,getFieldValue(doc,fName));
fName = "FullTimeEmployees";
profileInfo.put(fName,getFieldValue(doc,fName));
fName = "start";
profileInfo.put(fName,getFieldValue(doc,fName));
fName = "end";
profileInfo.put(fName,getFieldValue(doc,fName));
}
public void crawlKeyStats(String stockSym) {
try {
GetMethod httpGet = new GetMethod("http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20yahoo.finance.keystats%20WHERE%20symbol%3D'"+stockSym+"'&diagnostics=false&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
httpGet.setFollowRedirects( true );
System.out.println("executing request in crawlKeyStats" );
int responseCode = httpClient.executeMethod(httpGet);
byte[] data = httpGet.getResponseBody();
if (responseCode >= 400) {
System.out.println("Failed to send request "+ responseCode);
}else{
String xmlString = new String( data, ENCODING );
System.out.println("xml string "+ xmlString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(inStream);
processKeyStatsResponse(doc);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
public void crawlRssInfo(String stockSym) {
try {
GetMethod httpGet = new GetMethod("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Ffinance.yahoo.com%2Fq%3Fs%3D"+stockSym+"'%20and%20xpath%3D'%2F%2Fdiv%5B%40id%3D%22yfi_headlines%22%5D%2Fdiv%5B2%5D%2Ful%2Fli%2Fa'&diagnostics=true");
httpGet.setFollowRedirects( true );
System.out.println("executing request in crawlRssInfo" );
int responseCode = httpClient.executeMethod(httpGet);
byte[] data = httpGet.getResponseBody();
if (responseCode >= 400) {
System.out.println("Failed to send request "+ responseCode);
}else{
String xmlString = new String( data, ENCODING );
//System.out.println("xml string "+ xmlString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(inStream);
processRssResponse(doc);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}