|
|
|
@ -11,6 +11,7 @@ import ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type Market struct { |
|
|
|
|
regex *regexp.Regexp |
|
|
|
|
IsClosed bool |
|
|
|
|
Dow map[string]string |
|
|
|
|
Nasdaq 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] |
|
|
|
|