Browse Source

优化自动获取eid和fp,并写入conf.ini

main
pppscn 4 years ago
parent
commit
1a5c4c7ad3
  1. 5
      .gitignore
  2. 3
      README.md
  3. 126
      cmd/jdTdudfp.go
  4. 3
      conf.ini

5
.gitignore

@ -15,3 +15,8 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
/*.log
/.idea
/qr_code.png
/cookie.txt
*.bak

3
README.md

@ -44,7 +44,8 @@ jd_seckill login
### 自动获取eid,fp ### 自动获取eid,fp
> ⚠依赖谷歌浏览器,请安装谷歌浏览器,获取到的eid和fp请手动填入配置文件 > ⚠依赖谷歌浏览器,请安装谷歌浏览器,windows下请将安全目录加入系统变量Path
> ⚠【重要提醒】自动获取eid和fp期间,建议鼠标跟随页面跳转,滑动到【加入购物车】【去购车结算】【去结算】按钮,但不要点击,可以提升获取成功率!(经验之谈!)
执行以下命令按照提示操作: 执行以下命令按照提示操作:
```shell ```shell

126
cmd/jdTdudfp.go

@ -2,16 +2,20 @@ package cmd
import ( import (
"context" "context"
"fmt"
"github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network" "github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/target" "github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/unknwon/goconfig"
"github.com/ztino/jd_seckill/common" "github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/jd_seckill" "github.com/ztino/jd_seckill/jd_seckill"
"log" "log"
"net/url" "net/url"
"os"
"strconv"
"time" "time"
) )
@ -22,26 +26,26 @@ func init() {
var jdTdudfpCmd = &cobra.Command{ var jdTdudfpCmd = &cobra.Command{
Use: "jdTdudfp", Use: "jdTdudfp",
Short: "auto get jd eid and fp", Short: "auto get jd eid and fp",
Run: startJdTdudfp, Run: startJdTdudfp,
} }
func startJdTdudfp(cmd *cobra.Command, args []string) { func startJdTdudfp(cmd *cobra.Command, args []string) {
session:=jd_seckill.NewSession(common.CookieJar) session := jd_seckill.NewSession(common.CookieJar)
err:=session.CheckLoginStatus() err := session.CheckLoginStatus()
if err!=nil { if err != nil {
log.Println("自动获取eid和fp失败,请重新登录") log.Println("自动获取eid和fp失败,请重新登录")
}else { } else {
log.Println("开始自动获取eid和fp,如遇卡住请结束进程,重新启动") retryTimes, _ := strconv.Atoi(common.Config.MustValue("config", "retry_times", "5"))
options := []chromedp.ExecAllocatorOption{ options := []chromedp.ExecAllocatorOption{
chromedp.Flag("headless", false), chromedp.Flag("headless", false), //debug使用
//chromedp.Flag("blink-settings", "imagesEnabled=false"), chromedp.Flag("blink-settings", "imagesEnabled=false"), //禁用图片加载
chromedp.Flag("start-maximized", true), chromedp.Flag("start-maximized", true), //最大化窗口
chromedp.Flag("no-sandbox",true), chromedp.Flag("no-sandbox", true), //禁用沙盒, 性能优先
chromedp.Flag("disable-setuid-sandbox",true), chromedp.Flag("disable-setuid-sandbox", true), //禁用setuid沙盒, 性能优先
chromedp.Flag("no-default-browser-check",true), chromedp.Flag("no-default-browser-check", true), //不检查默认浏览器
chromedp.Flag("disable-plugins",true), chromedp.Flag("disable-plugins", true), //禁用扩展
chromedp.WindowSize(1920,1080), chromedp.UserAgent(common.Config.MustValue("config", "default_user_agent", "")),
chromedp.UserAgent(common.Config.MustValue("config","default_user_agent","")),
} }
options = append(chromedp.DefaultExecAllocatorOptions[:], options...) options = append(chromedp.DefaultExecAllocatorOptions[:], options...)
@ -49,9 +53,10 @@ func startJdTdudfp(cmd *cobra.Command, args []string) {
defer cc() defer cc()
ctx, cancel := chromedp.NewContext(c) ctx, cancel := chromedp.NewContext(c)
ch := addNewTabListener(ctx) _ = addNewTabListener(ctx)
defer cancel() defer cancel()
//设置cookie
u, _ := url.Parse("http://jd.com") u, _ := url.Parse("http://jd.com")
cookies := common.CookieJar.Cookies(u) cookies := common.CookieJar.Cookies(u)
err = chromedp.Run(ctx, err = chromedp.Run(ctx,
@ -67,61 +72,74 @@ func startJdTdudfp(cmd *cobra.Command, args []string) {
} }
return nil return nil
}), }),
chromedp.Navigate("https://jd.com"),
chromedp.Sleep(2*time.Second),
chromedp.Click(".cate_menu_lk"),
chromedp.Sleep(2*time.Second),
}, },
) )
if err != nil { if err != nil {
log.Println("设置cookie出错了")
log.Fatal(err) log.Fatal(err)
} }
newCtx, cancel2 := chromedp.NewContext(ctx, chromedp.WithTargetID(<-ch)) RETRY:
ch2 := addNewTabListener(newCtx) retryTimes--
defer cancel2() log.Println("【重要提醒】自动获取eid和fp期间,建议鼠标跟随页面跳转,滑动到【加入购物车】【去购车结算】【去结算】按钮,但不要点击,可以提升获取成功率!")
log.Println(fmt.Sprintf("开始自动获取eid和fp,如遇卡住请耐心等待,重试次数剩余: %v 次", retryTimes))
err = chromedp.Run(newCtx, var res []byte
chromedp.Click(".goods_item_link"), testSkuId := common.Config.MustValue("config", "test_sku_id", "")
chromedp.Sleep(4*time.Second), 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),
},
) )
if err != nil { if err != nil {
log.Println("chromedp 出错了")
log.Fatal(err) log.Fatal(err)
} }
newCtx2, cancel3 := chromedp.NewContext(ctx, chromedp.WithTargetID(<-ch2)) value := string(res)
defer cancel3() 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()
log.Println("eid:" + eid)
log.Println("fp:" + fp)
var res []byte //修改配置文件
err = chromedp.Run(newCtx2, confFile := "./conf.ini"
chromedp.Click("#InitCartUrl"), cfg, err := goconfig.LoadConfigFile(confFile)
chromedp.Sleep(2*time.Second), if err != nil {
chromedp.Click(".btn-addtocart"), log.Println("配置文件不存在,程序退出")
chromedp.Sleep(2*time.Second), os.Exit(0)
chromedp.Click(".common-submit-btn"), }
chromedp.Sleep(3*time.Second),
chromedp.Evaluate("_JdTdudfp", &res),
)
if err != nil {
log.Fatal(err)
}
value:=string(res) cfg.SetValue("config", "eid", eid)
if !gjson.Valid(value) || gjson.Get(value,"eid").String()=="" || gjson.Get(value,"fp").String()=="" { cfg.SetValue("config", "fp", fp)
log.Println("获取失败,请重新尝试,返回信息:"+value) if err := goconfig.SaveConfigFile(cfg, confFile); err != nil {
}else{ log.Println("保存配置文件失败,请手动填入配置文件")
log.Println("获取成功,请手动填入配置文件") }
log.Println("eid:"+gjson.Get(value,"eid").String())
log.Println("fp:"+gjson.Get(value,"fp").String()) log.Println("eid, fp参数已经自动填入配置文件")
} }
} }
} }
func addNewTabListener(ctx context.Context) <-chan target.ID { func addNewTabListener(ctx context.Context) <-chan target.ID {
/* mux := http.NewServeMux()
ts := httptest.NewServer(mux)
defer ts.Close()*/
return chromedp.WaitNewTarget(ctx, func(info *target.Info) bool { return chromedp.WaitNewTarget(ctx, func(info *target.Info) bool {
return true return true
}) })
} }

3
conf.ini

@ -4,6 +4,9 @@
# 随意填写可能导致订单无法提交等问题 # 随意填写可能导致订单无法提交等问题
eid = eid =
fp = fp =
# 自动获取eid, fp的重试次数,测试商品sku_id
retry_times = 5
test_sku_id = 100016034372
# 商品id # 商品id
# 已经是茅台的sku_id了 # 已经是茅台的sku_id了
sku_id = 100012043978 sku_id = 100012043978

Loading…
Cancel
Save