|
|
@ -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 |
|
|
|
}) |
|
|
|
} |
|
|
|