9
9
"github.com/chromedp/cdproto/dom"
10
10
"github.com/chromedp/cdproto/network"
11
11
"github.com/chromedp/cdproto/page"
12
- "github.com/chromedp/cdproto/target"
13
12
"github.com/chromedp/chromedp"
14
13
"github.com/tidwall/gjson"
15
14
"github.com/zqijzqj/mtSecKill/chromedpEngine"
@@ -31,7 +30,6 @@ type jdSecKill struct {
31
30
ctx context.Context
32
31
cancel context.CancelFunc
33
32
bCtx context.Context
34
- bWorksCtx []context.Context
35
33
isLogin bool
36
34
isClose bool
37
35
mu sync.Mutex
@@ -111,6 +109,9 @@ func (jsk *jdSecKill) GetReq(reqUrl string, params map[string]string, referer st
111
109
if err != nil {
112
110
return gjson.Result {}, err
113
111
}
112
+ if resp .StatusCode != 200 {
113
+ logs .PrintlnWarning ("httpCode: " , resp .StatusCode , "reqUrl: " , resp .Request .URL )
114
+ }
114
115
//设置cookie到浏览器
115
116
for _ , respCookie := range resp .Cookies () {
116
117
ok , err := network .SetCookie (respCookie .Name , respCookie .Value ).WithURL (resp .Request .URL .String ()).Do (ctx )
@@ -155,6 +156,10 @@ func (jsk *jdSecKill) PostReq(reqUrl string, params url.Values, referer string,
155
156
if err != nil {
156
157
return gjson.Result {}, err
157
158
}
159
+
160
+ if resp .StatusCode != 200 {
161
+ logs .PrintlnWarning ("httpCode: " , resp .StatusCode , "reqUrl: " , resp .Request .URL )
162
+ }
158
163
//设置cookie到浏览器
159
164
for _ , respCookie := range resp .Cookies () {
160
165
_ , _ = network .SetCookie (respCookie .Name , respCookie .Value ).WithURL (resp .Request .URL .String ()).Do (ctx )
@@ -244,28 +249,26 @@ func (jsk *jdSecKill) Run() error {
244
249
jsk .GetEidAndFp (),
245
250
jsk .WaitStart (),
246
251
chromedp .ActionFunc (func (ctx context.Context ) error {
247
- //提取抢购连接
248
- for _ , v := range jsk .bWorksCtx {
249
- go func (ctx2 context.Context ) {
252
+ for i := 0 ; i < jsk .Works ; i ++ {
253
+ go func () {
250
254
for {
251
255
jsk .FetchSecKillUrl ()
252
256
logs .PrintlnInfo ("正在访问抢购连接......" )
253
- _ , err := jsk .GetReq (jsk .SecKillUrl , nil , "https://item.jd.com/" + jsk .SkuId + ".html" , ctx2 , true )
257
+ _ , err := jsk .GetReq (jsk .SecKillUrl , nil , "https://item.jd.com/" + jsk .SkuId + ".html" , jsk .bCtx , true )
258
+ //这里访问会响应302 禁止重定向后就会是空数据 所以这里空数据是正常的
254
259
if err == nil || err .Error () == ErrEmptyData .Error () {
255
260
break
256
261
}
257
- logs .PrintErr ("抢购连接访问错误,正在重试:" , err )
258
262
}
259
- //_, _, _, _ = page.Navigate(jsk.SecKillUrl).WithReferrer("https://item.jd.com/"+jsk.SkuId+".html").Do(ctx2)
260
- SecKillRE:
263
+ SecKillRE:
261
264
//请求抢购连接,提交订单
262
- err := jsk .ReqSubmitSecKillOrder (ctx2 )
265
+ err := jsk .ReqSubmitSecKillOrder (jsk . bCtx )
263
266
if err != nil {
264
267
logs .PrintlnInfo (err , "等待重试" )
265
268
goto SecKillRE
266
269
}
267
- _ = chromedp .Navigate ("https://order.jd.com/center/list.action" ).Do (ctx2 )
268
- }(v )
270
+ _ = chromedp .Navigate ("https://order.jd.com/center/list.action" ).Do (jsk . bCtx )
271
+ }()
269
272
}
270
273
select {
271
274
case <- jsk .IsOkChan :
@@ -282,23 +285,6 @@ func (jsk *jdSecKill) Run() error {
282
285
func (jsk * jdSecKill ) WaitStart () chromedp.ActionFunc {
283
286
return func (ctx context.Context ) error {
284
287
u := "https://item.jd.com/" + jsk .SkuId + ".html"
285
- for i := 0 ; i < jsk .Works ; i ++ {
286
- go func () {
287
- tid , err := target .CreateTarget (u ).Do (ctx )
288
- if err == nil {
289
- c , _ := chromedp .NewContext (jsk .bCtx , chromedp .WithTargetID (tid ))
290
- _ = chromedp .Run (c , chromedp.Tasks {
291
- chromedp .ActionFunc (func (ctx context.Context ) error {
292
- logs .PrintlnInfo ("打开新的抢购标签....." )
293
- jsk .mu .Lock ()
294
- jsk .bWorksCtx = append (jsk .bWorksCtx , ctx )
295
- jsk .mu .Unlock ()
296
- return nil
297
- }),
298
- })
299
- }
300
- }()
301
- }
302
288
_ = chromedp .Navigate (u ).Do (ctx )
303
289
st := jsk .StartTime .UnixNano () / 1e6
304
290
logs .PrintlnInfo ("等待时间到达" + jsk .StartTime .Format (global .DateTimeFormatStr ) + "...... 请勿关闭浏览器" )
@@ -404,7 +390,7 @@ func (jsk *jdSecKill) ReqSubmitSecKillOrder(ctx context.Context) error {
404
390
}
405
391
406
392
//这里修改为直接使用http请求访问抢购结算页面 提高速度
407
- skUrl := fmt .Sprintf ("https://marathon.jd.com/seckill/seckill.action?= skuId=%s&num=%d&rid=%d" , jsk .SkuId , jsk .SecKillNum , time .Now ().Unix ())
393
+ skUrl := fmt .Sprintf ("https://marathon.jd.com/seckill/seckill.action?skuId=%s&num=%d&rid=%d" , jsk .SkuId , jsk .SecKillNum , time .Now ().Unix ())
408
394
logs .PrintlnInfo ("访问抢购订单结算页面......" , skUrl )
409
395
_ , _ = jsk .GetReq (skUrl , nil , "https://item.jd.com/" + jsk .SkuId + ".html" , ctx , true )
410
396
@@ -547,9 +533,6 @@ func (jsk *jdSecKill) GetSecKillInitInfo(ctx context.Context) error {
547
533
}
548
534
549
535
func (jsk * jdSecKill ) GetSecKillUrl () string {
550
- req , _ := http .NewRequest ("GET" , "https://itemko.jd.com/itemShowBtn" , nil )
551
- req .Header .Add ("User-Agent" , jsk .userAgent )
552
- req .Header .Add ("Referer" , "https://item.jd.com/" + jsk .SkuId + ".html" )
553
536
r , _ := jsk .GetReq ("https://itemko.jd.com/itemShowBtn" , map [string ]string {
554
537
"callback" : "jQuery" + strconv .FormatInt (global .GenerateRangeNum (1000000 , 9999999 ), 10 ),
555
538
"skuId" : jsk .SkuId ,
0 commit comments