From fb7545f9a2ddb8e0d7ad752bc589e3ecb7dc0278 Mon Sep 17 00:00:00 2001 From: hxuanyu <2252193204@qq.com> Date: Sat, 31 Jan 2026 00:22:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20Bing=20API=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E5=92=8C=E5=9C=B0=E5=8C=BA=E8=AF=86=E5=88=AB=E6=94=AF?= =?UTF-8?q?=E6=8C=81=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=BC=A0=E9=80=92=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87=E5=B9=B6=E5=A2=9E=E5=BC=BA=20HTTP=20?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A4=B4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/fetcher/fetcher.go | 40 +++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/internal/service/fetcher/fetcher.go b/internal/service/fetcher/fetcher.go index 9e5cc7c..b6648ce 100644 --- a/internal/service/fetcher/fetcher.go +++ b/internal/service/fetcher/fetcher.go @@ -93,9 +93,21 @@ func (f *Fetcher) FetchRegion(ctx context.Context, mkt string) error { } func (f *Fetcher) fetchByMkt(ctx context.Context, mkt string, idx int, n int) error { - url := fmt.Sprintf("%s?format=js&idx=%d&n=%d&uhd=1&mkt=%s", config.BingAPIBase, idx, n, mkt) + lang := strings.Split(mkt, "-")[0] + url := fmt.Sprintf("%s?format=js&idx=%d&n=%d&uhd=1&mkt=%s&setlang=%s", config.BingAPIBase, idx, n, mkt, lang) util.Logger.Info("Requesting Bing API", zap.String("url", url)) - resp, err := f.httpClient.Get(url) + + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + if err != nil { + util.Logger.Error("Failed to create Bing API request", zap.Error(err)) + return err + } + + // 添加请求头以增强地区/语言识别 + req.Header.Set("Accept-Language", fmt.Sprintf("%s,%s;q=0.9", mkt, lang)) + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") + + resp, err := f.httpClient.Do(req) if err != nil { util.Logger.Error("Failed to request Bing API", zap.Error(err)) return err @@ -140,7 +152,7 @@ func (f *Fetcher) processImage(ctx context.Context, bingImg BingImage, mkt strin imageName := f.extractImageName(bingImg.URLBase, bingImg.HSH) // 2. 处理变体 - imgURL, variantName := f.probeUHD(bingImg.URLBase) + imgURL, variantName := f.probeUHD(ctx, bingImg.URLBase) targetVariants := []struct { name string width int @@ -173,7 +185,7 @@ func (f *Fetcher) processImage(ctx context.Context, bingImg BingImage, mkt strin } else { util.Logger.Debug("Downloading and processing image", zap.String("url", imgURL), zap.String("imageName", imageName)) var err error - imgData, err = f.downloadImage(imgURL) + imgData, err = f.downloadImage(ctx, imgURL) if err != nil { util.Logger.Error("Failed to download image", zap.String("url", imgURL), zap.Error(err)) return err @@ -268,17 +280,29 @@ func (f *Fetcher) extractImageName(urlBase, hsh string) string { return name } -func (f *Fetcher) probeUHD(urlBase string) (string, string) { +func (f *Fetcher) probeUHD(ctx context.Context, urlBase string) (string, string) { uhdURL := fmt.Sprintf("https://www.bing.com%s_UHD.jpg", urlBase) - resp, err := f.httpClient.Head(uhdURL) + req, err := http.NewRequestWithContext(ctx, "HEAD", uhdURL, nil) + if err != nil { + return fmt.Sprintf("https://www.bing.com%s_1920x1080.jpg", urlBase), "1920x1080" + } + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") + + resp, err := f.httpClient.Do(req) if err == nil && resp.StatusCode == http.StatusOK { return uhdURL, "UHD" } return fmt.Sprintf("https://www.bing.com%s_1920x1080.jpg", urlBase), "1920x1080" } -func (f *Fetcher) downloadImage(url string) ([]byte, error) { - resp, err := f.httpClient.Get(url) +func (f *Fetcher) downloadImage(ctx context.Context, url string) ([]byte, error) { + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) + if err != nil { + return nil, err + } + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") + + resp, err := f.httpClient.Do(req) if err != nil { return nil, err }