func realTimeUpdate(code []string, address []int, done chan error) { db, err := createDatabase() if err != nil { done <- err fmt.Println(err) return } defer db.Close() var stockStr string for i, value := range code { if address[i] == 0 { stockStr = "sz" + value } else { stockStr = "sh" + value } err := CreateStockDataBase(stockStr, db) if err != nil { done <- err fmt.Println(err) return } } fmt.Println("start real time update stock") dwMgr := download.Instance() errParseCount := 0 errUpdateCount := 0 for { re, err := isStockUpdateTime() if err != nil { errUpdateCount++ } if errUpdateCount > 100 { fmt.Println(err) errUpdateCount = 0 } if re && err == nil { for i := 0; i < len(code); i++ { localPath, err := dwMgr.Download(string("http://hq.sinajs.cn/list=") + string("sh") + code[i]) buf, err := ioutil.ReadFile(localPath) if err != nil { dwMgr.RemoveFile(localPath) } err = ParseStockByUrl(stockStr, string(buf), db) if err != nil { errParseCount++ } if errParseCount > 2000 { fmt.Println(err) errParseCount = 0 } dwMgr.RemoveFile(localPath) } } } }
//todo:闲下来的时候进行数据补齐 func (mgr *realTime) realTimeUpdate(cdata *cData) { if mgr.isStockUpdateTime() { dwMgr := download.Instance() localPath, err := dwMgr.Download( /*mgr.stockUrl +*/ string("sh") + "12") _, err = ioutil.ReadFile(localPath) if err != nil { //todo: retry self } //parse insert //addrecord self time } }
func loadStockMap() (map[string]stockInfo, error) { dwMgr := download.Instance() localPath, err := dwMgr.Download("http://quote.eastmoney.com/stocklist.html#sz") if err != nil { fmt.Println("load remote stock map error") return nil, err } remoteStockMap, err := getStockMapByParseFile(localPath) if err != nil { fmt.Println("parse remote stock map error") return nil, err } return remoteStockMap, nil }
func (mgr *stockMgr) loadStockMap() (map[string]stockInfo, error) { dwMgr := download.Instance() localPath, err := dwMgr.Download(mgr.stockMapUrl) if err != nil { fmt.Println("load remote stock map error") return nil, err } remoteStockMap, err := mgr.getStockMapByParseFile(localPath) if err != nil { fmt.Println("parse remote stock map error") return nil, err } return remoteStockMap, nil }
func isStockUpdateTime() (result bool, err error) { dwMgr := download.Instance() contextFunc := func() (text string, err error) { count := 0 for { //随便拿个股票测试 localPath, err := dwMgr.Download(string("http://hq.sinajs.cn/list=sh601006")) if count == 5000 { return string(""), Error("error") } if err != nil { count++ continue } buf, err := ioutil.ReadFile(localPath) if err != nil { dwMgr.RemoveFile(localPath) return string(""), Error("error") } dwMgr.RemoveFile(localPath) return string(buf), nil } return string(""), nil } originText, err := contextFunc() if err != nil || len(originText) == 0 { return false, Error("err") } time.Sleep(time.Second * 60 * 60) text, err := contextFunc() if err != nil || len(originText) == 0 { //send email return false, Error("err") } if strings.EqualFold(originText, text) { return true, nil } return false, nil }