Don't bother fetching quotes if the market is closed

master
Michael Dvorkin 12 years ago
parent c7e2456ced
commit 233e5122d6
  1. 3
      lib/format.go
  2. 10
      lib/line_editor.go
  3. 14
      lib/profile.go
  4. 11
      lib/yahoo_market.go
  5. 47
      lib/yahoo_quotes.go

@ -47,6 +47,9 @@ func (self *Formatter) format_market(m *Market) string {
markup += "\n" markup += "\n"
markup += `{{.Advances.name}}: {{.Advances.nyse}} ({{.Advances.nysep}}) on NYSE and {{.Advances.nasdaq}} ({{.Advances.nasdaqp}}) on Nasdaq. ` markup += `{{.Advances.name}}: {{.Advances.nyse}} ({{.Advances.nysep}}) on NYSE and {{.Advances.nasdaq}} ({{.Advances.nasdaqp}}) on Nasdaq. `
markup += `{{.Declines.name}}: {{.Declines.nyse}} ({{.Declines.nysep}}) on NYSE and {{.Declines.nasdaq}} ({{.Declines.nasdaqp}}) on Nasdaq` markup += `{{.Declines.name}}: {{.Declines.nyse}} ({{.Declines.nysep}}) on NYSE and {{.Declines.nasdaq}} ({{.Declines.nasdaqp}}) on Nasdaq`
if !m.Open {
markup += `<right>U.S. markets closed</right>`
}
markup += "\n" markup += "\n"
markup += `New highs: {{.Highs.nyse}} on NYSE and {{.Highs.nasdaq}} on Nasdaq. ` markup += `New highs: {{.Highs.nyse}} on NYSE and {{.Highs.nasdaq}} on Nasdaq. `
markup += `New lows: {{.Lows.nyse}} on NYSE and {{.Lows.nasdaq}} on Nasdaq.` markup += `New lows: {{.Lows.nyse}} on NYSE and {{.Lows.nasdaq}} on Nasdaq.`

@ -147,17 +147,17 @@ func (self *LineEditor) execute() {
case '+': case '+':
tickers := self.tokenize() tickers := self.tokenize()
if len(tickers) > 0 { if len(tickers) > 0 {
self.quotes.profile.AddTickers(tickers) if added,_ := self.quotes.AddTickers(tickers); added > 0 {
self.screen.Draw(self.quotes) self.screen.Draw(self.quotes)
}
} }
case '-': case '-':
tickers := self.tokenize() tickers := self.tokenize()
if len(tickers) > 0 { if len(tickers) > 0 {
before := len(self.quotes.profile.Tickers) before := len(self.quotes.profile.Tickers)
self.quotes.profile.RemoveTickers(tickers) if removed,_ := self.quotes.RemoveTickers(tickers); removed > 0 {
after := len(self.quotes.profile.Tickers)
if after < before {
self.screen.Draw(self.quotes) self.screen.Draw(self.quotes)
after := before - removed
for i := before; i > after; i-- { for i := before; i > after; i-- {
self.screen.ClearLine(0, i + 4) self.screen.ClearLine(0, i + 4)
} }

@ -52,7 +52,8 @@ func (self *Profile) ListOfTickers() string {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Profile) AddTickers(tickers []string) { func (self *Profile) AddTickers(tickers []string) (added int, err error) {
added = 0
existing := make(map[string]bool) existing := make(map[string]bool)
for _, ticker := range self.Tickers { for _, ticker := range self.Tickers {
@ -62,22 +63,27 @@ func (self *Profile) AddTickers(tickers []string) {
for _, ticker := range tickers { for _, ticker := range tickers {
if _, found := existing[ticker]; !found { if _, found := existing[ticker]; !found {
self.Tickers = append(self.Tickers, ticker) self.Tickers = append(self.Tickers, ticker)
added++
} }
} }
sort.Strings(self.Tickers) sort.Strings(self.Tickers)
self.Save() err = self.Save()
return
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Profile) RemoveTickers(tickers []string) { func (self *Profile) RemoveTickers(tickers []string) (removed int, err error) {
removed = 0
for _, ticker := range tickers { for _, ticker := range tickers {
for i, existing := range self.Tickers { for i, existing := range self.Tickers {
if ticker == existing { // Requested ticker is there: remove i-th slice item. if ticker == existing { // Requested ticker is there: remove i-th slice item.
self.Tickers = append(self.Tickers[:i], self.Tickers[i+1:]...) self.Tickers = append(self.Tickers[:i], self.Tickers[i+1:]...)
removed++
} }
} }
} }
self.Save() err = self.Save()
return
} }
// private // private

@ -51,6 +51,7 @@ func (self *Market) Fetch() *Market {
panic(err) panic(err)
} }
body = self.check_if_market_open(body)
return self.extract(self.trim(body)) return self.extract(self.trim(body))
} }
@ -59,6 +60,16 @@ func (self *Market) Format() string {
return new(Formatter).Format(self) return new(Formatter).Format(self)
} }
// private
//-----------------------------------------------------------------------------
func (self *Market) check_if_market_open(body []byte) []byte {
start := bytes.Index(body, []byte(`id="yfs_market_time"`))
finish := start + bytes.Index(body[start:], []byte(`</span>`))
self.Open = !bytes.Contains(body[start:finish], []byte(`closed`))
return body[finish:]
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Market) trim(body []byte) []byte { func (self *Market) trim(body []byte) []byte {
start := bytes.Index(body, []byte(`<table id="yfimktsumm"`)) start := bytes.Index(body, []byte(`<table id="yfimktsumm"`))

@ -72,22 +72,25 @@ func (self *Quotes) Initialize(market *Market, profile *Profile) *Quotes {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Quotes) Fetch() *Quotes { func (self *Quotes) Fetch() *Quotes {
if self.market.Open || self.stocks == nil {
// Format the URL and send the request. // Format the URL and send the request.
url := fmt.Sprintf(yahoo_quotes_url, self.profile.ListOfTickers()) 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)
} }
// Fetch response and get its body. // Fetch response and get its body.
defer response.Body.Close() defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body) body, err := ioutil.ReadAll(response.Body)
if err != nil { if err != nil {
panic(err) panic(err)
}
self.parse(self.sanitize(body))
} }
return self.parse(self.sanitize(body)) return self
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -95,6 +98,22 @@ func (self *Quotes) Format() string {
return new(Formatter).Format(self) return new(Formatter).Format(self)
} }
//-----------------------------------------------------------------------------
func (self *Quotes) AddTickers(tickers []string) (added int, err error) {
if added, err = self.profile.AddTickers(tickers); added > 0 {
self.stocks = nil // Force fetch.
}
return
}
//-----------------------------------------------------------------------------
func (self *Quotes) RemoveTickers(tickers []string) (removed int, err error) {
if removed, err = self.profile.RemoveTickers(tickers); removed > 0 {
self.stocks = nil // Force fetch.
}
return
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
func (self *Quotes) parse(body []byte) *Quotes { func (self *Quotes) parse(body []byte) *Quotes {
lines := bytes.Split(body, []byte{'\n'}) lines := bytes.Split(body, []byte{'\n'})

Loading…
Cancel
Save