Browse Source

Merge branch 'main' into main

main
ztino 4 years ago
committed by GitHub
parent
commit
f49828c50c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 52
      cmd/seckill.go
  2. 47
      jd_seckill/seckill.go

52
cmd/seckill.go

@ -17,6 +17,7 @@ import (
func init() { func init() {
rootCmd.AddCommand(seckillCmd) rootCmd.AddCommand(seckillCmd)
seckillCmd.Flags().BoolP("run","r",false,"Run directly without waiting for the time to buy")
} }
var seckillCmd = &cobra.Command{ var seckillCmd = &cobra.Command{
@ -26,6 +27,8 @@ var seckillCmd = &cobra.Command{
} }
func startSeckill(cmd *cobra.Command, args []string) { func startSeckill(cmd *cobra.Command, args []string) {
//获取是否直接运行抢购
isRun,_:=cmd.Flags().GetBool("run")
session:=jd_seckill.NewSession(common.CookieJar) session:=jd_seckill.NewSession(common.CookieJar)
err:=session.CheckLoginStatus() err:=session.CheckLoginStatus()
if err!=nil { if err!=nil {
@ -34,34 +37,29 @@ func startSeckill(cmd *cobra.Command, args []string) {
//活跃用户会话,当会话失效自动退出程序 //活跃用户会话,当会话失效自动退出程序
user:=jd_seckill.NewUser(common.Client,common.Config) user:=jd_seckill.NewUser(common.Client,common.Config)
go KeepSession(user) go KeepSession(user)
//计算抢购时间 //直接运行抢购跳过等待抢购时间
startTime, err := common.Hour2Unix(common.Config.MustValue("config", "buy_time", "09:59:59")) if !isRun {
if err != nil { //计算抢购时间
log.Fatal("抢购开始时间初始化失败,请检查时间格式", err) nowLocalTime:=time.Now().UnixNano()/1e6
} jdTime,_:=GetJdTime()
if startTime.Unix() < time.Now().Unix() { buyDate:=common.Config.MustValue("config","buy_time","")
startTime = startTime.AddDate(0, 0, 1) loc, _ := time.LoadLocation("Local")
} t,_:=time.ParseInLocation("2006-01-02 15:04:05",buyDate,loc)
log.Println("抢购开始时间:", startTime) buyTime:=t.UnixNano()/1e6
diffTime:=nowLocalTime-jdTime
localTime := time.Now().UnixNano() / 1e6 log.Println(fmt.Sprintf("正在等待到达设定时间:%s,检测本地时间与京东服务器时间误差为【%d】毫秒",buyDate,diffTime))
log.Println("本地系统时间:", localTime) timerTime:=(buyTime+diffTime)-jdTime
jdTime, _ := GetJdTime() if timerTime<=0 {
log.Println("京东云端时间:", jdTime) log.Println("请设置抢购时间")
diffTime := localTime - jdTime os.Exit(0)
log.Println(fmt.Sprintf("本地时间与京东服务器时间误差为【%d】毫秒", diffTime)) }
//等待抢购
buyTime := startTime.UnixNano() / 1e6 time.Sleep(time.Duration(timerTime)*time.Millisecond)
timerTime := (buyTime + diffTime) - jdTime //开始抢购
if timerTime <= 0 { log.Println("时间到达,开始执行……")
log.Println("请设置抢购开始时间,或直接清空") }else{
os.Exit(0) log.Println("开始执行……")
} }
//等待抢购
log.Println("等待抢购中……")
time.Sleep(time.Duration(timerTime) * time.Millisecond)
//开始抢购
log.Println("时间到达,开始执行……")
seckill:=jd_seckill.NewSeckill(common.Client,common.Config) seckill:=jd_seckill.NewSeckill(common.Client,common.Config)
//开启抢购任务,第二个参数为开启几个协程 //开启抢购任务,第二个参数为开启几个协程
//怕封号的可以减少协程数量,相反抢到的成功率也减低了 //怕封号的可以减少协程数量,相反抢到的成功率也减低了

47
jd_seckill/seckill.go

@ -79,20 +79,6 @@ func (this *Seckill) getSeckillUrl() (string, error) {
url := "" url := ""
for { for {
_, body, _ := req.Send().End() _, body, _ := req.Send().End()
//临时打印数据
log.Println("返回信息:"+body)
//先注释,测试过gjson可以解析jQuery1153906({"type":"3","state":"13","url":""})格式
/* var cbBody string
cbBody = body
spBody := strings.Split(body, "(")
if len(spBody) >= 2 {
cbBody = strings.Trim(spBody[1], ")")
}
if gjson.Get(cbBody, "url").Exists() && gjson.Get(cbBody, "url").String() != "" {
url = gjson.Get(cbBody, "url").String()
break
}*/
if gjson.Get(body, "url").Exists() && gjson.Get(body, "url").String() != "" { if gjson.Get(body, "url").Exists() && gjson.Get(body, "url").String() != "" {
url = gjson.Get(body, "url").String() url = gjson.Get(body, "url").String()
break break
@ -160,16 +146,23 @@ func (this *Seckill) SeckillInitInfo() (string, error) {
req.SetData("sku", skuId) req.SetData("sku", skuId)
req.SetData("num", seckillNum) req.SetData("num", seckillNum)
req.SetData("isModifyAddress", "false") req.SetData("isModifyAddress", "false")
resp, body, err := req.SetUrl("https://marathon.jd.com/seckillnew/orderService/pc/init.action").SetMethod("post").Send().End() req.SetUrl("https://marathon.jd.com/seckillnew/orderService/pc/init.action").SetMethod("post")
if err != nil || resp.StatusCode != http.StatusOK { //尝试获取三次
log.Println("初始化秒杀信息失败") errorCount:=3
return "", errors.New("初始化秒杀信息失败") errorMsg:=""
} for errorCount > 0 {
if !gjson.Valid(body) { _, body, _ := req.Send().End()
log.Println("抢购失败,返回信息:" + body) if body!="null" && gjson.Valid(body) {
return "", errors.New("抢购失败,返回信息:" + body) log.Println("获取秒杀初始化信息成功")
return body,nil
}else{
log.Println("获取秒杀初始化信息失败,返回信息:"+body)
errorMsg=body
}
errorCount=errorCount-1
time.Sleep(300*time.Millisecond)
} }
return body, nil return "", errors.New(errorMsg)
} }
func (this *Seckill) SubmitSeckillOrder() bool { func (this *Seckill) SubmitSeckillOrder() bool {
@ -184,6 +177,10 @@ func (this *Seckill) SubmitSeckillOrder() bool {
return false return false
} }
address := gjson.Get(initInfo, "addressList").Array() address := gjson.Get(initInfo, "addressList").Array()
if !gjson.Get(initInfo, "addressList").Exists() || len(address)<1 {
log.Println("抢购失败,可能你还未设置默认收货地址")
return false
}
defaultAddress := address[0] defaultAddress := address[0]
isinvoiceInfo := gjson.Get(initInfo, "invoiceInfo").Exists() isinvoiceInfo := gjson.Get(initInfo, "invoiceInfo").Exists()
invoiceTitle := "-1" invoiceTitle := "-1"
@ -243,6 +240,10 @@ func (this *Seckill) SubmitSeckillOrder() bool {
_ = service.SendMessage(this.conf, "茅台抢购通知", "抢购失败,网络错误") _ = service.SendMessage(this.conf, "茅台抢购通知", "抢购失败,网络错误")
return false return false
} }
//临时打印数据
log.Println("返回信息:"+body)
if !gjson.Valid(body) { if !gjson.Valid(body) {
log.Println("抢购失败,返回信息:" + body) log.Println("抢购失败,返回信息:" + body)
_ = service.SendMessage(this.conf, "茅台抢购通知", "抢购失败,返回信息:"+body) _ = service.SendMessage(this.conf, "茅台抢购通知", "抢购失败,返回信息:"+body)

Loading…
Cancel
Save