コード例 #1
0
 /**
  * @param ticker The ticker of the stock to look up.
  * @return A retrieval.CSV that contains the given stock's data over the past year.
  * @throws ConnectionException if it has problems connecting to Yahoo.
  * @throws exceptions.MissingCSVDataException if Yahoo does not have any data for the given stock.
  */
 public synchronized YahooCSV getStockData(String ticker)
     throws ConnectionException, MissingCSVDataException {
   boolean connectionsuccess = false;
   while (!connectionsuccess) {
     try {
       String contents =
           client
               .getPage(
                   new StringBuilder("http://real-chart.finance.yahoo.com/table.csv?s=")
                       .append(ticker)
                       .toString())
               .getWebResponse()
               .getContentAsString();
       if (contents.charAt(0) == '<') {
         throw new MissingCSVDataException("No data exists for this stock");
       }
       final YahooCSV csv = new YahooCSV(contents, ticker);
       connectionsuccess = true;
       client.close();
       System.out.println(new StringBuilder(ticker).append(" connected to Yahoo").toString());
       return csv;
     } catch (UnknownHostException uhe) {
       // Connection error.
       System.out.println(
           new StringBuilder("Exception in retrieval.")
               .append("InternetConnection.getStockData(")
               .append(ticker)
               .append(")")
               .toString());
       System.out.println(uhe.toString());
       System.out.println("Waiting to reconnect.");
       try {
         Thread.sleep(5000);
       } catch (Exception e) {
         System.out.println(
             new StringBuilder("Exception in ")
                 .append("retrieval.InternetConnection.getStockData(")
                 .append(ticker)
                 .append(")")
                 .toString());
         System.out.println(uhe.toString());
       }
     } catch (MissingCSVDataException mcsvde) {
       // There is no CSV data for this stock.
       System.out.println(
           new StringBuilder("Exception in ")
               .append("retrieval.InternetConnection.getStockData(")
               .append(ticker)
               .append(")")
               .toString());
       System.out.println(mcsvde.toString());
       connectionsuccess = true;
       client.close();
     } catch (Exception e) {
       System.out.println(
           new StringBuilder("Exception in ")
               .append("retrieval.InternetConnection.getStockData(")
               .append(ticker)
               .append(")")
               .toString());
       System.out.println(e.toString());
     }
   }
   throw new ConnectionException(
       new StringBuilder("Unable to find CSV File for ").append(ticker).toString());
 }
コード例 #2
0
 /**
  * @param ticker The ticker of the stock to look up.
  * @param startdate The first date of the range of dates over which to search.
  * @return A retrieval.CSV that contains the given stock's data over the past year.
  * @throws ConnectionException if it has problems connecting to Yahoo.
  * @throws exceptions.MissingCSVDataException if Yahoo does not have any data for the given stock.
  * @throws exceptions.InvalidStartDateException if the start date is equal to or after the current
  *     date.
  */
 public synchronized YahooCSV getStockData(String ticker, GregorianCalendar startdate)
     throws ConnectionException, MissingCSVDataException, InvalidStartDateException {
   // Make sure that the start date is before today!
   GregorianCalendar today = new GregorianCalendar();
   if (!startdate.before(today)) {
     throw new InvalidStartDateException("The start date must be before the current date.");
   }
   // This connection loop will re-connect to Yahoo if the initial and
   // subsequent attempts fail.
   boolean connectionsuccess = false;
   while (!connectionsuccess) {
     try {
       String contents =
           client
               .getPage(
                   new StringBuilder("http://real-chart.finance.yahoo.com/table.csv?s=")
                       .append(ticker)
                       .append("&a=")
                       .append(startdate.get(Calendar.MONTH))
                       .append("&b=")
                       .append(startdate.get(Calendar.DATE))
                       .append("&c=")
                       .append(startdate.get(Calendar.YEAR))
                       .toString())
               .getWebResponse()
               .getContentAsString();
       if (contents.charAt(0) == '<') {
         // There is no stock data for the given ticker, and Yahoo
         // has responded with an error page rather than a CSV.
         throw new MissingCSVDataException("No data exists for this stock");
       }
       final YahooCSV csv = new YahooCSV(contents, ticker);
       connectionsuccess = true;
       client.close();
       System.out.println(ticker + " connected to Yahoo.");
       return csv;
     } catch (UnknownHostException uhe) {
       // Connection error.
       System.out.println(
           new StringBuilder("Exception in ")
               .append("retrieval.InternetConnection.getStockData(")
               .append(ticker)
               .append(")")
               .toString());
       System.out.println(uhe.toString());
       System.out.println("Waiting to reconnect.");
       try {
         Thread.sleep(5000);
       } catch (Exception e) {
         System.out.println(
             new StringBuilder("Exception in ")
                 .append("retrieval.InternetConnection.getStockData(")
                 .append(ticker)
                 .append(")")
                 .toString());
         System.out.println(uhe.toString());
       }
     } catch (MissingCSVDataException mcsvde) {
       // There is no CSV data for this stock.
       System.out.println(
           new StringBuilder("Exception in ")
               .append("retrieval.InternetConnection.getStockData(")
               .append(ticker)
               .append(")")
               .toString());
       System.out.println(mcsvde.toString());
       connectionsuccess = true;
       client.close();
     } catch (Exception e) {
       System.out.println(
           new StringBuilder("Exception in ")
               .append("retrieval.InternetConnection.getStockData(")
               .append(ticker)
               .append(")")
               .toString());
       System.out.println(e.toString());
     }
   }
   throw new ConnectionException(
       new StringBuilder("Unable to find CSV File for ").append(ticker).toString());
 }