Yahoo Finance Quotes YQL Rest JSON


References before one goes into stuff:

  • https://developer.yahoo.com/java/howto-parseRestJava.html#parsejson
  • https://developer.yahoo.com/yql/
  • https://developer.yahoo.com/yql/console/

Here is the code:

/**
 * 
 */
package com.kant.yahooFinanceQuoteReader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author shashi
 * 
 */
public class YahooFinanceQuoteClient {

 private static final String QUOTE = "quote";
 private static final String RESULTS = "results";
 private static final String QUERY = "query";
 private final String[] values = { "symbol", "Ask", "AverageDailyVolume",
   "PercentChange", "StockExchange", "DaysRange" };

 /**
  * @param args
  */
 public static void main(String[] args) {
  YahooFinanceQuoteClient yahooClient = new YahooFinanceQuoteClient();
  List arguments = new ArrayList<>();
  arguments.add("YHOO");
  arguments.add("AAPL");
  arguments.add("GOOG");
  arguments.add("MSFT");
  yahooClient.checkYahooFinanceForQuotes(arguments);

  //Above or this
  /*for (String item : arguments) {
   yahooClient.checkYahooFinanceForQuote(item);
  }*/
 }

 /**
  * 
  */
 public void checkYahooFinanceForQuote(String symbol) {
  try {
   String urlString = "http://query.yahooapis.com/v1/public/yql?"/* "http://finance.yahoo.com/rss/headline?" */;
   String params = "q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22"
     + symbol
     + "%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
   URL url = new URL(urlString + params);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   StringBuffer buffer = getResponseFromWS(conn);
   System.out.println("[Response]:");
   System.out.println(buffer);
   readJsonObject(buffer);
   conn.disconnect();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /**
  * 
  * @param args
  */
 public void checkYahooFinanceForQuotes(List args) {
  try {
   String urlString = "http://query.yahooapis.com/v1/public/yql?";
   String header = "q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(";
   String tail = ")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
   Iterator iterator = args.iterator();
   String subPart = "%22";
   while (iterator.hasNext()) {
    if (!subPart.equals("%22"))
     subPart = subPart + "%2C%22";
    String subStr = iterator.next();
    subPart = subPart + subStr + "%22";
   }
   // System.out.println(header + subPart + tail);
   URL url = new URL(urlString + header + subPart + tail);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   StringBuffer buffer = getResponseFromWS(conn);
   System.out.println("[Response]:");
   System.out.println(buffer);
   readJsonObject(buffer);
   conn.disconnect();
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private StringBuffer getResponseFromWS(HttpURLConnection conn)
   throws IOException {
  conn.setRequestMethod("GET");
  conn.setRequestProperty("Accept", "application/text");
  if (conn.getResponseCode() != 200) {
   throw new RuntimeException("Failed : HTTP error code : "
     + conn.getResponseCode());
  }
  BufferedReader br = new BufferedReader(new InputStreamReader(
    (conn.getInputStream())));
  String output = null;
  StringBuffer buffer = new StringBuffer();
  while ((output = br.readLine()) != null) {
   buffer.append(output);
  }
  return buffer;
 }

 /**
  * 
  * @param buffer
  */
 private void readJsonObject(StringBuffer buffer) {
  try {
   JSONObject jo = new JSONObject(buffer.toString());
   jo = jo.getJSONObject(QUERY);
   jo = jo.getJSONObject(RESULTS);
   if (jo.optJSONObject(QUOTE) == null) {
    JSONArray ja = jo.getJSONArray(QUOTE);
    for (int i = 0; i < ja.length(); i++) {
     JSONObject resultObject = ja.getJSONObject(i);
     printJsonObject(resultObject);
    }
   } else {
    JSONObject resultObject = jo.getJSONObject(QUOTE);
    printJsonObject(resultObject);
   }
  } catch (JSONException e) {
   e.printStackTrace();
  }
 }

 /**
  * 
  * @param resultObject
  * @throws JSONException
  */
 private void printJsonObject(JSONObject resultObject) throws JSONException {
  System.out.println("--");
  for (String value : values)
   System.out.println(value + ":  " + resultObject.get(value));
  System.out.println("--");
 }

}



Comments