| 1 | package net.sourceforge.finmodel.asset.stock; |
| 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.InputStreamReader; |
| 5 | import java.math.BigDecimal; |
| 6 | import java.net.URL; |
| 7 | import java.net.URLConnection; |
| 8 | |
| 9 | import org.apache.commons.logging.Log; |
| 10 | import org.apache.commons.logging.LogFactory; |
| 11 | |
| 12 | public class StockLookup { |
| 13 | private static Log log = LogFactory.getLog(StockLookup.class); |
| 14 | |
| 15 | private String ticker; |
| 16 | private String name; |
| 17 | private String exchange; |
| 18 | private BigDecimal price; |
| 19 | private BigDecimal dividend; |
| 20 | |
| 21 | private String providerUrl = "http://finance.yahoo.com/d/quotes.csv"; |
| 22 | |
| 23 | public StockLookup(String ticker) { |
| 24 | this.ticker = ticker; |
| 25 | update(); |
| 26 | } |
| 27 | |
| 28 | public void update() |
| 29 | { |
| 30 | String stockURL = providerUrl + "?s=" + ticker + "&f=npyx"; |
| 31 | try { |
| 32 | URLConnection feed = new URL(stockURL).openConnection(); |
| 33 | |
| 34 | BufferedReader input = new BufferedReader( new InputStreamReader( feed.getInputStream())); |
| 35 | String rawData = input.readLine(); |
| 36 | // System.err.println(rawData); |
| 37 | if (!rawData.equals("")) { |
| 38 | String data[] = rawData.split(","); |
| 39 | name = data[0]; |
| 40 | price = new BigDecimal(data[1]); |
| 41 | if (!data[2].equals("N/A")) { |
| 42 | dividend = new BigDecimal(data[2]); |
| 43 | } else { |
| 44 | dividend = BigDecimal.ZERO; |
| 45 | } |
| 46 | exchange = data[3]; |
| 47 | } |
| 48 | |
| 49 | } catch (Exception e) { |
| 50 | log.error("Error getting data: " + stockURL, e); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public String getExchange() { |
| 55 | return exchange; |
| 56 | } |
| 57 | |
| 58 | public String getName() { |
| 59 | return name; |
| 60 | } |
| 61 | |
| 62 | public BigDecimal getPrice() { |
| 63 | return price; |
| 64 | } |
| 65 | |
| 66 | public BigDecimal getDividendYield() { |
| 67 | return dividend; |
| 68 | } |
| 69 | |
| 70 | public static void main(String args[]) { |
| 71 | StockLookup lmt = new StockLookup("LMT"); |
| 72 | System.err.println("LMT: " + lmt.getName()); |
| 73 | StockLookup vwinx = new StockLookup("VWINX"); |
| 74 | System.err.println("VWINX: " + vwinx.getPrice()); |
| 75 | } |
| 76 | } |