From fcd1306672b0c19268c066355c058228ea1731de Mon Sep 17 00:00:00 2001 From: ztino <> Date: Mon, 4 Jan 2021 22:05:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=AF=E8=83=BD=E5=BC=95?= =?UTF-8?q?=E8=B5=B7=E6=8A=A2=E8=B4=AD=E9=93=BE=E6=8E=A5=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jd_seckill/seckill.go | 27 +++++++++++++++++++++------ jd_seckill/user.go | 7 ++++++- main.go | 4 ---- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/jd_seckill/seckill.go b/jd_seckill/seckill.go index e493eff..8be8e04 100644 --- a/jd_seckill/seckill.go +++ b/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 { diff --git a/jd_seckill/user.go b/jd_seckill/user.go index 85fd987..2a86d06 100644 --- a/jd_seckill/user.go +++ b/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 { diff --git a/main.go b/main.go index 71a77e2..1dd52b9 100644 --- a/main.go +++ b/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"