Refactored templates and parsing of the market data

master
Michael Dvorkin 11 years ago
parent 00a7ee406d
commit e5c017270d
  1. 44
      lib/layout.go
  2. 61
      lib/yahoo_market.go

@ -47,41 +47,18 @@ func (self *Layout) Initialize() *Layout {
//-----------------------------------------------------------------------------
func (self *Layout) Market(m *Market) string {
markup := `{{.Dow.name}}: `
if m.Dow[`change`][0:1] != `-` {
markup += `<green>{{.Dow.change}} ({{.Dow.percent}})</> at {{.Dow.latest}}, `
} else {
markup += `{{.Dow.change}} ({{.Dow.percent}}) at {{.Dow.latest}}, `
}
markup += `{{.Sp500.name}}: `
if m.Sp500[`change`][0:1] != `-` {
markup += `<green>{{.Sp500.change}} ({{.Sp500.percent}})</> at {{.Sp500.latest}}, `
} else {
markup += `{{.Sp500.change}} ({{.Sp500.percent}}) at {{.Sp500.latest}}, `
}
markup += `{{.Nasdaq.name}}: `
if m.Nasdaq[`change`][0:1] != `-` {
markup += `<green>{{.Nasdaq.change}} ({{.Nasdaq.percent}})</> at {{.Nasdaq.latest}}`
} else {
markup += `{{.Nasdaq.change}} ({{.Nasdaq.percent}}) at {{.Nasdaq.latest}}`
}
markup += "\n"
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`
if m.IsClosed {
markup += `<right>U.S. markets closed</right>`
}
markup += "\n"
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 := `{{.Dow.name}}: {{.Dow.change}} ({{.Dow.percent}}) at {{.Dow.latest}}, {{.Sp500.name}}: {{.Sp500.change}} ({{.Sp500.percent}}) at {{.Sp500.latest}}, {{.Nasdaq.name}}: {{.Nasdaq.change}} ({{.Nasdaq.percent}}) at {{.Nasdaq.latest}}
{{.Advances.name}}: {{.Advances.nyse}} ({{.Advances.nysep}}) on NYSE and {{.Advances.nasdaq}} ({{.Advances.nasdaqp}}) on Nasdaq. {{.Declines.name}}: {{.Declines.nyse}} ({{.Declines.nysep}}) on NYSE and {{.Declines.nasdaq}} ({{.Declines.nasdaqp}}) on Nasdaq {{if .IsClosed}}<right>U.S. markets closed</right>{{end}}
New highs: {{.Highs.nyse}} on NYSE and {{.Highs.nasdaq}} on Nasdaq. New lows: {{.Lows.nyse}} on NYSE and {{.Lows.nasdaq}} on Nasdaq.`
template, err := template.New(`market`).Parse(markup)
if err != nil {
panic(err)
}
buffer := new(bytes.Buffer)
err = template.Execute(buffer, m)
if err != nil {
highlight(m.Dow, m.Sp500, m.Nasdaq)
if err = template.Execute(buffer, m); err != nil {
panic(err)
}
@ -173,6 +150,15 @@ func (self *Layout) prettify(quotes *Quotes) []Stock {
return pretty
}
//-----------------------------------------------------------------------------
func highlight(collections ...map[string]string) {
for _, collection := range collections {
if collection[`change`][0:1] != `-` {
collection[`change`] = `<green>` + collection[`change`] + `</>`
}
}
}
//-----------------------------------------------------------------------------
func group(stocks []Stock) []Stock {
grouped := make([]Stock, len(stocks))

@ -11,15 +11,16 @@ import (
)
type Market struct {
IsClosed bool
Dow map[string]string
Nasdaq map[string]string
Sp500 map[string]string
Advances map[string]string
Declines map[string]string
Unchanged map[string]string
Highs map[string]string
Lows map[string]string
regex *regexp.Regexp
IsClosed bool
Dow map[string]string
Nasdaq map[string]string
Sp500 map[string]string
Advances map[string]string
Declines map[string]string
Unchanged map[string]string
Highs map[string]string
Lows map[string]string
}
//-----------------------------------------------------------------------------
@ -34,6 +35,26 @@ func (self *Market) Initialize() *Market {
self.Highs = make(map[string]string)
self.Lows = make(map[string]string)
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,
}
self.regex = regexp.MustCompile(strings.Join(rules, ``))
return self
}
@ -83,26 +104,7 @@ func (self *Market) trim(body []byte) []byte {
//-----------------------------------------------------------------------------
func (self *Market) extract(snippet []byte) *Market {
const any = `\s*<.+?>`
const some = `<.+?`
const space = `\s*`
const color = `#([08c]{6});">\s*`
const price = `([\d\.,]+)`
const percent = `\(([\d\.,%]+)\)`
regex := []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,
}
re := regexp.MustCompile(strings.Join(regex, ``))
matches := re.FindAllStringSubmatch(string(snippet), -1)
matches := self.regex.FindAllStringSubmatch(string(snippet), -1)
// if len(matches) > 0 {
// fmt.Printf("%d matches\n", len(matches[0]))
@ -113,7 +115,6 @@ func (self *Market) extract(snippet []byte) *Market {
// println(`No matches`)
// }
self.Dow[`name`] = matches[0][1]
self.Dow[`latest`] = matches[0][2]
self.Dow[`change`] = matches[0][4]

Loading…
Cancel
Save