Skip to content

Commit a36d701

Browse files
committed
e
1 parent 96806cc commit a36d701

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

chromedpEngine/allocator.go

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ var DefaultOptions = []chromedp.ExecAllocatorOption{
3333
chromedp.Flag("disable-plugins", false),
3434
//chromedp.Flag("disable-software-rasterizer", true),
3535
chromedp.NoDefaultBrowserCheck,
36-
chromedp.DisableGPU,
3736
chromedp.NoFirstRun,
3837
}
3938

secKill/jdSecKill.go

+16-33
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/chromedp/cdproto/dom"
1010
"github.com/chromedp/cdproto/network"
1111
"github.com/chromedp/cdproto/page"
12-
"github.com/chromedp/cdproto/target"
1312
"github.com/chromedp/chromedp"
1413
"github.com/tidwall/gjson"
1514
"github.com/zqijzqj/mtSecKill/chromedpEngine"
@@ -31,7 +30,6 @@ type jdSecKill struct {
3130
ctx context.Context
3231
cancel context.CancelFunc
3332
bCtx context.Context
34-
bWorksCtx []context.Context
3533
isLogin bool
3634
isClose bool
3735
mu sync.Mutex
@@ -111,6 +109,9 @@ func (jsk *jdSecKill) GetReq(reqUrl string, params map[string]string, referer st
111109
if err != nil {
112110
return gjson.Result{}, err
113111
}
112+
if resp.StatusCode != 200 {
113+
logs.PrintlnWarning("httpCode: ", resp.StatusCode, "reqUrl: ", resp.Request.URL)
114+
}
114115
//设置cookie到浏览器
115116
for _, respCookie := range resp.Cookies() {
116117
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,
155156
if err != nil {
156157
return gjson.Result{}, err
157158
}
159+
160+
if resp.StatusCode != 200 {
161+
logs.PrintlnWarning("httpCode: ", resp.StatusCode, "reqUrl: ", resp.Request.URL)
162+
}
158163
//设置cookie到浏览器
159164
for _, respCookie := range resp.Cookies() {
160165
_, _ = network.SetCookie(respCookie.Name, respCookie.Value).WithURL(resp.Request.URL.String()).Do(ctx)
@@ -244,28 +249,26 @@ func (jsk *jdSecKill) Run() error {
244249
jsk.GetEidAndFp(),
245250
jsk.WaitStart(),
246251
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() {
250254
for {
251255
jsk.FetchSecKillUrl()
252256
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 禁止重定向后就会是空数据 所以这里空数据是正常的
254259
if err == nil || err.Error() == ErrEmptyData.Error() {
255260
break
256261
}
257-
logs.PrintErr("抢购连接访问错误,正在重试:", err)
258262
}
259-
//_, _, _, _ = page.Navigate(jsk.SecKillUrl).WithReferrer("https://item.jd.com/"+jsk.SkuId+".html").Do(ctx2)
260-
SecKillRE:
263+
SecKillRE:
261264
//请求抢购连接,提交订单
262-
err := jsk.ReqSubmitSecKillOrder(ctx2)
265+
err := jsk.ReqSubmitSecKillOrder(jsk.bCtx)
263266
if err != nil {
264267
logs.PrintlnInfo(err, "等待重试")
265268
goto SecKillRE
266269
}
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+
}()
269272
}
270273
select {
271274
case <-jsk.IsOkChan:
@@ -282,23 +285,6 @@ func (jsk *jdSecKill) Run() error {
282285
func (jsk *jdSecKill) WaitStart() chromedp.ActionFunc {
283286
return func(ctx context.Context) error {
284287
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-
}
302288
_ = chromedp.Navigate(u).Do(ctx)
303289
st := jsk.StartTime.UnixNano() / 1e6
304290
logs.PrintlnInfo("等待时间到达" + jsk.StartTime.Format(global.DateTimeFormatStr) + "...... 请勿关闭浏览器")
@@ -404,7 +390,7 @@ func (jsk *jdSecKill) ReqSubmitSecKillOrder(ctx context.Context) error {
404390
}
405391

406392
//这里修改为直接使用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())
408394
logs.PrintlnInfo("访问抢购订单结算页面......", skUrl)
409395
_, _ = jsk.GetReq(skUrl, nil, "https://item.jd.com/"+jsk.SkuId+".html", ctx, true)
410396

@@ -547,9 +533,6 @@ func (jsk *jdSecKill) GetSecKillInitInfo(ctx context.Context) error {
547533
}
548534

549535
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")
553536
r, _ := jsk.GetReq("https://itemko.jd.com/itemShowBtn", map[string]string{
554537
"callback": "jQuery" + strconv.FormatInt(global.GenerateRangeNum(1000000, 9999999), 10),
555538
"skuId": jsk.SkuId,

0 commit comments

Comments
 (0)