|
|
@ -10,10 +10,10 @@ import ( |
|
|
|
`strings` |
|
|
|
`strings` |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// See http://www.gummy-stuff.org/Yahoo-data.htm
|
|
|
|
// See http://www.gummy-stuff.org/Yahoo-stocks.htm
|
|
|
|
// Also http://query.yahooapis.com/v1/public/yql
|
|
|
|
// Also http://query.yahooapis.com/v1/public/yql
|
|
|
|
// ?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in(%22ALU%22,%22AAPL%22)
|
|
|
|
// ?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in(%22ALU%22,%22AAPL%22)
|
|
|
|
// &env=http%3A%2F%2Fdatatables.org%2Falltables.env
|
|
|
|
// &env=http%3A%2F%2Fstockstables.org%2Falltables.env
|
|
|
|
// &format=json'
|
|
|
|
// &format=json'
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Current, Change, Open, High, Low, 52-W High, 52-W Low, Volume, AvgVolume, P/E, Yield, Market Cap.
|
|
|
|
// Current, Change, Open, High, Low, 52-W High, 52-W Low, Volume, AvgVolume, P/E, Yield, Market Cap.
|
|
|
@ -36,7 +36,7 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
const yahoo_quotes_url = `http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=,l1c6k2oghjkva2r2rdyj3j1` |
|
|
|
const yahoo_quotes_url = `http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=,l1c6k2oghjkva2r2rdyj3j1` |
|
|
|
|
|
|
|
|
|
|
|
type Quote struct { |
|
|
|
type Stock struct { |
|
|
|
Ticker string |
|
|
|
Ticker string |
|
|
|
LastTrade string |
|
|
|
LastTrade string |
|
|
|
Change string |
|
|
|
Change string |
|
|
@ -55,12 +55,26 @@ type Quote struct { |
|
|
|
MarketCap string |
|
|
|
MarketCap string |
|
|
|
MarketCapX string |
|
|
|
MarketCapX string |
|
|
|
} |
|
|
|
} |
|
|
|
type Quotes []Quote |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func GetQuotes(tickers string) Quotes { |
|
|
|
type Quotes struct { |
|
|
|
|
|
|
|
market *Market |
|
|
|
|
|
|
|
profile *Profile |
|
|
|
|
|
|
|
stocks []Stock |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *Quotes) Initialize(market *Market, profile *Profile) *Quotes { |
|
|
|
|
|
|
|
self.market = market |
|
|
|
|
|
|
|
self.profile = profile |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *Quotes) Fetch() *Quotes { |
|
|
|
|
|
|
|
|
|
|
|
// Format the URL and send the request.
|
|
|
|
// Format the URL and send the request.
|
|
|
|
url := fmt.Sprintf(yahoo_quotes_url, tickers) |
|
|
|
url := fmt.Sprintf(yahoo_quotes_url, self.profile.ListOfTickers()) |
|
|
|
response, err := http.Get(url) |
|
|
|
response, err := http.Get(url) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
panic(err) |
|
|
@ -73,51 +87,48 @@ func GetQuotes(tickers string) Quotes { |
|
|
|
panic(err) |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return parse(sanitize(body)) |
|
|
|
return self.parse(self.sanitize(body)) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (q *Quote) Color() string { |
|
|
|
|
|
|
|
if strings.Index(q.Change, "-") == -1 { |
|
|
|
|
|
|
|
return `</green><green>` |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return `` // `</red><red>`
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func sanitize(body []byte) []byte { |
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
return bytes.Replace(bytes.TrimSpace(body), []byte{'"'}, []byte{}, -1) |
|
|
|
func (self *Quotes) parse(body []byte) *Quotes { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func parse(body []byte) Quotes { |
|
|
|
|
|
|
|
lines := bytes.Split(body, []byte{'\n'}) |
|
|
|
lines := bytes.Split(body, []byte{'\n'}) |
|
|
|
quotes := make(Quotes, len(lines)) |
|
|
|
self.stocks = make([]Stock, len(lines)) |
|
|
|
|
|
|
|
|
|
|
|
for i, line := range lines { |
|
|
|
for i, line := range lines { |
|
|
|
// fmt.Printf("\n\n{%d} -> [%s]\n\n", i, string(line))
|
|
|
|
columns := bytes.Split(bytes.TrimSpace(line), []byte{','}) |
|
|
|
parse_line(line, "es[i]) |
|
|
|
self.stocks[i].Ticker = string(columns[0]) |
|
|
|
|
|
|
|
self.stocks[i].LastTrade = string(columns[1]) |
|
|
|
|
|
|
|
self.stocks[i].Change = string(columns[2]) |
|
|
|
|
|
|
|
self.stocks[i].ChangePercent = string(columns[3]) |
|
|
|
|
|
|
|
self.stocks[i].Open = string(columns[4]) |
|
|
|
|
|
|
|
self.stocks[i].Low = string(columns[5]) |
|
|
|
|
|
|
|
self.stocks[i].High = string(columns[6]) |
|
|
|
|
|
|
|
self.stocks[i].Low52 = string(columns[7]) |
|
|
|
|
|
|
|
self.stocks[i].High52 = string(columns[8]) |
|
|
|
|
|
|
|
self.stocks[i].Volume = string(columns[9]) |
|
|
|
|
|
|
|
self.stocks[i].AvgVolume = string(columns[10]) |
|
|
|
|
|
|
|
self.stocks[i].PeRatio = string(columns[11]) |
|
|
|
|
|
|
|
self.stocks[i].PeRatioX = string(columns[12]) |
|
|
|
|
|
|
|
self.stocks[i].Dividend = string(columns[13]) |
|
|
|
|
|
|
|
self.stocks[i].Yield = string(columns[14]) |
|
|
|
|
|
|
|
self.stocks[i].MarketCap = string(columns[15]) |
|
|
|
|
|
|
|
self.stocks[i].MarketCapX = string(columns[16]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return quotes |
|
|
|
return self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (self *Quotes) sanitize(body []byte) []byte { |
|
|
|
|
|
|
|
return bytes.Replace(bytes.TrimSpace(body), []byte{'"'}, []byte{}, -1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func parse_line(line []byte, quote *Quote) { |
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
columns := bytes.Split(bytes.TrimSpace(line), []byte{','}) |
|
|
|
func (stock *Stock) Color() string { |
|
|
|
|
|
|
|
if strings.Index(stock.Change, "-") == -1 { |
|
|
|
quote.Ticker = string(columns[0]) |
|
|
|
return `</green><green>` |
|
|
|
quote.LastTrade = string(columns[1]) |
|
|
|
} else { |
|
|
|
quote.Change = string(columns[2]) |
|
|
|
return `` // `</red><red>`
|
|
|
|
quote.ChangePercent = string(columns[3]) |
|
|
|
} |
|
|
|
quote.Open = string(columns[4]) |
|
|
|
|
|
|
|
quote.Low = string(columns[5]) |
|
|
|
|
|
|
|
quote.High = string(columns[6]) |
|
|
|
|
|
|
|
quote.Low52 = string(columns[7]) |
|
|
|
|
|
|
|
quote.High52 = string(columns[8]) |
|
|
|
|
|
|
|
quote.Volume = string(columns[9]) |
|
|
|
|
|
|
|
quote.AvgVolume = string(columns[10]) |
|
|
|
|
|
|
|
quote.PeRatio = string(columns[11]) |
|
|
|
|
|
|
|
quote.PeRatioX = string(columns[12]) |
|
|
|
|
|
|
|
quote.Dividend = string(columns[13]) |
|
|
|
|
|
|
|
quote.Yield = string(columns[14]) |
|
|
|
|
|
|
|
quote.MarketCap = string(columns[15]) |
|
|
|
|
|
|
|
quote.MarketCapX = string(columns[16]) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|