如何过滤iTunes Search API中的音频播客?

时间:2012-06-20 06:59:48

标签: api itunes podcast

通过使用iTunes API搜索播客(http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html),结果包含音频和视频播客。有没有办法只从API中检索音频播客?

提前致谢: - )

2 个答案:

答案 0 :(得分:0)

从文档中看,似乎不太可能过滤音频和视频播客;但是,您可以遍历结果项目并检查每个项目是否为音频或视频以进行过滤。你可以这样做,从RSS提要中找到更多信息,或者使用subscribePodcast网址再次调用iTunes(参见示例)。

using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text; 
        using System.Net;
        using System.Web.Script.Serialization;
        using System.Xml.Linq;
        using System.IO;

        namespace ConsoleTest
        {
            class Program
            {
                static void Main(string[] args)
                {

                    //Searching for Windows Weekly
                    string url = "https://itunes.apple.com/search?term=Windows%20Weekly&media=podcast&attibute=audio";

                    string json = FetchHTML(url);

                    JavaScriptSerializer s = new JavaScriptSerializer();
                    var result = s.Deserialize(json);

                    var audioOnly = new List();

                    foreach (var item in result.Results)
                    {

                    if (!IsVideo(item.TrackId))
                    {
                        audioOnly.Add(item);
                    }

                }

                foreach (var au in audioOnly)
                {
                    Console.WriteLine(au.TrackName);
                }

                Console.ReadLine();
            }

            static bool IsVideo(string id)
            {
                string req = "https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/com.apple.jingle.app.finance.DirectAction/subscribePodcast?id=" + id + "&wasWarnedAboutPodcasts=true";

                string xml = FetchHTML(req,true);

                bool isVideo = false;

                var re = XElement.Load(new StringReader(xml)).Elements("dict").Elements("dict");

                bool next = false;
                foreach (var e in re.Elements())
                {
                    if (next)
                    {
                        //This is the is video value
                        isVideo = e.Name.LocalName.Trim().ToLower() == "true";
                        next = false;
                        break;
                    }
                    if (e.Value == "is-video")
                    {
                        next = true;
                    } 
                }

                return isVideo;
            }



            static string FetchHTML(string url,bool doItAsITunes = false)
            {
                string htmlCode = "";

                using (WebClient client = new WebClient())
                {
                    if (doItAsITunes)
                    {
                        client.Headers.Add("user-agent", "iTunes/9.1.1");
                    }

                    htmlCode = client.DownloadString(url);
                }

                return htmlCode;
            }

        }

        public class SearchResult
        {
            public SearchResult()
            {
                Results = new List();
            }

            public int ResultCount { set; get; }
            public List Results { set; get; }
        }

        public class Item
        {
            public Item()
            {
                GenreIDs = new List();
                Genres = new List();

            }

            public string WrapperType { set; get; }
            public string Kind { set; get; }
            public string ArtistID { set; get; }
            public string CollectionID { set; get; }
            public string TrackId { set; get; }
            public string ArtistName { set; get; }
            public string CollectionName { set; get; }
            public string TrackName { set; get; }
            public string CollectionCensoredName { set; get; }
            public string TrackCensoredName { set; get; }
            public string ArtistViewUrl { set; get; }
            public string FeedUrl { set; get; }
            public string TrackViewUrl { set; get; }
            public string PreviewUrl { set; get; }
            public string ArtworkUrl60 { set; get; }
            public string ArtworkUrl100 { set; get; }
            public float CollectionPrice { set; get; }
            public float TrackPrice { set; get; }
            public string CollectionExplicitness { set; get; }
            public string TrackExplicitness { set; get; }
            public string DiscCount { set; get; }
            public string DiscNumber { set; get; }
            public string TrackCount { set; get; }
            public string TrackNumber { set; get; }
            public string TrackTimeMillis { set; get; }
            public string Country { set; get; }
            public string Currency { set; get; }
            public string PrimaryGenreName { set; get; }
            public List GenreIDs { set; get; }
            public List Genres { set; get; }
        }

    }

答案 1 :(得分:-1)

是。在常规搜索中你会得到一切: https://itunes.apple.com/search?term=jack+johnson

但是你可以添加一些参数来请求例如(对于你的情况)

&entity=song

所以请求将是:

https://itunes.apple.com/search?term=jack+johnson&entity=song

有关this docs

搜索 seaction的更多信息,请参阅