Search This Blog

Wednesday, June 5, 2013

Java and Google Finance

Here are a couple code snippets using JDK6 to fetch data from the Google Finance website.  
Here are some default variables.  The base URL is the google site that will return a json string with requested symbols.  The symbols file is sitting on my Desktop and is just a CSV with 1 column.  Each record is a stock symbol. e.g. JNJ
The Boolean friendlyPrint variable determines the output format.
 private static String defaultBaseUrl = "http://www.google.com/finance/info?infotype=infoquoteall&q=";  
  private static String defaultSymbolsFile = System.getProperty("user.home")  
   + "/Desktop/NasdaqSymbols.csv";  
  private static boolean friendlyPrint = true;  
This main method first creates an ArrayList of the first 100 stock symbols from the CSV, and then appends them with a comma to the base url.  The URL is submitted and the response is parsed into a Simple-JSON Array. Last, the data is displayed on the console.
  public static void main(String[] args) throws Exception {  
  ArrayList symbols = FileUtils.getTextListValues(defaultSymbolsFile);  
  StringBuilder syms = new StringBuilder();   
  String sb = Util.fetchURL(defaultBaseUrl + syms);  
  JSONArray json = (JSONArray) new JSONParser().parse(sb.substring(3));  
  console.utils.GoogleFinancePrinter.printJsonArray(json, friendlyPrint);  
  }  
This is the method called above to fetch the data using the URLConnection object. It reads each line of the response and passes it into a stringbuilder. Then returns the final string.
 static String fetchURL(String url) throws Exception {   
  URL gf = new URL(url);   
  URLConnection yc = gf.openConnection();   
  BufferedReader in = new BufferedReader(new InputStreamReader(   
   yc.getInputStream()));   
  StringBuilder sb = new StringBuilder();   
  String inputLine;   
  while ((inputLine = in.readLine()) != null)   
   sb.append(inputLine);   
  in.close();   
  return sb.toString();   
  }   
Here is the method that prints the data on the console. There is more data in the json object than what is displayed in the friendly condition. These are just a few.
  public static void printJsonArray(JSONArray json, boolean friendly) {  
  if (friendly) {  
   for (int i = 0; i < json.size(); i++) {  
   System.out.println("ID: "  
    + ((JSONObject) json.get(i)).get("id"));  
   System.out.println("Symbol: "  
    + ((JSONObject) json.get(i)).get("t"));  
   System.out.println("Name: "  
    + ((JSONObject) json.get(i)).get("name"));  
   System.out.println("Type: "  
    + ((JSONObject) json.get(i)).get("type"));  
   System.out.println("Exchange: "  
    + ((JSONObject) json.get(i)).get("e"));  
   System.out.println("LastTrade: $"  
    + ((JSONObject) json.get(i)).get("l"));  
   System.out.println("Last Trade: "  
    + ((JSONObject) json.get(i)).get("lt"));  
   System.out.println("Change: "  
    + ((JSONObject) json.get(i)).get("c") + "("  
    + ((JSONObject) json.get(i)).get("cp") + "%)");  
   System.out.println("------------------------------");  
   }  
  } else {  
   System.out.print(json.toJSONString());  
  }  
  }  

No comments:

Post a Comment