package cse308.swift.server.quotequerier; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.log4j.Logger; import com.sun.imageio.plugins.common.InputStreamAdapter; import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List; import cse308.swift.shared.beans.QuoteHistoricalData; import au.com.bytecode.opencsv.CSVReader; public class YahooQuoteQuerier { /** * Instance of the Logger Object */ static Logger logger = Logger.getLogger(YahooQuoteQuerier.class.getName()); /** * The Base URL to Connect to Yahoo's Historical Data Service */ final private String BASE_URL = "http://ichart.finance.yahoo.com/table.csv?" ; public enum IntervalRetrievalType {DAILY, WEEKLY, MONTHLY} /** * This method retrieves the historical data from Yahoo in the form of a .csv file. * @param quoteSymbol The symbol of the Quote * @param startDate The start date of the retrieval * @param endDate The end data of the retrieval * @param retrievalType Whether the Retrieval will return Daily, Weekly or Monthly Data */ private QuoteHistoricalData fetchData(String quoteSymbol, Date startDate, Date endDate, IntervalRetrievalType retrievalType) { String fetchURL = BASE_URL + "s=" + quoteSymbol.toUpperCase(); if(startDate != null) { SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); SimpleDateFormat dayFormat = new SimpleDateFormat("d"); SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy"); fetchURL += "&a=" + monthFormat.format(startDate) + "&b=" + dayFormat.format(startDate) + "&c=" + yearFormat.format(startDate); if(endDate != null) { fetchURL += "&d" + monthFormat.format(endDate) + "&e=" + dayFormat.format(endDate) + "&f=" + yearFormat.format(endDate); } } switch(retrievalType) { case DAILY: fetchURL += "&g=d"; break; case MONTHLY: fetchURL += "&g=m"; break; case WEEKLY: fetchURL += "&g=w"; break; } logger.info("Getting Historical Data for Quote: " + quoteSymbol + "\n Fetch URL: " + fetchURL); try { URL csvURL = new URL(fetchURL); InputStream in = csvURL.openStream(); CSVReader csvReader = new CSVReader(new InputStreamReader(in, "UTF-8")); java.util.List csvList = csvReader.readAll(); QuoteHistoricalData qhd = new QuoteHistoricalData(quoteSymbol, startDate, endDate, fetchURL, csvList); logger.info("Successfully Retrieved Historical Data for Quote: " + quoteSymbol); return qhd; } catch (MalformedURLException e) { logger.warn("Error Generating Fetch URL!"); } catch (UnsupportedEncodingException e) { logger.warn("Error Generating Fetch URL, Encoding is Incorrect!"); } catch (IOException e) { logger.warn("Error Fetching URL - Quote Maybe Inccorrect!"); } return null; } /** * Gets the Historical Data of the Quote Based on the Start Date and End Date * @param quoteSymbol The Quote of the Stock * @param startDate The Start Date from where you want to get the Data From * @param endDate The End Date * @param retrievalType Whether the Retrieval will return Daily, Weekly or Monthly Data */ public QuoteHistoricalData getQuoteHistoricalData(String quoteSymbol, Date startDate, Date endDate, IntervalRetrievalType retrievalType) { return fetchData(quoteSymbol, startDate, endDate, retrievalType); } /** * Gets the Historical Data of the Quote Based on the Start Date * @param quoteSymbol The Quote of the Stock * @param startDate The Start Date from where you want to get the Data From * @param retrievalType Whether the Retrieval will return Daily, Weekly or Monthly Data */ public QuoteHistoricalData getQuoteHistoricalData(String quoteSymbol, Date startDate, IntervalRetrievalType retrievalType) { return fetchData(quoteSymbol, startDate, null, retrievalType); } /** * Gets the Historical Data of the Quote * @param quoteSymbol The Quote of the Stock * @param retrievalType Whether the Retrieval will return Daily, Weekly or Monthly Data */ public QuoteHistoricalData getQuoteHistoricalData(String quoteSymbol, IntervalRetrievalType retrievalType) { return fetchData(quoteSymbol, null, null, retrievalType); } }