get all fields from apple podcast
This commit is contained in:
@@ -104,7 +104,7 @@ type ChartsOptions struct {
|
||||
}
|
||||
|
||||
// SearchPodcasts 搜索播客
|
||||
func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*model.SearchResult, error) {
|
||||
func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*model.SearchResult2, error) {
|
||||
if term == "" {
|
||||
return nil, fmt.Errorf("搜索词不能为空")
|
||||
}
|
||||
@@ -178,76 +178,22 @@ func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*m
|
||||
fmt.Printf("body: %s\n", string(body))
|
||||
|
||||
// 解析JSON响应
|
||||
var response struct {
|
||||
ResultCount int `json:"resultCount"`
|
||||
Results []map[string]interface{} `json:"results"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &response); err != nil {
|
||||
return nil, fmt.Errorf("解析响应失败: %w", err)
|
||||
}
|
||||
// var response struct {
|
||||
// ResultCount int `json:"resultCount"`
|
||||
// Results []model.ApplePodcast `json:"results"`
|
||||
// }
|
||||
|
||||
// 创建搜索结果
|
||||
result := &model.SearchResult{
|
||||
ResultCount: response.ResultCount,
|
||||
Successful: true,
|
||||
Items: make([]model.Item, 0, len(response.Results)),
|
||||
result := &model.SearchResult2{
|
||||
Items: []model.ApplePodcast{},
|
||||
ProcessedTime: time.Now(),
|
||||
}
|
||||
|
||||
// 解析结果
|
||||
for _, itemData := range response.Results {
|
||||
item := model.Item{}
|
||||
|
||||
// 提取基本信息
|
||||
if v, ok := itemData["collectionId"]; ok {
|
||||
if id, ok := v.(float64); ok {
|
||||
item.CollectionID = int(id)
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["collectionName"]; ok {
|
||||
if name, ok := v.(string); ok {
|
||||
item.Title = name
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["artistName"]; ok {
|
||||
if name, ok := v.(string); ok {
|
||||
item.Author = name
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["feedUrl"]; ok {
|
||||
if url, ok := v.(string); ok {
|
||||
item.FeedURL = url
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["artworkUrl100"]; ok {
|
||||
if url, ok := v.(string); ok {
|
||||
item.ArtworkURL = url
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["artworkUrl600"]; ok {
|
||||
if url, ok := v.(string); ok {
|
||||
item.ArtworkURL600 = url
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["genres"]; ok {
|
||||
if genres, ok := v.([]interface{}); ok {
|
||||
for _, g := range genres {
|
||||
if genre, ok := g.(string); ok {
|
||||
item.Genres = append(item.Genres, genre)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.Items = append(result.Items, item)
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("解析响应失败: %w", err)
|
||||
}
|
||||
result.ResultCount = len(result.Items)
|
||||
result.Successful = true
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -256,9 +202,10 @@ func (p *ITunesProvider) SearchPodcasts(term string, options *SearchOptions) (*m
|
||||
// 可选参数包括国家、语言、结果数量限制、是否包含限制内容和流派
|
||||
// 由于热门播客列表以feed形式返回,为了与SearchResult兼容,需要解析feed并获取每个项目的底层结果
|
||||
// 这会导致每个结果都有一个HTTP调用,鉴于热门播客列表更新不频繁,建议客户端缓存结果
|
||||
func (p *ITunesProvider) Charts(options *ChartsOptions) (*model.SearchResult, error) {
|
||||
func (p *ITunesProvider) Charts(options *ChartsOptions) (*model.SearchResult2, error) {
|
||||
// 构建URL
|
||||
url := p.buildChartsURL(options)
|
||||
fmt.Printf("url: %s\n", url)
|
||||
|
||||
// 发送请求
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
@@ -286,55 +233,29 @@ func (p *ITunesProvider) Charts(options *ChartsOptions) (*model.SearchResult, er
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取响应失败: %w", err)
|
||||
}
|
||||
// fmt.Printf("body: %s\n", string(body)[:3000])
|
||||
// fmt.Printf("body: %s\n", string(body))
|
||||
|
||||
// 解析JSON响应
|
||||
var response struct {
|
||||
Feed struct {
|
||||
Entry []map[string]interface{} `json:"entry"`
|
||||
} `json:"feed"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &response); err != nil {
|
||||
var chartResult model.ChartResult
|
||||
if err := json.Unmarshal(body, &chartResult); err != nil {
|
||||
return nil, fmt.Errorf("解析响应失败: %w", err)
|
||||
}
|
||||
|
||||
// 创建搜索结果
|
||||
result := &model.SearchResult{
|
||||
Successful: true,
|
||||
Items: make([]model.Item, 0),
|
||||
ProcessedTime: time.Now(),
|
||||
result := &model.SearchResult2{
|
||||
Items: make([]model.ApplePodcast, 0),
|
||||
}
|
||||
|
||||
// 解析结果
|
||||
if response.Feed.Entry != nil {
|
||||
for _, entry := range response.Feed.Entry {
|
||||
if chartResult.Feed.Entry != nil {
|
||||
for _, entry := range chartResult.Feed.Entry {
|
||||
// 获取播客ID
|
||||
var id string
|
||||
if idObj, ok := entry["id"]; ok {
|
||||
if idMap, ok := idObj.(map[string]interface{}); ok {
|
||||
if attrs, ok := idMap["attributes"]; ok {
|
||||
if attrsMap, ok := attrs.(map[string]interface{}); ok {
|
||||
if imID, ok := attrsMap["im:id"]; ok {
|
||||
id = fmt.Sprintf("%v", imID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
id := entry.ID.Attributes.ID
|
||||
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取播客标题
|
||||
// 不再使用title变量,直接在需要时获取
|
||||
if titleObj, ok := entry["title"]; ok {
|
||||
// 移除未使用的titleMap变量
|
||||
if _, ok := titleObj.(map[string]interface{}); ok {
|
||||
// 处理标题信息,如果需要使用
|
||||
}
|
||||
}
|
||||
|
||||
// 为每个播客获取详细信息
|
||||
lookupURL := fmt.Sprintf("%s/lookup?id=%s", p.FeedAPIEndpoint, id)
|
||||
lookupReq, err := http.NewRequest("GET", lookupURL, nil)
|
||||
@@ -358,73 +279,21 @@ func (p *ITunesProvider) Charts(options *ChartsOptions) (*model.SearchResult, er
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
fmt.Printf("lookupBody: %s\n", string(lookupBody))
|
||||
|
||||
var lookupResponse struct {
|
||||
ResultCount int `json:"resultCount"`
|
||||
Results []map[string]interface{} `json:"results"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(lookupBody, &lookupResponse); err != nil {
|
||||
var tempResult model.LookupResult
|
||||
if err := json.Unmarshal(lookupBody, &tempResult); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if lookupResponse.ResultCount > 0 && len(lookupResponse.Results) > 0 {
|
||||
itemData := lookupResponse.Results[0]
|
||||
item := model.Item{}
|
||||
|
||||
// 提取基本信息
|
||||
if v, ok := itemData["collectionId"]; ok {
|
||||
if id, ok := v.(float64); ok {
|
||||
item.CollectionID = int(id)
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["collectionName"]; ok {
|
||||
if name, ok := v.(string); ok {
|
||||
item.Title = name
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["artistName"]; ok {
|
||||
if name, ok := v.(string); ok {
|
||||
item.Author = name
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["feedUrl"]; ok {
|
||||
if url, ok := v.(string); ok {
|
||||
item.FeedURL = url
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["artworkUrl100"]; ok {
|
||||
if url, ok := v.(string); ok {
|
||||
item.ArtworkURL = url
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["artworkUrl600"]; ok {
|
||||
if url, ok := v.(string); ok {
|
||||
item.ArtworkURL600 = url
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := itemData["genres"]; ok {
|
||||
if genres, ok := v.([]interface{}); ok {
|
||||
for _, g := range genres {
|
||||
if genre, ok := g.(string); ok {
|
||||
item.Genres = append(item.Genres, genre)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.Items = append(result.Items, item)
|
||||
if tempResult.ResultCount > 0 && len(tempResult.Items) > 0 {
|
||||
result.Items = append(result.Items, tempResult.Items[0])
|
||||
}
|
||||
}
|
||||
result.ResultCount = len(result.Items)
|
||||
}
|
||||
|
||||
result.ResultCount = len(result.Items)
|
||||
// result.ResultCount = len(result.Items)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -455,7 +324,7 @@ func (p *ITunesProvider) buildChartsURL(options *ChartsOptions) string {
|
||||
if options.Limit > 0 {
|
||||
buf += strconv.Itoa(options.Limit)
|
||||
} else {
|
||||
buf += "20"
|
||||
buf += "200"
|
||||
}
|
||||
|
||||
// 添加流派
|
||||
@@ -468,9 +337,9 @@ func (p *ITunesProvider) buildChartsURL(options *ChartsOptions) string {
|
||||
// 添加限制内容设置
|
||||
buf += "/explicit="
|
||||
if options.Explicit {
|
||||
buf += "Yes"
|
||||
buf += "true"
|
||||
} else {
|
||||
buf += "No"
|
||||
buf += "false"
|
||||
}
|
||||
|
||||
// 添加JSON格式
|
||||
|
||||
Reference in New Issue
Block a user