Merge pull request #26 from cuihantao/master

Fixed time zone name display (and market data pulling)
master
Brandon Lee Camilleri 5 years ago committed by GitHub
commit 7ed54edf57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 66
      cnn_market.go
  2. 5
      screen.go

@ -57,22 +57,23 @@ func NewMarket() *Market {
market.errors = `` market.errors = ``
const any = `\s*(?:.+?)` const any = `\s*(?:.+?)`
const price = `>([\d\.,]+)</span>` const change = `>([\+\-]?[\d\.,]+)<\/span>`
const price = `>([\d\.,]+)<\/span>`
const percent = `>([\+\-]?[\d\.,]+%?)<` const percent = `>([\+\-]?[\d\.,]+%?)<`
rules := []string{ rules := []string{
`>Dow<`, any, percent, any, price, any, percent, any, `>Dow<`, any, percent, any, price, any, change, any,
`>Nasdaq<`, any, percent, any, price, any, percent, any, `>Nasdaq<`, any, percent, any, price, any, change, any,
`">S&P<`, any, percent, any, price, any, percent, any, `">S&P<`, any, percent, any, price, any, change, any,
`>Nikkei 225<`, any, percent, any, price, any, percent, any, `>10\-year yield<`, any, price, any, percent, any,
`>Hang Seng<`, any, percent, any, price, any, percent, any,
`>FTSE 100<`, any, percent, any, price, any, percent, any,
`>DAX<`, any, percent, any, price, any, percent, any,
`>10-year yield<`, any, price, any, percent, any,
`>Oil<`, any, price, any, percent, any, `>Oil<`, any, price, any, percent, any,
`>Yen<`, any, price, any, percent, any, `>Yen<`, any, price, any, percent, any,
`>Euro<`, any, price, any, percent, any, `>Euro<`, any, price, any, percent, any,
`>Gold<`, any, price, any, percent, any, `>Gold<`, any, price, any, percent, any,
`>Nikkei 225<`, any, percent, any, price, any, change, any,
`>Hang Seng<`, any, percent, any, price, any, change, any,
`>FTSE 100<`, any, percent, any, price, any, change, any,
`>DAX<`, any, percent, any, price, any, change, any,
} }
market.regex = regexp.MustCompile(strings.Join(rules, ``)) market.regex = regexp.MustCompile(strings.Join(rules, ``))
@ -147,37 +148,38 @@ func (market *Market) extract(snippet []byte) *Market {
market.Sp500[`latest`] = matches[8] market.Sp500[`latest`] = matches[8]
market.Sp500[`percent`] = matches[9] market.Sp500[`percent`] = matches[9]
market.Tokyo[`change`] = matches[10]
market.Tokyo[`latest`] = matches[11]
market.Tokyo[`percent`] = matches[12]
market.HongKong[`change`] = matches[13] market.Yield[`name`] = `10-year Yield`
market.HongKong[`latest`] = matches[14] market.Yield[`latest`] = matches[10]
market.HongKong[`percent`] = matches[15] market.Yield[`change`] = matches[11]
market.London[`change`] = matches[16] market.Oil[`latest`] = matches[12]
market.London[`latest`] = matches[17] market.Oil[`change`] = matches[13]
market.London[`percent`] = matches[18]
market.Frankfurt[`change`] = matches[19] market.Yen[`latest`] = matches[14]
market.Frankfurt[`latest`] = matches[20] market.Yen[`change`] = matches[15]
market.Frankfurt[`percent`] = matches[21]
market.Yield[`name`] = `10-year Yield` market.Euro[`latest`] = matches[16]
market.Yield[`latest`] = matches[22] market.Euro[`change`] = matches[17]
market.Yield[`change`] = matches[23]
market.Gold[`latest`] = matches[18]
market.Gold[`change`] = matches[19]
market.Oil[`latest`] = matches[24] market.Tokyo[`change`] = matches[20]
market.Oil[`change`] = matches[25] market.Tokyo[`latest`] = matches[21]
market.Tokyo[`percent`] = matches[22]
market.Yen[`latest`] = matches[26] market.HongKong[`change`] = matches[23]
market.Yen[`change`] = matches[27] market.HongKong[`latest`] = matches[24]
market.HongKong[`percent`] = matches[25]
market.Euro[`latest`] = matches[28] market.London[`change`] = matches[26]
market.Euro[`change`] = matches[29] market.London[`latest`] = matches[27]
market.London[`percent`] = matches[28]
market.Gold[`latest`] = matches[30] market.Frankfurt[`change`] = matches[29]
market.Gold[`change`] = matches[31] market.Frankfurt[`latest`] = matches[30]
market.Frankfurt[`percent`] = matches[31]
return market return market
} }

@ -86,8 +86,9 @@ func (screen *Screen) ClearLine(x int, y int) *Screen {
// Draw accepts variable number of arguments and knows how to display the // Draw accepts variable number of arguments and knows how to display the
// market data, stock quotes, current time, and an arbitrary string. // market data, stock quotes, current time, and an arbitrary string.
func (screen *Screen) Draw(objects ...interface{}) *Screen { func (screen *Screen) Draw(objects ...interface{}) *Screen {
zonename, _ := time.Now().In(time.Local).Zone()
if screen.pausedAt != nil { if screen.pausedAt != nil {
defer screen.DrawLine(0, 0, `<right><r>`+screen.pausedAt.Format(`3:04:05pm PST`)+`</r></right>`) defer screen.DrawLine(0, 0, `<right><r>`+screen.pausedAt.Format(`3:04:05pm ` + zonename)+`</r></right>`)
} }
for _, ptr := range objects { for _, ptr := range objects {
switch ptr.(type) { switch ptr.(type) {
@ -98,7 +99,7 @@ func (screen *Screen) Draw(objects ...interface{}) *Screen {
object := ptr.(*Quotes) object := ptr.(*Quotes)
screen.draw(screen.layout.Quotes(object.Fetch())) screen.draw(screen.layout.Quotes(object.Fetch()))
case time.Time: case time.Time:
timestamp := ptr.(time.Time).Format(`3:04:05pm PST`) timestamp := ptr.(time.Time).Format(`3:04:05pm ` + zonename)
screen.DrawLine(0, 0, `<right>`+timestamp+`</right>`) screen.DrawLine(0, 0, `<right>`+timestamp+`</right>`)
default: default:
screen.draw(ptr.(string)) screen.draw(ptr.(string))

Loading…
Cancel
Save