func main() { c := conf.Load("../../app/app.conf") db.Init("app", c.GetConf("db").MapData()) db.Current().Log = log.NewEmpty() log.Level = log.LOG_ERR fmt.Println(dfcf.FillDzjy(gos.NowInLocation()), "finished") }
// FetchRzrqSumData 抓取数据 func FetchRzrqSumData(page int) ([]byte, error) { st := gos.NowInLocation().Unix() / 30 formt := "http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=FD&sty=%s&st=0&sr=1&p=%d&ps=50&js=var%%20ruOtumOo={pages:(pc),data:[(x)]}&rt=%d" url := fmt.Sprintf(formt, "SHSZHSSUM", page, st) body, err := wget.GetBody(url) if err != nil { return nil, gos.DoError(err) } return body[bytes.Index(body, []byte("[")) : len(body)-1], nil }
func StockQuarter() string { now := gos.NowInLocation() q2, _ := time.ParseInLocation("2006-01-02", fmt.Sprint(now.Year(), "-06-30"), now.Location()) q3, _ := time.ParseInLocation("2006-01-02", fmt.Sprint(now.Year(), "-09-30"), now.Location()) q4, _ := time.ParseInLocation("2006-01-02", fmt.Sprint(now.Year(), "-12-31"), now.Location()) if now.Unix() > q4.Unix() { return fmt.Sprint(now.Year(), "-12-31") } else if now.Unix() > q3.Unix() { return fmt.Sprint(now.Year(), "-09-30") } else if now.Unix() > q2.Unix() { return fmt.Sprint(now.Year(), "-06-30") } else { return fmt.Sprint(now.Year(), "-03-31") } }
func isStockCached(code string) bool { var isOk bool var v []*RzrqStockData if v, isOk = stockDataCached[code]; !isOk { return false } if len(v) == 0 { return false } t := v[0].Date df := "20060102" now := gos.NowInLocation() // 00:00 - 08:00 if now.Hour() < 8 { now = now.AddDate(0, 0, -1) } tStr := t.Format(df) nowStr := now.Format(df) appConf := gos.Configuration.GetConf("other") if util.InStringSlice(appConf.GetStringSlice("holiday"), nowStr) { return true } switch now.Weekday() { case time.Sunday: if now.AddDate(0, 0, -2).Format(df) == tStr { return true } case time.Monday: if now.AddDate(0, 0, -3).Format(df) == tStr { return true } default: if now.Format(df) == tStr { return true } } return false }
func isCached() bool { if len(sumdataCached) == 0 { return false } t := sumdataCached[0].Date df := "20060102" now := gos.NowInLocation() // 00:00 - 08:00 当作前一天看待 if now.Hour() < 8 { now = now.AddDate(0, 0, -1) } tStr := t.Format(df) nowStr := now.Format(df) appConf := gos.Configuration.GetConf("other") if util.InStringSlice(appConf.GetStringSlice("holiday"), nowStr) { return true } switch now.Weekday() { case time.Sunday: if now.AddDate(0, 0, -2).Format(df) == tStr { return true } case time.Monday: if now.AddDate(0, 0, -3).Format(df) == tStr { if sumdataCached[0].SMrzye < 0 { return false } return true } default: if nowStr == tStr { return true } } return false }
// FetchPingGuData 抓取行业评估数据 func FetchPingGuData(code string) (*PingGuStruct, error) { // http://stockData.stock.hexun.com/600028.shtml now := gos.NowInLocation() formt := "http://pinggu.stock.hexun.com/DataProvider/StockFinanceNewFlash.ashx?&code=%s&s=%d" body, err := wget.GetBody(fmt.Sprintf(formt, code, now.Unix())) if err != nil { return nil, gos.DoError(err) } v := &PingGuStruct{} err = xml.Unmarshal(body, v) if err != nil { return nil, gos.DoError(err) } quarter := util.StockQuarter() for _, item := range v.Content { item.Date = quarter switch item.Name { case "综合能力": item.Itype = 1 case "盈利能力": item.Itype = 2 case "偿债能力": item.Itype = 3 case "成长能力": item.Itype = 4 case "资产经营": item.Itype = 5 case "市场表现": item.Itype = 6 case "投资收益": item.Itype = 7 } } return v, nil }
// http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=SHT&sty=SHTTMYE&rt=0.6341747129336 // curl "http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=SHT&sty=SHTTMYE&rt=0.6341747129336" -H "Pragma: no-cache" -H "Accept-Encoding: gzip, deflate, sdch" -H "Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36" -H "Accept: */*" -H "Referer: http://data.eastmoney.com/bkzj/hgt.html" -H "X-Requested-With: ShockwaveFlash/18.0.0.232" -H "Cookie: HAList=f-0-000003-B"%"u80A1"%"u6307"%"u6570; pgv_pvi=3511567662; pgv_info=ssi=s7100762258" -H "Connection: keep-alive" -H "Cache-Control: no-cache" --compressed // RzrqSumData 抓取数据 func GetHgtAmount() ([]*HgtAmount, error) { r := rand.New(rand.NewSource(99)) formt := "http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=SHT&sty=SHTTMYE&rt=%v" body, err := wget.GetBody(fmt.Sprintf(formt, r.Float64())) if err != nil { return nil, gos.DoError(err) } now := gos.NowInLocation() arr := bytes.Split(body, []byte("\r\n")) var tmp [][]byte result := make([]*HgtAmount, len(arr)) var amountA float64 var amountB float64 var date time.Time for i, v := range arr { tmp = bytes.Split(v, gos.B_SEMICOLON) amountA, err = util.Str2Float64(string(tmp[1])) if err != nil { amountA = 0 } amountB, err = util.Str2Float64(string(tmp[2])) if err != nil { amountA = 0 } date, err = time.ParseInLocation("2006/1/2 15:04", fmt.Sprint(now.Format("2006/1/2"), " ", string(tmp[0])), gos.GetSite().Location) if err != nil { date = now } result[i] = &HgtAmount{ Date: date, AmountA: amountA, AmountB: amountB, } } return result, nil }
// alertAtHgtChanged // n range of minute func alertAtHgtChanged(n int, diff float64) error { now := gos.NowInLocation() switch now.Weekday() { case time.Sunday, time.Saturday: return nil } appConf := gos.Configuration.GetConf("other") if util.InStringSlice(appConf.GetStringSlice("holiday"), now.Format("20060102")) { return nil } //如果已经通知过,那么未来一段时间内不再通知 if countAlertAtHgtChanged > countAlertAtHgtChangedStep { isAlertAtHgtChanged = false } if isAlertAtHgtChanged && countAlertAtHgtChanged < countAlertAtHgtChangedStep { countAlertAtHgtChanged++ return nil } nowUnix := now.Unix() df := "2006-01-02 15:04" t := fmt.Sprintf("%04d-%02d-%02d", now.Year(), now.Month(), now.Day()) begin, err := time.ParseInLocation(df, fmt.Sprintf("%s %02d:%02d", t, 9, 0), gos.GetSite().Location) if err != nil { return err } beginUnix := begin.Unix() end, err := time.ParseInLocation(df, fmt.Sprintf("%s %02d:%02d", t, 15, 15), gos.GetSite().Location) if err != nil { return err } // gos.Log.Info("alertAtHgtChanged A", n, diff, now, begin, end) if nowUnix < beginUnix || nowUnix > end.Unix() { return nil } minute := int((nowUnix - beginUnix) / 60) // 排除中午时间 midA, _ := time.ParseInLocation(df, fmt.Sprintf("%s %02d:%02d", t, 12, 0), gos.GetSite().Location) midB, _ := time.ParseInLocation(df, fmt.Sprintf("%s %02d:%02d", t, 13, 0), gos.GetSite().Location) if nowUnix > midA.Unix() && nowUnix < midB.Unix() { return nil } else if nowUnix > midB.Unix() { minute -= 60 } if minute < n+2 { return nil } items, err := GetHgtAmount() if err != nil { return err } // gos.Log.Info("alertAtHgtChanged B", minute, n, len(items)) amountCurrent := items[minute].AmountA // 如果当前时间的金额==0,那么退一步取值 if amountCurrent == 0 { minute = minute - 1 amountCurrent = items[minute].AmountA } // 如果任然==0,判断是数据有误 if amountCurrent == 0 && minute > 10 { gos.Log.Info("amountCurrent==0 min=", minute, " ", items[minute], items[minute-1], items[minute-2], items[minute-3]) return nil } amountBefore := items[minute-n].AmountA if amountBefore == 0 && minute > 10 { gos.Log.Info("amountBefore==0 min=", minute, " ", items[minute], items[minute-1], items[minute-2], items[minute-3]) return nil } diffCurrent := amountCurrent - amountBefore // gos.Log.Info("alertAtHgtChanged C", "cur", amountCurrent, "bef", amountBefore, "Diff:", diffCurrent, items[minute].Date, items[minute-n].Date) // 如果幅度小于预期,则退出检查 if math.Abs(diffCurrent) < diff { return nil } conf := gos.Configuration.GetConf("mail") addr := conf.Get("addr") from := mail.Address{conf.Get("mail_user_name"), conf.Get("mail")} user := conf.Get("mail_user") password := conf.Get("mail_password") client := gos.NewSmtpPlainMail(addr, from, user, password) to := make([]*mail.Address, 1) to[0] = &mail.Address{"jiorry", "*****@*****.**"} var title string var body string if diffCurrent > 0 { title = fmt.Sprintf("沪港通资金异动 %.2f", diffCurrent) } else { title = fmt.Sprintf("沪港通资金异动 %.2f", diffCurrent) } for i := 0; i < n; i++ { body += fmt.Sprintln(items[minute-i].Date.Format("15:04"), " ", items[minute-i].AmountA) } body += fmt.Sprintf("资金变动:%.2f\n", diffCurrent) body += fmt.Sprintln("http://data.eastmoney.com/bkzj/hgt.html") err = client.Send(title, body, to) if err != nil { return err } gos.Log.Info("Send Email", t) isAlertAtHgtChanged = true countAlertAtHgtChanged = 0 return nil }