From b783ba852494cc91368f03a88e0585b637441afb Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Tue, 5 Jan 2021 20:00:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=E6=8A=A2=E8=B4=AD?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=8C=E6=97=A0=E9=9C=80=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=8A=A2=E8=B4=AD=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +--- cmd/seckill.go | 34 ++++++++++++++++++++++------------ common/lib.go | 6 +++++- common/var.go | 9 ++++++--- conf.ini | 4 ++-- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1a09cca..79f70b6 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,6 @@ go get github.com/ztino/jd_seckill ## 待办 -- 日志目前还未输出到本地日志文件保存 -- 自动化抢购支持,无需设置抢购时间 - 跨平台桌面端支持,打算使用:https://github.com/therecipe/qt ## 使用 @@ -114,7 +112,7 @@ $ zbarimg qr_code.png > qrcode.txt && qrencode -r qrcode.txt -o - -t UTF8 # 解 ## 抢购流程/抢购结果 -- 程序开始抢购总时间为两分钟,不管有无抢购成功,都会停止,抢购详情请查阅日志和自己配置的第三方推送服务。 +- 程序开始抢购总时间为两分钟,不管有无抢购成功,都会停止,抢购详情请查阅日志和自己配置的第三方推送服务(目前支持:钉钉机器人、SMTP邮件、Server酱) - 第二天抢购需要修改抢购时间和重新开始抢购任务。 diff --git a/cmd/seckill.go b/cmd/seckill.go index 8464061..df5b740 100644 --- a/cmd/seckill.go +++ b/cmd/seckill.go @@ -35,21 +35,31 @@ func startSeckill(cmd *cobra.Command, args []string) { user:=jd_seckill.NewUser(common.Client,common.Config) go KeepSession(user) //计算抢购时间 - nowLocalTime:=time.Now().UnixNano()/1e6 - jdTime,_:=GetJdTime() - buyDate:=common.Config.MustValue("config","buy_time","") - loc, _ := time.LoadLocation("Local") - t,_:=time.ParseInLocation("2006-01-02 15:04:05",buyDate,loc) - buyTime:=t.UnixNano()/1e6 - diffTime:=nowLocalTime-jdTime - log.Println(fmt.Sprintf("正在等待到达设定时间:%s,检测本地时间与京东服务器时间误差为【%d】毫秒",buyDate,diffTime)) - timerTime:=(buyTime+diffTime)-jdTime - if timerTime<=0 { - log.Println("请设置抢购时间") + startTime, err := common.Hour2Unix(common.Config.MustValue("config", "buy_time", "09:59:59")) + if err != nil { + log.Fatal("抢购开始时间初始化失败,请检查时间格式", err) + } + if startTime.Unix() < time.Now().Unix() { + startTime = startTime.AddDate(0, 0, 1) + } + log.Println("抢购开始时间:", startTime) + + localTime := time.Now().UnixNano() / 1e6 + log.Println("本地系统时间:", localTime) + jdTime, _ := GetJdTime() + log.Println("京东云端时间:", jdTime) + diffTime := localTime - jdTime + log.Println(fmt.Sprintf("本地时间与京东服务器时间误差为【%d】毫秒", diffTime)) + + buyTime := startTime.UnixNano() / 1e6 + timerTime := (buyTime + diffTime) - jdTime + if timerTime <= 0 { + log.Println("请设置抢购开始时间,或直接清空") os.Exit(0) } //等待抢购 - time.Sleep(time.Duration(timerTime)*time.Millisecond) + log.Println("等待抢购中……") + time.Sleep(time.Duration(timerTime) * time.Millisecond) //开始抢购 log.Println("时间到达,开始执行……") seckill:=jd_seckill.NewSeckill(common.Client,common.Config) diff --git a/common/lib.go b/common/lib.go index 6a6a2d0..58aa2fe 100644 --- a/common/lib.go +++ b/common/lib.go @@ -108,4 +108,8 @@ func OpenImage(file string) { _=cmd.Start() } } -} \ No newline at end of file +} + +func Hour2Unix(hour string) (time.Time, error) { + return time.ParseInLocation(DateTimeFormatStr, time.Now().Format(DateFormatStr) + " " + hour, time.Local) +} diff --git a/common/var.go b/common/var.go index 023779c..69a776f 100644 --- a/common/var.go +++ b/common/var.go @@ -5,9 +5,12 @@ import ( "github.com/unknwon/goconfig" ) -const SoftName = "jd_seckill" - -const Version = "0.1.7" +const ( + SoftName = "jd_seckill" + Version = "0.1.7" + DateTimeFormatStr = "2006-01-02 15:04:05" + DateFormatStr = "2006-01-02" +) var Client *httpc.HttpClient diff --git a/conf.ini b/conf.ini index eba8fae..f9e2541 100644 --- a/conf.ini +++ b/conf.ini @@ -12,8 +12,8 @@ test_sku_id = 100016034372 sku_id = 100012043978 # 抢购数量 seckill_num = 2 -# 抢购开始时间设定 2021-01-01 09:59:59 -buy_time = 2021-01-01 09:59:59 +# 抢购开始时间设定,格式:09:59:59,如果小于当前时间,则表示明天这个时间点开始;大于当前时间,则为今天。 +buy_time = 09:59:59 # 抢购总时间,单位:分钟,默认两分钟 seckill_time = # 抢购任务数量,默认5个