From 075a4812e4a47e9f2c5d4e8deb8120db76628827 Mon Sep 17 00:00:00 2001 From: timerzz Date: Tue, 14 May 2024 17:43:08 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8Dcoach-client=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/coach-client/client.go | 6 +++--- pkg/coach-client/client_test.go | 18 ++++++++++++++++++ pkg/coach-client/model.go | 12 +++++++----- product/controller.go | 4 ++-- 4 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 pkg/coach-client/client_test.go diff --git a/pkg/coach-client/client.go b/pkg/coach-client/client.go index 1d1d299..9fb3b8b 100644 --- a/pkg/coach-client/client.go +++ b/pkg/coach-client/client.go @@ -6,9 +6,9 @@ import ( "gitea.timerzz.com/kedaya_haitao/common/pkg/proxy" restry_pool "gitea.timerzz.com/kedaya_haitao/common/pkg/restry-pool" "github.com/go-resty/resty/v2" + "github.com/golang/glog" "github.com/pkg/errors" proxy2 "github.com/timerzz/proxypool/pkg/proxy" - "log/slog" "time" ) @@ -30,7 +30,7 @@ func (c *Client) ViewAllBags(ctx context.Context, page int) (resp PageDataRespon } sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut) defer cancel() - urlPath := fmt.Sprintf("https://www.coachoutlet.com/api/get-shop/bags/view-all?page=%s", page) + urlPath := fmt.Sprintf("https://www.coachoutlet.com/api/get-shop/bags/view-all?page=%d", page) err = tryRequest(sctx, urlPath, &resp, c.pool.RandomIterator()) return } @@ -45,7 +45,7 @@ func tryRequest(ctx context.Context, urlPath string, respData any, proxyGetter f } _, err := callByProxy(ctx, p, urlPath, respData) if err != nil { - slog.Debug(err.Error()) + glog.Error(err) continue } return nil diff --git a/pkg/coach-client/client_test.go b/pkg/coach-client/client_test.go new file mode 100644 index 0000000..a8eff65 --- /dev/null +++ b/pkg/coach-client/client_test.go @@ -0,0 +1,18 @@ +package coach_client + +import ( + "context" + "gitea.timerzz.com/kedaya_haitao/common/pkg/proxy" + "testing" +) + +func TestClient(t *testing.T) { + subs := []string{"https://us.timerzz.com:26106/vmess/sub", "https://us.timerzz.com:26106/trojan/sub", "https://us.timerzz.com:26106/ssr/sub"} + pool := proxy.NewProxyPool(subs) + client := NewClient(pool) + resp, err := client.ViewAllBags(context.Background(), 1) + if err != nil { + t.Fatal(err) + } + t.Log(resp) +} diff --git a/pkg/coach-client/model.go b/pkg/coach-client/model.go index 286c412..19169dc 100644 --- a/pkg/coach-client/model.go +++ b/pkg/coach-client/model.go @@ -1,11 +1,13 @@ package coach_client type PageDataResponse struct { - Total int `json:"total"` - Page int `json:"page"` - TotalPages int `json:"totalPages"` - PageSize int `json:"pageSize"` - Products []Product `json:"products"` + PageData struct { + Total int `json:"total"` + Page int `json:"page"` + TotalPages int `json:"totalPages"` + PageSize int `json:"pageSize"` + Products []Product `json:"products"` + } `json:"pageData"` } type Product struct { Name string `json:"name"` diff --git a/product/controller.go b/product/controller.go index 3274f4b..612e7a7 100644 --- a/product/controller.go +++ b/product/controller.go @@ -67,9 +67,9 @@ func (c *Controller) Crawl() error { if err != nil { return fmt.Errorf("访问coach第%d页失败: %w", page, err) } - totalPage = resp.TotalPages + totalPage = resp.PageData.TotalPages - if err = c.saveRespData(resp.Products); err != nil { + if err = c.saveRespData(resp.PageData.Products); err != nil { return fmt.Errorf("保存第%d页数据失败: %w", page, err) } }