Browse Source

修复可能因为错误未捕获导致程序异常退出

main
ztino 4 years ago
parent
commit
db1de86202
  1. 51
      jd_seckill/seckill.go
  2. 35
      jd_seckill/user.go

51
jd_seckill/seckill.go

@ -47,7 +47,7 @@ func (this *Seckill) MakeReserve() {
}
skuId:=this.conf.Read("config","sku_id")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Referer",fmt.Sprintf("https://item.jd.com/%s.html",skuId))
resp,body,err:=req.SetUrl("https://yushou.jd.com/youshouinfo.action?callback=fetchJSON&sku="+skuId+"&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -63,34 +63,31 @@ func (this *Seckill) MakeReserve() {
func (this *Seckill) getSeckillUrl() (string,error) {
skuId:=this.conf.Read("config","sku_id")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Host","itemko.jd.com")
req.SetHeader("Referer",fmt.Sprintf("https://item.jd.com/%s.html",skuId))
resp,body,err:=req.SetUrl("https://itemko.jd.com/itemShowBtn?callback=jQuery{}"+strconv.Itoa(common.Rand(1000000,9999999))+"&skuId="+skuId+"&from=pc&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
log.Println("抢购链接获取失败,稍后自动重试")
return "",errors.New("抢购链接获取失败,稍后自动重试")
}
url:=gjson.Get(body,"url").String()
if url=="" {
url:=""
for {
_,body,_:=req.SetUrl("https://itemko.jd.com/itemShowBtn?callback=jQuery{}"+strconv.Itoa(common.Rand(1000000,9999999))+"&skuId="+skuId+"&from=pc&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if gjson.Get(body,"url").Exists() && gjson.Get(body,"url").String()!="" {
url=gjson.Get(body,"url").String()
break
}
log.Println("抢购链接获取失败,稍后自动重试")
return "",errors.New("抢购链接获取失败,稍后自动重试")
time.Sleep(3*time.Millisecond)
}
//https://divide.jd.com/user_routing?skuId=8654289&sn=c3f4ececd8461f0e4d7267e96a91e0e0&from=pc
url=strings.ReplaceAll(url,"divide","marathon")
//https://marathon.jd.com/captcha.html?skuId=8654289&sn=c3f4ececd8461f0e4d7267e96a91e0e0&from=pc
url=strings.ReplaceAll(url,"user_routing","captcha.html")
log.Println("抢购链接获取成功:"+url)
return url,nil
}
func (this *Seckill) RequestSeckillUrl() {
user:=NewUser(this.client,this.conf)
userInfo,err:=user.GetUserInfo()
if err!=nil {
log.Println("获取用户信息失败")
}else{
log.Println("用户:"+userInfo)
}
userInfo,_:=user.GetUserInfo()
log.Println("用户:"+userInfo)
shopTitle,err:=this.SkuTitle()
if err!=nil {
log.Println("获取商品信息失败")
@ -100,7 +97,7 @@ func (this *Seckill) RequestSeckillUrl() {
url,_:=this.getSeckillUrl()
skuId:=this.conf.Read("config","sku_id")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Host","marathon.jd.com")
req.SetHeader("Referer",fmt.Sprintf("https://item.jd.com/%s.html",skuId))
_,_,_=req.SetUrl(url).SetMethod("get").Send().End()
@ -111,7 +108,7 @@ func (this *Seckill) SeckillPage() {
skuId:=this.conf.Read("config","sku_id")
seckillNum:=this.conf.Read("config","seckill_num")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Host","marathon.jd.com")
req.SetHeader("Referer",fmt.Sprintf("https://item.jd.com/%s.html",skuId))
_,_,_=req.SetUrl("https://marathon.jd.com/seckill/seckill.action?skuId="+skuId+"&num="+seckillNum+"&rid="+strconv.Itoa(int(time.Now().Unix()))).SetMethod("get").Send().End()
@ -122,7 +119,7 @@ func (this *Seckill) SeckillInitInfo() (string,error) {
skuId:=this.conf.Read("config","sku_id")
seckillNum:=this.conf.Read("config","seckill_num")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Host","marathon.jd.com")
req.SetData("sku",skuId)
req.SetData("num",seckillNum)
@ -132,6 +129,10 @@ func (this *Seckill) SeckillInitInfo() (string,error) {
log.Println("初始化秒杀信息失败")
return "",errors.New("初始化秒杀信息失败")
}
if !gjson.Valid(body) {
log.Println("抢购失败,返回信息:"+body)
return "", errors.New("抢购失败,返回信息:"+body)
}
return body,nil
}
@ -141,7 +142,11 @@ func (this *Seckill) SubmitSeckillOrder() bool {
skuId:=this.conf.Read("config","sku_id")
seckillNum:=this.conf.Read("config","seckill_num")
paymentPwd:=this.conf.Read("account","payment_pwd")
initInfo,_:=this.SeckillInitInfo()
initInfo,err:=this.SeckillInitInfo()
if err!=nil {
log.Println(fmt.Sprintf("抢购失败,无法获取生成订单的基本信息,接口返回:【%s】",err))
return false
}
address:=gjson.Get(initInfo,"addressList").Array()
defaultAddress:=address[0]
isinvoiceInfo:=gjson.Get(initInfo,"invoiceInfo").Exists()
@ -162,7 +167,7 @@ func (this *Seckill) SubmitSeckillOrder() bool {
token:=gjson.Get(initInfo,"token").String()
log.Println("提交抢购订单...")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Host","marathon.jd.com")
req.SetHeader("Referer",fmt.Sprintf("https://marathon.jd.com/seckill/seckill.action?skuId=%s&num=%s&rid=%d",skuId,seckillNum,int(time.Now().Unix())))
req.SetData("skuId",skuId)
@ -208,10 +213,10 @@ func (this *Seckill) SubmitSeckillOrder() bool {
return false
}
if !gjson.Valid(body) {
log.Println("抢购失败,返回信息:"+common.Substr(body,0,128))
log.Println("抢购失败,返回信息:"+body)
if this.conf.Read("messenger","enable")=="true" && this.conf.Read("messenger","type")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.Read("messenger","email")},"茅台抢购通知","抢购失败,返回信息:"+common.Substr(body,0,128))
_=email.SendMail([]string{this.conf.Read("messenger","email")},"茅台抢购通知","抢购失败,返回信息:"+body)
}
return false
}

35
jd_seckill/user.go

@ -25,7 +25,7 @@ func NewUser(client *httpc.HttpClient,conf *conf.Config) *User {
func (this *User) loginPage() {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Connection","keep-alive")
req.SetHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
_,_,_=req.SetUrl("https://passport.jd.com/new/login.aspx").SetMethod("get").Send().End()
@ -36,7 +36,7 @@ func (this *User) QrLogin() (string,error) {
this.loginPage()
//二维码登录
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Referer","https://passport.jd.com/new/login.aspx")
resp,err:=req.SetUrl("https://qr.m.jd.com/show?appid=133&size=300&t="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().EndFile("./","qr_code.png")
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -59,7 +59,7 @@ func (this *User) QrLogin() (string,error) {
func (this *User) QrcodeTicket(wlfstkSmdl string) (string,error) {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Referer","https://passport.jd.com/new/login.aspx")
resp,body,err:=req.SetUrl("https://qr.m.jd.com/check?appid=133&callback=jQuery"+strconv.Itoa(common.Rand(1000000,9999999))+"&token="+wlfstkSmdl+"&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -76,7 +76,7 @@ func (this *User) QrcodeTicket(wlfstkSmdl string) (string,error) {
func (this *User) TicketInfo(ticket string) (string,error) {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Referer","https://passport.jd.com/uc/login?ltype=logout")
resp,body,err:=req.SetUrl("https://passport.jd.com/uc/qrCodeTicketValidation?t="+ticket).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -94,7 +94,7 @@ func (this *User) TicketInfo(ticket string) (string,error) {
func (this *User) RefreshStatus() error {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
resp,_,err:=req.SetUrl("https://order.jd.com/center/list.action?rid="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if err==nil && resp.StatusCode==http.StatusOK {
return nil
@ -105,14 +105,23 @@ func (this *User) RefreshStatus() error {
func (this *User) GetUserInfo() (string,error) {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","DEFAULT_USER_AGENT"))
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("Referer","https://order.jd.com/center/list.action")
resp,body,err:=req.SetUrl("https://passport.jd.com/user/petName/getUserInfoForMiniJd.action?callback="+strconv.Itoa(common.Rand(1000000,9999999))+"&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
log.Println("获取用户信息失败")
return "",errors.New("获取用户信息失败")
}else{
b,_:=common.GbkToUtf8([]byte(gjson.Get(body,"nickName").String()))
return string(b), nil
errorCount:=5
nickName:=""
for {
if errorCount>0 {
_,body,_:=req.SetUrl("https://passport.jd.com/user/petName/getUserInfoForMiniJd.action?callback="+strconv.Itoa(common.Rand(1000000,9999999))+"&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if gjson.Get(body,"nickName").Exists() {
nickName=gjson.Get(body,"nickName").String()
break
}
errorCount=errorCount-1
time.Sleep(3*time.Millisecond)
}else{
break
}
}
b,_:=common.GbkToUtf8([]byte(nickName))
return string(b), nil
}
Loading…
Cancel
Save