From 2623502cc8155f6cae5c50f693320451c68d5706 Mon Sep 17 00:00:00 2001 From: eson Date: Wed, 25 Nov 2020 16:33:15 +0800 Subject: [PATCH] feat(config): add retry and waitcapture property --- .gitignore | 1 + client.go | 45 ++++++++++++++++++++++++++++++++++++--------- client_test.go | 2 +- 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee1e57a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +debug.test diff --git a/client.go b/client.go index 45f0d08..cce9fe0 100644 --- a/client.go +++ b/client.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "os" + "strconv" "sync" "time" @@ -54,12 +55,36 @@ type Client struct { type Label struct { label string - conditionJS string - conditionlock sync.Mutex + conditionJS string + retry string + waitcapture string + configlock sync.Mutex cli *Client } +// GetWaitime Get return waitime int +func (l *Label) GetWaitime() int { + r, _ := strconv.Atoi(l.waitcapture) + return r +} + +// SetWaitime Set waitime int +func (l *Label) SetWaitime(waitime int) { + l.waitcapture = strconv.Itoa(waitime) +} + +// GetRetry Get return retry int +func (l *Label) GetRetry() int { + r, _ := strconv.Atoi(l.retry) + return r +} + +// SetRetry Set retry int +func (l *Label) SetRetry(retry int) { + l.retry = strconv.Itoa(retry) +} + // GetHash 根据label获取hash路径 func (l *Label) GetHash(label string) string { if cb, ok := l.cli.register.Load(label); ok { @@ -83,8 +108,8 @@ func (l *Label) Open(urlstr string, carray interface{}) (bodyRes string, ok bool // SetContentCondition 设置识别到的内容条件. js代码. 必须是一个函数. 命名可以随意. 返回bool func (l *Label) SetContentCondition(jsScript string) { - l.conditionlock.Lock() - defer l.conditionlock.Unlock() + l.configlock.Lock() + defer l.configlock.Unlock() l.conditionJS = jsScript } @@ -98,8 +123,8 @@ func (l *Label) SetContentConditionFromFile(jsScriptFile string) { if err != nil { panic(err) } - l.conditionlock.Lock() - defer l.conditionlock.Unlock() + l.configlock.Lock() + defer l.configlock.Unlock() // log.Println(string(data)) l.conditionJS = string(data) } @@ -132,7 +157,7 @@ func (cli *Client) Register(label string, callback func(cxt *CallbackContext)) * } cli.register.Store(label, cb) cli.register.Store(cb.hash, cb) - l := &Label{label: label, cli: cli} + l := &Label{label: label, cli: cli, waitcapture: "6000", retry: "1"} return l } @@ -239,9 +264,11 @@ func (cli *Client) open(label *Label, urlstr string, carray interface{}) (bodyRe } func() { - label.conditionlock.Lock() - defer label.conditionlock.Unlock() + label.configlock.Lock() + defer label.configlock.Unlock() data["content_condition"] = []string{label.conditionJS} + data["waitcapture"] = []string{label.waitcapture} + data["retry"] = []string{label.retry} }() resp, err := http.DefaultClient.PostForm(cli.chromeProxyAddr+"/task/put", data) diff --git a/client_test.go b/client_test.go index 122fa62..1e3e076 100644 --- a/client_test.go +++ b/client_test.go @@ -20,7 +20,7 @@ func TestPort(t *testing.T) { ltest.SetContentConditionFromFile("example.js") cli.Connect() - // log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser", nil)) + log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser", nil)) // log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser", "213")) // log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser")) // log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser"))