Faster market data parsing

master
Michael Dvorkin 11 years ago
parent 97439691cb
commit b3e5ebfdbe
  1. 100
      yahoo_market.go

@ -13,7 +13,7 @@ import (
`strings`
)
const marketURL = `http://finance.yahoo.com/` // `http://finance.yahoo.com/marketupdate/overview`
const marketURL = `http://finance.yahoo.com`
// Market stores current market information displayed in the top three lines of
// the screen. The market data is fetched and parsed from the HTML page above.
@ -55,7 +55,7 @@ func (market *Market) Initialize() *Market {
rules := []string{
`S&P 500</span>`, any, price, color, price, any, percent, any,
`Dow</span>`, any, price, color, price, any, percent, any,
`NASDAQ</span>`, any, price, color, price, any, percent, any,
`Nasdaq</span>`, any, price, color, price, any, percent, any,
`FTSE</span>`, any, price, color, price, any, percent, any,
`DAX</span>`, any, price, color, price, any, percent, any,
`CAC 40</span>`, any, price, color, price, any, percent, any,
@ -122,116 +122,116 @@ func (market *Market) trim(body []byte) []byte {
//-----------------------------------------------------------------------------
func (market *Market) extract(snippet []byte) *Market {
matches := market.regex.FindAllStringSubmatch(string(snippet), -1)
//fmt.Printf("\n\n\n%q\n\n\n", matches[0])
if len(matches) < 1 || len(matches[0]) < 37 {
matches := market.regex.FindStringSubmatch(string(snippet))
// fmt.Printf("\n\n\n%q\n\n\n", matches)
if len(matches) < 37 {
panic(`Unable to parse ` + marketURL)
}
market.Sp500[`name`] = `S&P 500`
market.Sp500[`latest`] = matches[0][1]
market.Sp500[`change`] = matches[0][3]
market.Sp500[`percent`] = matches[0][4]
if matches[0][2] == `green` {
market.Sp500[`latest`] = matches[1]
market.Sp500[`change`] = matches[3]
market.Sp500[`percent`] = matches[4]
if matches[2] == `green` {
market.Sp500[`change`] = `+` + market.Sp500[`change`]
market.Sp500[`percent`] = `+` + market.Sp500[`percent`]
} else if matches[0][2] == `red` {
} else if matches[2] == `red` {
market.Sp500[`change`] = `-` + market.Sp500[`change`]
market.Sp500[`percent`] = `-` + market.Sp500[`percent`]
}
market.Dow[`name`] = `Dow`
market.Dow[`latest`] = matches[0][5]
market.Dow[`change`] = matches[0][7]
market.Dow[`percent`] = matches[0][8]
if matches[0][6] == `green` {
market.Dow[`latest`] = matches[5]
market.Dow[`change`] = matches[7]
market.Dow[`percent`] = matches[8]
if matches[6] == `green` {
market.Dow[`change`] = `+` + market.Dow[`change`]
market.Dow[`percent`] = `+` + market.Dow[`percent`]
} else if matches[0][6] == `red` {
} else if matches[6] == `red` {
market.Dow[`change`] = `-` + market.Dow[`change`]
market.Dow[`percent`] = `-` + market.Dow[`percent`]
}
market.Nasdaq[`name`] = `NASDAQ`
market.Nasdaq[`latest`] = matches[0][9]
market.Nasdaq[`change`] = matches[0][11]
market.Nasdaq[`percent`] = matches[0][12]
if matches[0][10] == `green` {
market.Nasdaq[`latest`] = matches[9]
market.Nasdaq[`change`] = matches[11]
market.Nasdaq[`percent`] = matches[12]
if matches[10] == `green` {
market.Nasdaq[`change`] = `+` + market.Nasdaq[`change`]
market.Nasdaq[`percent`] = `+` + market.Nasdaq[`percent`]
} else if matches[0][10] == `red` {
} else if matches[10] == `red` {
market.Nasdaq[`change`] = `-` + market.Nasdaq[`change`]
market.Nasdaq[`percent`] = `-` + market.Nasdaq[`percent`]
}
market.London[`name`] = `London`
market.London[`latest`] = matches[0][13]
market.London[`change`] = matches[0][15]
market.London[`percent`] = matches[0][16]
if matches[0][14] == `green` {
market.London[`latest`] = matches[13]
market.London[`change`] = matches[15]
market.London[`percent`] = matches[16]
if matches[14] == `green` {
market.London[`change`] = `+` + market.London[`change`]
market.London[`percent`] = `+` + market.London[`percent`]
} else if matches[0][14] == `red` {
} else if matches[14] == `red` {
market.London[`change`] = `-` + market.London[`change`]
market.London[`percent`] = `-` + market.London[`percent`]
}
market.Frankfurt[`name`] = `Frankfurt`
market.Frankfurt[`latest`] = matches[0][17]
market.Frankfurt[`change`] = matches[0][19]
market.Frankfurt[`percent`] = matches[0][20]
if matches[0][18] == `green` {
market.Frankfurt[`latest`] = matches[17]
market.Frankfurt[`change`] = matches[19]
market.Frankfurt[`percent`] = matches[20]
if matches[18] == `green` {
market.Frankfurt[`change`] = `+` + market.Frankfurt[`change`]
market.Frankfurt[`percent`] = `+` + market.Frankfurt[`percent`]
} else if matches[0][18] == `red` {
} else if matches[18] == `red` {
market.Frankfurt[`change`] = `-` + market.Frankfurt[`change`]
market.Frankfurt[`percent`] = `-` + market.Frankfurt[`percent`]
}
market.Paris[`name`] = `Paris`
market.Paris[`latest`] = matches[0][21]
market.Paris[`change`] = matches[0][23]
market.Paris[`percent`] = matches[0][24]
if matches[0][22] == `green` {
market.Paris[`latest`] = matches[21]
market.Paris[`change`] = matches[23]
market.Paris[`percent`] = matches[24]
if matches[22] == `green` {
market.Paris[`change`] = `+` + market.Paris[`change`]
market.Paris[`percent`] = `+` + market.Paris[`percent`]
} else if matches[0][22] == `red` {
} else if matches[22] == `red` {
market.Paris[`change`] = `-` + market.Paris[`change`]
market.Paris[`percent`] = `-` + market.Paris[`percent`]
}
market.Tokyo[`name`] = `Tokyo`
market.Tokyo[`latest`] = matches[0][25]
market.Tokyo[`change`] = matches[0][27]
market.Tokyo[`percent`] = matches[0][28]
if matches[0][26] == `green` {
market.Tokyo[`latest`] = matches[25]
market.Tokyo[`change`] = matches[27]
market.Tokyo[`percent`] = matches[28]
if matches[26] == `green` {
market.Tokyo[`change`] = `+` + market.Tokyo[`change`]
market.Tokyo[`percent`] = `+` + market.Tokyo[`percent`]
} else if matches[0][26] == `red` {
} else if matches[26] == `red` {
market.Tokyo[`change`] = `-` + market.Tokyo[`change`]
market.Tokyo[`percent`] = `-` + market.Tokyo[`percent`]
}
market.HongKong[`name`] = `Hong Kong`
market.HongKong[`latest`] = matches[0][29]
market.HongKong[`change`] = matches[0][31]
market.HongKong[`percent`] = matches[0][32]
if matches[0][30] == `green` {
market.HongKong[`latest`] = matches[29]
market.HongKong[`change`] = matches[31]
market.HongKong[`percent`] = matches[32]
if matches[30] == `green` {
market.HongKong[`change`] = `+` + market.HongKong[`change`]
market.HongKong[`percent`] = `+` + market.HongKong[`percent`]
} else if matches[0][30] == `red` {
} else if matches[30] == `red` {
market.HongKong[`change`] = `-` + market.HongKong[`change`]
market.HongKong[`percent`] = `-` + market.HongKong[`percent`]
}
market.Shanghai[`name`] = `Shanghai`
market.Shanghai[`latest`] = matches[0][33]
market.Shanghai[`change`] = matches[0][35]
market.Shanghai[`percent`] = matches[0][36]
if matches[0][34] == `green` {
market.Shanghai[`latest`] = matches[33]
market.Shanghai[`change`] = matches[35]
market.Shanghai[`percent`] = matches[36]
if matches[34] == `green` {
market.Shanghai[`change`] = `+` + market.Shanghai[`change`]
market.Shanghai[`percent`] = `+` + market.Shanghai[`percent`]
} else if matches[0][34] == `red` {
} else if matches[34] == `red` {
market.Shanghai[`change`] = `-` + market.Shanghai[`change`]
market.Shanghai[`percent`] = `-` + market.Shanghai[`percent`]
}

Loading…
Cancel
Save