Browse Source

修复可能引起抢购链接获取失败问题

main
ztino 4 years ago
parent
commit
fcd1306672
  1. 27
      jd_seckill/seckill.go
  2. 7
      jd_seckill/user.go
  3. 4
      main.go

27
jd_seckill/seckill.go

@ -79,7 +79,8 @@ func (this *Seckill) getSeckillUrl() (string, error) {
_, body, _ := req.Send().End()
//临时打印数据
log.Println("返回信息:"+body)
var cbBody string
//先注释,测试过gjson可以解析jQuery1153906({"type":"3","state":"13","url":""})格式
/* var cbBody string
cbBody = body
spBody := strings.Split(body, "(")
if len(spBody) >= 2 {
@ -89,6 +90,10 @@ func (this *Seckill) getSeckillUrl() (string, error) {
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() != "" {
url = gjson.Get(body, "url").String()
break
}
log.Println("抢购链接获取失败,稍后自动重试")
time.Sleep(300 * time.Millisecond)
@ -115,7 +120,12 @@ func (this *Seckill) RequestSeckillUrl() {
url, _ := this.getSeckillUrl()
skuId := this.conf.MustValue("config", "sku_id", "")
log.Println("访问商品的抢购连接...")
req := httpc.NewRequest(this.client)
client:=httpc.NewHttpClient()
client.SetCookieJar(common.CookieJar)
client.SetRedirect(func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
})
req := httpc.NewRequest(client)
req.SetHeader("User-Agent", this.getUserAgent())
req.SetHeader("Host", "marathon.jd.com")
req.SetHeader("Referer", fmt.Sprintf("https://item.jd.com/%s.html", skuId))
@ -125,8 +135,13 @@ func (this *Seckill) RequestSeckillUrl() {
func (this *Seckill) SeckillPage() {
log.Println("访问抢购订单结算页面...")
skuId := this.conf.MustValue("config", "sku_id", "")
seckillNum := this.conf.MustValue("config", "seckill_num", "")
req := httpc.NewRequest(this.client)
seckillNum := this.conf.MustValue("config", "seckill_num", "2")
client:=httpc.NewHttpClient()
client.SetCookieJar(common.CookieJar)
client.SetRedirect(func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
})
req := httpc.NewRequest(client)
req.SetHeader("User-Agent", this.getUserAgent())
req.SetHeader("Host", "marathon.jd.com")
req.SetHeader("Referer", fmt.Sprintf("https://item.jd.com/%s.html", skuId))
@ -136,7 +151,7 @@ func (this *Seckill) SeckillPage() {
func (this *Seckill) SeckillInitInfo() (string, error) {
log.Println("获取秒杀初始化信息...")
skuId := this.conf.MustValue("config", "sku_id", "")
seckillNum := this.conf.MustValue("config", "seckill_num", "")
seckillNum := this.conf.MustValue("config", "seckill_num", "2")
req := httpc.NewRequest(this.client)
req.SetHeader("User-Agent", this.getUserAgent())
req.SetHeader("Host", "marathon.jd.com")
@ -159,7 +174,7 @@ func (this *Seckill) SubmitSeckillOrder() bool {
eid := this.conf.MustValue("config", "eid", "")
fp := this.conf.MustValue("config", "fp", "")
skuId := this.conf.MustValue("config", "sku_id", "")
seckillNum := this.conf.MustValue("config", "seckill_num", "")
seckillNum := this.conf.MustValue("config", "seckill_num", "2")
paymentPwd := this.conf.MustValue("account", "payment_pwd", "")
initInfo, err := this.SeckillInitInfo()
if err != nil {

7
jd_seckill/user.go

@ -97,7 +97,12 @@ func (this *User) TicketInfo(ticket string) (string,error) {
}
func (this *User) RefreshStatus() error {
req:=httpc.NewRequest(this.client)
client:=httpc.NewHttpClient()
client.SetCookieJar(common.CookieJar)
client.SetRedirect(func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
})
req:=httpc.NewRequest(client)
req.SetHeader("User-Agent",this.getUserAgent())
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 {

4
main.go

@ -6,7 +6,6 @@ import (
"github.com/ztino/jd_seckill/cmd"
"github.com/ztino/jd_seckill/common"
"log"
"net/http"
"os"
"runtime"
)
@ -16,9 +15,6 @@ func init() {
common.Client=httpc.NewHttpClient()
common.CookieJar=httpc.NewCookieJar()
common.Client.SetCookieJar(common.CookieJar)
common.Client.SetRedirect(func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
})
//配置文件初始化
confFile:="./conf.ini"

Loading…
Cancel
Save