From ea5315348beb9375c805df2151d5d5f550bdac4b Mon Sep 17 00:00:00 2001 From: timerzz Date: Fri, 6 Dec 2024 17:07:14 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0coach=20=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/coach-client/{client_cn.go => cn.go} | 3 +- .../{client_cn_test.go => cn_test.go} | 0 pkg/coach-client/{client_us.go => us.go} | 71 +++++++++++++------ .../{client_us_test.go => us_test.go} | 0 pkg/proxy/proxy_test.go | 15 ++++ structs/storage/article_test.go | 2 +- structs/storage/provider-article_test.go | 17 +++++ 7 files changed, 86 insertions(+), 22 deletions(-) rename pkg/coach-client/{client_cn.go => cn.go} (99%) rename pkg/coach-client/{client_cn_test.go => cn_test.go} (100%) rename pkg/coach-client/{client_us.go => us.go} (71%) rename pkg/coach-client/{client_us_test.go => us_test.go} (100%) create mode 100644 pkg/proxy/proxy_test.go diff --git a/pkg/coach-client/client_cn.go b/pkg/coach-client/cn.go similarity index 99% rename from pkg/coach-client/client_cn.go rename to pkg/coach-client/cn.go index 43b4758..8564614 100644 --- a/pkg/coach-client/client_cn.go +++ b/pkg/coach-client/cn.go @@ -4,12 +4,13 @@ import ( "context" "crypto/tls" "fmt" + "time" + "github.com/bytedance/sonic" "github.com/cloudwego/hertz/pkg/app/client" "github.com/cloudwego/hertz/pkg/network/standard" "github.com/cloudwego/hertz/pkg/protocol" "github.com/corpix/uarand" - "time" ) type XMacHeader string diff --git a/pkg/coach-client/client_cn_test.go b/pkg/coach-client/cn_test.go similarity index 100% rename from pkg/coach-client/client_cn_test.go rename to pkg/coach-client/cn_test.go diff --git a/pkg/coach-client/client_us.go b/pkg/coach-client/us.go similarity index 71% rename from pkg/coach-client/client_us.go rename to pkg/coach-client/us.go index b94116d..9f9e76e 100644 --- a/pkg/coach-client/client_us.go +++ b/pkg/coach-client/us.go @@ -14,19 +14,50 @@ import ( proxy2 "github.com/timerzz/proxypool/pkg/proxy" ) -type US struct { - pool *proxy.ProxyPool - defaultTimeOut time.Duration +const ( + BaseUrl_Coach = "https://www.coach.com" + BaseUrl_CoachOutlet = "https://www.coachoutlet.com" +) + +type USClientBuilder struct { + pool *proxy.ProxyPool } -func USClient(pool *proxy.ProxyPool) *US { - return &US{ - pool: pool, - defaultTimeOut: 10 * time.Minute, +func US(pool *proxy.ProxyPool) *USClientBuilder { + return &USClientBuilder{ + pool: pool, } } -func tryRequest(ctx context.Context, urlPath string, respData any, proxyGetter func() proxy2.Proxy) error { +func (u *USClientBuilder) Coach() USClient { + return &us{ + pool: u.pool, + defaultTimeOut: 10 * time.Minute, + baseUrl: BaseUrl_Coach, + } +} + +func (u *USClientBuilder) CoachOutlet() USClient { + return &us{ + pool: u.pool, + defaultTimeOut: 10 * time.Minute, + baseUrl: BaseUrl_CoachOutlet, + } +} + +type USClient interface { + RequestInventory(ctx context.Context, pid string) (inv Inventory, err error) + RequestProductDetail(ctx context.Context, pid string) (data ProductData, err error) + ViewAllBags(ctx context.Context, page int) (resp PageDataResponse, err error) +} + +type us struct { + pool *proxy.ProxyPool + defaultTimeOut time.Duration + baseUrl string +} + +func tryRequest(ctx context.Context, base, urlPath string, respData any, proxyGetter func() proxy2.Proxy) error { for p := proxyGetter(); p != nil; p = proxyGetter() { select { case <-ctx.Done(): @@ -37,7 +68,7 @@ func tryRequest(ctx context.Context, urlPath string, respData any, proxyGetter f default: } - resp, err := callByProxy(ctx, p, urlPath, respData) + resp, err := callByProxy(ctx, p, base, urlPath, respData) if err != nil { slog.Debug(err.Error()) continue @@ -51,7 +82,7 @@ func tryRequest(ctx context.Context, urlPath string, respData any, proxyGetter f return errors.New("未获取到可用的代理") } -func callByProxy(ctx context.Context, p proxy2.Proxy, urlPath string, result any) (*resty.Response, error) { +func callByProxy(ctx context.Context, p proxy2.Proxy, base, urlPath string, result any) (*resty.Response, error) { addr, err := proxy.UrlToMetadata(urlPath) if err != nil { return nil, err @@ -66,7 +97,7 @@ func callByProxy(ctx context.Context, p proxy2.Proxy, urlPath string, result any var cli = restry_pool.GetRestyClient(conn) defer restry_pool.PutRestyClient(cli) - return cli.R().SetResult(result).Get(urlPath) + return cli.SetBaseURL(base).R().SetResult(result).Get(urlPath) } type Inventory struct { @@ -88,12 +119,12 @@ type InventoryResponse struct { } `json:"inventory"` } -func (c *US) RequestInventory(ctx context.Context, pid string) (inv Inventory, err error) { +func (c *us) RequestInventory(ctx context.Context, pid string) (inv Inventory, err error) { sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut) defer cancel() var resp InventoryResponse - urlPath := fmt.Sprintf("https://www.coachoutlet.com/api/inventory?vgId=%s&includeVariantData=false", url.QueryEscape(pid)) - err = tryRequest(sctx, urlPath, &resp, c.pool.RandomIterator()) + urlPath := fmt.Sprintf("/api/inventory?vgId=%s&includeVariantData=false", url.QueryEscape(pid)) + err = tryRequest(sctx, c.baseUrl, urlPath, &resp, c.pool.RandomIterator()) inv = resp.Inventory.InventoryInfo return } @@ -123,12 +154,12 @@ type ProductDataResponse struct { ProductData []*ProductData `json:"productsData"` } -func (c *US) RequestProductDetail(ctx context.Context, pid string) (data ProductData, err error) { +func (c *us) RequestProductDetail(ctx context.Context, pid string) (data ProductData, err error) { sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut) defer cancel() var resp ProductDataResponse - urlPath := fmt.Sprintf("https://www.coachoutlet.com/api/get-products?ids=%s&includeInventory=false", url.QueryEscape(pid)) - err = tryRequest(sctx, urlPath, &resp, c.pool.RandomIterator()) + urlPath := fmt.Sprintf("/api/get-products?ids=%s&includeInventory=false", url.QueryEscape(pid)) + err = tryRequest(sctx, c.baseUrl, urlPath, &resp, c.pool.RandomIterator()) if len(resp.ProductData) == 0 && err != nil { err = fmt.Errorf("获取详情信息为空") return @@ -191,13 +222,13 @@ type Sales struct { DecimalPrice string `json:"decimalPrice"` } -func (c *US) ViewAllBags(ctx context.Context, page int) (resp PageDataResponse, err error) { +func (c *us) ViewAllBags(ctx context.Context, page int) (resp PageDataResponse, err error) { if page < 1 { page = 1 } sctx, cancel := context.WithTimeout(ctx, c.defaultTimeOut) defer cancel() - urlPath := fmt.Sprintf("https://www.coachoutlet.com/api/get-shop/bags/view-all?page=%d", page) - err = tryRequest(sctx, urlPath, &resp, c.pool.RandomIterator()) + urlPath := fmt.Sprintf("/api/get-shop/bags/view-all?page=%d", page) + err = tryRequest(sctx, c.baseUrl, urlPath, &resp, c.pool.RandomIterator()) return } diff --git a/pkg/coach-client/client_us_test.go b/pkg/coach-client/us_test.go similarity index 100% rename from pkg/coach-client/client_us_test.go rename to pkg/coach-client/us_test.go diff --git a/pkg/proxy/proxy_test.go b/pkg/proxy/proxy_test.go new file mode 100644 index 0000000..62dc814 --- /dev/null +++ b/pkg/proxy/proxy_test.go @@ -0,0 +1,15 @@ +package proxy + +import "testing" + +func TestNewProxyPool(t *testing.T) { + NewProxyPool(&Option{ + Subscribes: []string{ + "https://us.timerzz.com:26106/vmess/sub", + "https://us.timerzz.com:26106/trojan/sub", + "https://us.timerzz.com:26106/ssr/sub", + "https://us.timerzz.com:26106/ss/sub", + }, + Clash: []string{"https://us.timerzz.com:26106/clash/proxies"}, + }) +} diff --git a/structs/storage/article_test.go b/structs/storage/article_test.go index 07a3916..4513f42 100644 --- a/structs/storage/article_test.go +++ b/structs/storage/article_test.go @@ -10,7 +10,7 @@ import ( ) func GetDevDB() (*gorm.DB, error) { - dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya", "5432") + dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya_dev", "5432") return gorm.Open(postgres.Open(dsn), &gorm.Config{}) } diff --git a/structs/storage/provider-article_test.go b/structs/storage/provider-article_test.go index 358fa0a..13c4990 100644 --- a/structs/storage/provider-article_test.go +++ b/structs/storage/provider-article_test.go @@ -3,6 +3,7 @@ package storage import ( "fmt" "testing" + "time" v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" "gorm.io/gorm" @@ -86,3 +87,19 @@ func TestFullImage(t *testing.T) { //} //fmt.Printf("以下共%d个pid未找到: %s", len(nots), nots) } + +func TestProviderArticleApi_FindByPid(t *testing.T) { + db, err := GetDevDB() + if err != nil { + t.Fatal(err) + } + var a v2.ProviderArticle + now := time.Now() + n := now.String() + fmt.Println(n) + tt := time.Now().Add(-30 * time.Minute) + s := tt.String() + fmt.Println(s) + db.Preload("HistoryAts", "created_at > ?", tt).First(&a, "pid=?", "CX607-IMXB8") + fmt.Println(a) +}