From 4bf06457519a3e0cfd86bdc8d2aeee948b91eebf Mon Sep 17 00:00:00 2001 From: ztino <> Date: Tue, 5 Jan 2021 22:12:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96eid?= =?UTF-8?q?=E5=92=8Cfp,=E6=B5=8F=E8=A7=88=E5=99=A8=E5=8D=A1=E4=BD=8F?= =?UTF-8?q?=E4=B8=8D=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + cmd/jdTdudfp.go | 67 +++++++++++++++++-------------------------------- 2 files changed, 24 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 0369067..311a4e9 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ /qr_code.png /cookie.txt *.bak +go.sum diff --git a/cmd/jdTdudfp.go b/cmd/jdTdudfp.go index e55a494..1d8c62b 100644 --- a/cmd/jdTdudfp.go +++ b/cmd/jdTdudfp.go @@ -2,10 +2,8 @@ package cmd import ( "context" - "fmt" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/network" - "github.com/chromedp/cdproto/target" "github.com/chromedp/chromedp" "github.com/spf13/cobra" "github.com/tidwall/gjson" @@ -15,12 +13,13 @@ import ( "log" "net/url" "os" - "strconv" "time" ) func init() { rootCmd.AddCommand(jdTdudfpCmd) + jdTdudfpCmd.Flags().StringP("good_url","g","","") + _=jdTdudfpCmd.MarkFlagRequired("good_url") } var jdTdudfpCmd = &cobra.Command{ @@ -35,8 +34,7 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { if err != nil { log.Println("自动获取eid和fp失败,请重新登录") } else { - retryTimes, _ := strconv.Atoi(common.Config.MustValue("config", "retry_times", "5")) - + log.Println("开始自动获取eid和fp,如遇卡住请结束进程,重新启动") options := []chromedp.ExecAllocatorOption{ chromedp.Flag("headless", false), //debug使用 chromedp.Flag("blink-settings", "imagesEnabled=false"), //禁用图片加载 @@ -53,18 +51,23 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { defer cc() ctx, cancel := chromedp.NewContext(c) - _ = addNewTabListener(ctx) defer cancel() - //设置cookie + //获取cookie u, _ := url.Parse("http://jd.com") cookies := common.CookieJar.Cookies(u) + + //商品链接 + good_url,_:=cmd.Flags().GetString("good_url") + + var res []byte err = chromedp.Run(ctx, chromedp.Tasks{ chromedp.ActionFunc(func(ctx context.Context) error { + //设置cookie expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour)) for _, cookie := range cookies { - _, _ = network.SetCookie(cookie.Name, cookie.Value). + network.SetCookie(cookie.Name, cookie.Value). WithExpires(&expr). WithPath("/"). WithDomain("." + cookie.Domain). @@ -73,33 +76,18 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { return nil }), }, - ) - if err != nil { - log.Println("设置cookie出错了") - log.Fatal(err) - } - - RETRY: - retryTimes-- - log.Println("【重要提醒】自动获取eid和fp期间,建议鼠标跟随页面跳转,滑动到【加入购物车】【去购车结算】【去结算】按钮,但不要点击,可以提升获取成功率!") - log.Println(fmt.Sprintf("开始自动获取eid和fp,如遇卡住请耐心等待,重试次数剩余: %v 次", retryTimes)) - var res []byte - testSkuId := common.Config.MustValue("config", "test_sku_id", "") - err = chromedp.Run(ctx, - chromedp.Tasks{ - chromedp.Navigate(fmt.Sprintf("http://item.jd.com/%s.html", testSkuId)), - chromedp.WaitVisible("#InitCartUrl"), //加入购物车 - chromedp.Sleep(2 * time.Second), - chromedp.Click("#InitCartUrl"), - chromedp.WaitVisible(".btn-addtocart"), //去购车结算 - chromedp.Sleep(2 * time.Second), - chromedp.Click(".btn-addtocart"), - chromedp.WaitVisible(".common-submit-btn"), //去结算 - chromedp.Sleep(2 * time.Second), - chromedp.Click(".common-submit-btn"), - chromedp.Sleep(3 * time.Second), - chromedp.Evaluate("_JdTdudfp", &res), - }, + chromedp.Navigate(good_url), + chromedp.WaitVisible("#InitCartUrl"), //加入购物车 + chromedp.Sleep(2 * time.Second), + chromedp.Click("#InitCartUrl"), + chromedp.WaitVisible(".btn-addtocart"), //去购车结算 + chromedp.Sleep(2 * time.Second), + chromedp.Click(".btn-addtocart"), + chromedp.WaitVisible(".common-submit-btn"), //去结算 + chromedp.Sleep(2 * time.Second), + chromedp.Click(".common-submit-btn"), + chromedp.Sleep(3 * time.Second), + chromedp.Evaluate("_JdTdudfp", &res), ) if err != nil { log.Println("chromedp 出错了") @@ -109,9 +97,6 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { value := string(res) if !gjson.Valid(value) || gjson.Get(value, "eid").String() == "" || gjson.Get(value, "fp").String() == "" { log.Println("获取失败,请重新尝试,返回信息:" + value) - if retryTimes > 0 { - goto RETRY - } } else { eid := gjson.Get(value, "eid").String() fp := gjson.Get(value, "fp").String() @@ -137,9 +122,3 @@ func startJdTdudfp(cmd *cobra.Command, args []string) { } } - -func addNewTabListener(ctx context.Context) <-chan target.ID { - return chromedp.WaitNewTarget(ctx, func(info *target.Info) bool { - return true - }) -}