From 02c0ef1c16b610058a6867ae49ba8ebaa9398560 Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Sat, 13 Jul 2013 15:25:16 -0700 Subject: [PATCH] Regex tweaks for Yahoo market data --- dj.go | 147 ++++++++++++++++++++++++++++++++++++++++++-- lib/format.go | 21 ++++++- lib/yahoo_market.go | 34 ++++++---- 3 files changed, 182 insertions(+), 20 deletions(-) diff --git a/dj.go b/dj.go index ad47ef0..0840def 100644 --- a/dj.go +++ b/dj.go @@ -147,6 +147,141 @@ func main() { LAST CHANGE +` + +html = ` +... + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SymbolLastChange
Dow15,464.30Up 3.38 (0.02%)
Nasdaq3,600.08 0.00 (0.00%)
S&P 5001,680.19Up 5.17 (0.31%)
10-Yr Bond2.60% + Up 0.03
NYSE Volume3,315,714,000.00
Nasdaq Volume...1,578,376,875.00
+
+Indices: US - World | Most Actives +
+ + +

Advances & Declines

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 NYSENASDAQ
Advances1,962 + (48%) + 1,322 + (53%) +
Declines2,029 + (49%) + 1,068 + (43%) +
Unchanged126 + (3%) + 115 + (5%) +
Up Vol*1,822 + (55%) + 918 + (58%) +
Down Vol*1,417 + (43%) + 636 + (40%) +
Unch. Vol*76 + (2%) + 24 + (2%) +
New Hi's355337
New Lo's11014
+
+
+*in millions
+ +
+
+
+

Most Actives

+
+ + + +... ` start := strings.Index(html, `
` const some = `<.+?` const space = `\s*` - const arrow = `"(Up|Down)">\s*` + const color = `#([08c]{6});">\s*` const price = `([\d\.,]+)` const percent = `\(([\d\.,%]+)\)` regex := []string{ - "(Dow)", any, price, some, arrow, any, price, some, percent, any, - "(Nasdaq)", any, price, some, arrow, any, price, some, percent, any, - "(S&P 500)", any, price, some, arrow, any, price, some, percent, any, - "(Advances)", any, price, space, percent, any, price, space, percent, any, + "(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, @@ -182,7 +317,7 @@ func main() { } else { println("No matches") } - +return m := Market{ Dow: make(map[string]string), Nasdaq: make(map[string]string), diff --git a/lib/format.go b/lib/format.go index 7c63e57..72a1561 100644 --- a/lib/format.go +++ b/lib/format.go @@ -13,9 +13,24 @@ import ( //----------------------------------------------------------------------------- func FormatMarket(m Market) string { - markup := `{{.Dow.name}}: {{.Dow.change}} ({{.Dow.percent}}) at {{.Dow.latest}}, ` - markup += `{{.Sp500.name}}: {{.Sp500.change}} ({{.Sp500.percent}}) at {{.Sp500.latest}}, ` - markup += `{{.Nasdaq.name}}: {{.Nasdaq.change}} ({{.Nasdaq.percent}}) at {{.Nasdaq.latest}}` + markup := `{{.Dow.name}}: ` + if m.Dow[`change`][0:1] != "-" { + markup += `{{.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 += `{{.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 += `{{.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` diff --git a/lib/yahoo_market.go b/lib/yahoo_market.go index 70b492c..2b67e04 100644 --- a/lib/yahoo_market.go +++ b/lib/yahoo_market.go @@ -52,14 +52,14 @@ func extract(snippet []byte) Market { const any = `\s*<.+?>` const some = `<.+?` const space = `\s*` - const arrow = `"(Up|Down)">\s*` + const color = `#([08c]{6});">\s*` const price = `([\d\.,]+)` const percent = `\(([\d\.,%]+)\)` regex := []string{ - "(Dow)", any, price, some, arrow, any, price, some, percent, any, - "(Nasdaq)", any, price, /*some, arrow,*/ any, price, some, percent, any, - "(S&P 500)", any, price, some, arrow, any, price, some, percent, any, + "(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, @@ -89,36 +89,48 @@ func extract(snippet []byte) Market { Highs: make(map[string]string), Lows: make(map[string]string), } - return m + m.Dow[`name`] = matches[0][1] m.Dow[`latest`] = matches[0][2] m.Dow[`change`] = matches[0][4] - if matches[0][3] == "Up" { + switch matches[0][3] { + case "008800": m.Dow[`change`] = "+" + matches[0][4] m.Dow[`percent`] = "+" + matches[0][5] - } else { + case "cc0000": m.Dow[`change`] = "-" + matches[0][4] m.Dow[`percent`] = "-" + matches[0][5] + default: + m.Dow[`change`] = matches[0][4] + m.Dow[`percent`] = matches[0][5] } m.Nasdaq[`name`] = matches[0][6] m.Nasdaq[`latest`] = matches[0][7] - if matches[0][8] == "Up" { + switch matches[0][8] { + case "008800": m.Nasdaq[`change`] = "+" + matches[0][9] m.Nasdaq[`percent`] = "+" + matches[0][10] - } else { + case "cc0000": m.Nasdaq[`change`] = "-" + matches[0][9] m.Nasdaq[`percent`] = "-" + matches[0][10] + default: + m.Nasdaq[`change`] = matches[0][9] + m.Nasdaq[`percent`] = matches[0][10] } m.Sp500[`name`] = matches[0][11] m.Sp500[`latest`] = matches[0][12] - if matches[0][13] == "Up" { + switch matches[0][13] { + case "008800": m.Sp500[`change`] = "+" + matches[0][14] m.Sp500[`percent`] = "+" + matches[0][15] - } else { + case "cc0000": m.Sp500[`change`] = "-" + matches[0][14] m.Sp500[`percent`] = "-" + matches[0][15] + default: + m.Sp500[`change`] = matches[0][14] + m.Sp500[`percent`] = matches[0][15] } m.Advances[`name`] = matches[0][16]