Esempio n. 1
0
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
}
Esempio n. 2
0
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
}
Esempio n. 3
0
File: hgt.go Progetto: jiorry/gotock
// 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
}