diff --git a/search/itunes_search.go b/search/itunes_search.go index f06fc2c..1bc7ec0 100644 --- a/search/itunes_search.go +++ b/search/itunes_search.go @@ -295,6 +295,11 @@ func (p *ITunesProvider) ChartsContext(ctx context.Context, options *ChartsOptio // 为每个播客获取详细信息 lookupURL := fmt.Sprintf("%s/lookup?id=%s", p.FeedAPIEndpoint, id) + // 带上榜单地区:lookup 缺省查 US 区,只在其他地区上架的节目不带地区查不到 + // (实测 CN 区 1498 榜前 50 档会丢 2 档)。 + if options.Country != "" { + lookupURL += "&country=" + options.Country + } p.Logger.Infof("%03d lookup url: %s", cnt, lookupURL) cnt++ lookupReq, err := http.NewRequestWithContext(ctx, "GET", lookupURL, nil) diff --git a/search/itunes_search_test.go b/search/itunes_search_test.go new file mode 100644 index 0000000..1e111cd --- /dev/null +++ b/search/itunes_search_test.go @@ -0,0 +1,54 @@ +package search + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "go.uber.org/ratelimit" +) + +// 榜单逐项 lookup 必须带上榜单地区:只在该地区上架的节目,缺省(US 区)lookup 查不到。 +func TestChartsContextLookupUsesChartCountry(t *testing.T) { + var mu sync.Mutex + var lookupQueries []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case strings.HasPrefix(r.URL.Path, "/cn/rss/toppodcasts/"): + _, _ = w.Write([]byte(`{"feed":{"entry":[{"id":{"attributes":{"im:id":"1796650777"}},"summary":{"label":"s"}}]}}`)) + case r.URL.Path == "/lookup": + mu.Lock() + lookupQueries = append(lookupQueries, r.URL.RawQuery) + mu.Unlock() + if r.URL.Query().Get("country") != "cn" { + _, _ = w.Write([]byte(`{"resultCount":0,"results":[]}`)) + return + } + _, _ = w.Write([]byte(`{"resultCount":1,"results":[{"collectionId":1796650777,"collectionName":"CN Only","feedUrl":"https://f"}]}`)) + default: + http.NotFound(w, r) + } + })) + defer srv.Close() + + p := NewITunesProvider() + p.FeedAPIEndpoint = srv.URL + p.Limiter = ratelimit.NewUnlimited() + + result, err := p.ChartsContext(context.Background(), &ChartsOptions{Country: "cn", GenreID: "1498", Limit: 1}) + if err != nil { + t.Fatalf("ChartsContext: %v", err) + } + if len(lookupQueries) != 1 || !strings.Contains(lookupQueries[0], "country=cn") { + t.Fatalf("lookup 应带 country=cn,实际 %v", lookupQueries) + } + if result.ResultCount != 1 || result.Items[0].CollectionName != "CN Only" { + t.Fatalf("只在 CN 区上架的节目应被查回,实际 %+v", result.Items) + } + if result.Items[0].Summary != "s" { + t.Fatalf("简介取自榜单条目,实际 %q", result.Items[0].Summary) + } +}