// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved. // Use of this source code is governed by a MIT-style license that can // be found in the LICENSE file. package mop import ( `bytes` `fmt` `io/ioutil` `net/http` `regexp` `strings` ) const marketURL = `http://finance.yahoo.com/marketupdate/overview` type Market struct { IsClosed bool // True when U.S. markets are closed. Dow map[string]string // Hash of Dow Jones indicators. Nasdaq map[string]string // Hash of NASDAQ indicators. Sp500 map[string]string // Hash of S&P 500 indicators. Advances map[string]string // Declines map[string]string // Unchanged map[string]string // Highs map[string]string // Lows map[string]string // regex *regexp.Regexp // errors string // } //----------------------------------------------------------------------------- func (market *Market) Initialize() *Market { market.IsClosed = false market.Dow = make(map[string]string) market.Nasdaq = make(map[string]string) market.Sp500 = make(map[string]string) market.Advances = make(map[string]string) market.Declines = make(map[string]string) market.Unchanged = make(map[string]string) market.Highs = make(map[string]string) market.Lows = make(map[string]string) market.errors = `` const any = `\s*<.+?>` const some = `<.+?` const space = `\s*` const color = `#([08c]{6});">\s*` const price = `([\d\.,]+)` const percent = `\(([\d\.,%]+)\)` rules := []string{ `(Dow)`, any, price, some, color, price, some, percent, any, `(Nasdaq)`, any, price, some, color, price, some, percent, any, `(S&P 500)`, any, price, some, color, price, some, percent, any, `(Advances)`, any, price, space, percent, any, price, space, percent, any, `(Declines)`, any, price, space, percent, any, price, space, percent, any, `(Unchanged)`, any, price, space, percent, any, price, space, percent, any, `(New Hi's)`, any, price, any, price, any, `(New Lo's)`, any, price, any, price, any, } market.regex = regexp.MustCompile(strings.Join(rules, ``)) return market } //----------------------------------------------------------------------------- func (market *Market) Fetch() (this *Market) { this = market // <-- This ensures we return correct market after recover() from panic() attack. defer func() { if err := recover(); err != nil { market.errors = fmt.Sprintf("Error fetching market data...\n%s", err) } }() response, err := http.Get(marketURL) if err != nil { panic(err) } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) if err != nil { panic(err) } body = market.checkIfMarketIsOpen(body) return market.extract(market.trim(body)) } //----------------------------------------------------------------------------- func (market *Market) Ok() (bool, string) { return market.errors == ``, market.errors } // private //----------------------------------------------------------------------------- func (market *Market) checkIfMarketIsOpen(body []byte) []byte { start := bytes.Index(body, []byte(`id="yfs_market_time"`)) finish := start + bytes.Index(body[start:], []byte(``)) snippet := body[start:finish] market.IsClosed = bytes.Contains(snippet, []byte(`closed`)) || bytes.Contains(snippet, []byte(`open in`)) return body[finish:] } //----------------------------------------------------------------------------- func (market *Market) trim(body []byte) []byte { start := bytes.Index(body, []byte(`