示例#1
0
        protected override void FetchHeadline()
        {
            channels.Clear();
            // フィルターのクリア
            channelsMatchesToFilterCache   = null;
            channelsUnmatchesToFilterCache = null;
            fetchHeadlineFormatStatus      = FetchHeadlineFormats.None;

            // ヘッドラインが取得できない場合は取得を3回繰り返す。
            // RSSタイプのヘッドラインが取得できないことがある。
            for (int i = 0; (i < 3) && (fetchHeadlineFormatStatus != FetchHeadlineFormats.Rss); ++i)
            {
                fetchHeadline();
            }
        }
示例#2
0
        /// <summary>
        /// ヘッドラインを取得する
        /// </summary>
        private void fetchHeadline()
        {
            fetchHeadlineFormatStatus = FetchHeadlineFormats.None;

            Stream    input  = null;
            XmlReader reader = null;

            try
            {
                // 番組
                Channel channel = null;

                // Itemタグの中にいるか
                bool inItenFlag = false;
                // Enclosureの一時リスト
                List <Enclosure> list = new List <Enclosure>();

                // URLを生成
                string seachWord;
                if (Setting.SearchWord != string.Empty)
                {
                    seachWord = "&search=" + Uri.EscapeUriString(Setting.SearchWord.Replace(' ', '+').Replace(" ", "+"));
                }
                // 検索単語が空の場合はサーバからヘッドラインが返ってこないので、"Top 40"ジャンルのヘッドラインを取得してごまかす
                else
                {
                    seachWord = "&genre=" + Uri.EscapeUriString("Top 40");
                }
                string limitStr = (Setting.PerView.ToString() != string.Empty) ? ("&limit=" + this.Setting.PerView) : string.Empty;
                Uri    uri      = new Uri(SHOUTCAST_URL + "?rss=1" + seachWord + limitStr);

                input  = connectionSetting.CreateStream(uri);
                reader = new XmlTextReader(input);

                while (reader.Read())
                {
                    if ((fetchCancel == true) || (fetchHeadlineFormatStatus == FetchHeadlineFormats.StationList))
                    {
                        return;
                    }

                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if ((fetchHeadlineFormatStatus == FetchHeadlineFormats.None) && (reader.LocalName == "rss"))
                        {
                            fetchHeadlineFormatStatus = FetchHeadlineFormats.Rss;
                        }
                        else if ((fetchHeadlineFormatStatus == FetchHeadlineFormats.None) && (reader.LocalName == "stationlist"))
                        {
                            fetchHeadlineFormatStatus = FetchHeadlineFormats.StationList;
                        }
                        else if (reader.LocalName == "item")
                        {
                            inItenFlag = true;
                            channel    = new Channel();
                        } // End of item
                        // itemタグの中にいる場合
                        else if (inItenFlag)
                        {
                            if (reader.LocalName == "title")
                            {
                                channel.Title = reader.ReadString();
                            } // End of title
                            else if (reader.LocalName == "description")
                            {
                                channel.Description = reader.ReadString();
                                Match match = listenerRegex.Match(channel.Description);
                                if (match.Success == true)
                                {
                                    try
                                    {
                                        channel.Listener = int.Parse(match.Groups[2].Value);
                                    }
                                    catch (ArgumentException) {; }
                                    catch (FormatException) {; }
                                    catch (OverflowException) {; }
                                }
                                Match match2 = bitRateRegex.Match(channel.Description);
                                if (match2.Success == true)
                                {
                                    try
                                    {
                                        channel.Bitrate = int.Parse(match2.Groups[2].Value);
                                    }
                                    catch (ArgumentException) {; }
                                    catch (FormatException) {; }
                                    catch (OverflowException) {; }
                                }
                            } // End of description
                            else if (reader.LocalName == "category")
                            {
                                if (channel.Category == string.Empty)
                                {
                                    channel.Category = reader.ReadString();
                                }
                                else
                                {
                                    channel.Category = channel.Category + "," + reader.ReadString();
                                }
                            } // End of category
                            else if (reader.LocalName == "enclosure")
                            {
                                Uri    url    = null;
                                string length = string.Empty;
                                string type   = string.Empty;
                                try
                                {
                                    if (reader.MoveToFirstAttribute())
                                    {
                                        url    = new Uri(reader.GetAttribute("url"));
                                        length = reader.GetAttribute("length");
                                        type   = reader.GetAttribute("type");
                                    }
                                    if (length == null)
                                    {
                                        length = string.Empty;
                                    }
                                    if (type == null)
                                    {
                                        type = string.Empty;
                                    }

                                    // Enclosureタグの数だけ、 Enclosure一時リストにEnclosureの内容を追加していく
                                    Enclosure item = new Enclosure(url, length, type);
                                    if (item.IsPodcast())
                                    {
                                        list.Add(item);
                                    }
                                }
                                catch (UriFormatException) {; }
                            } // End of enclosure
                        }
                    }
                    else if ((reader.NodeType == XmlNodeType.EndElement) && (reader.LocalName == "item"))
                    {
                        inItenFlag = false;
                        if (channel != null)
                        {
                            channel.DislpayFormat = this.Setting.DisplayFormat;

                            // Enclosureの要素の数だけ、Channelの複製を作る
                            if (list.Count != 0)
                            {
                                foreach (Enclosure enclosure in list)
                                {
                                    Channel channel2 = (Channel)channel.Clone();
                                    channel2.PlayUrl = enclosure.Url;
                                    channel2.Length  = enclosure.Length;
                                    channel2.Type    = enclosure.Type;
                                    channels.Add(channel2);
                                    OnChannelAdded(channel2);
                                }
                            }
                        }

                        // Enclosure一時リストをクリア
                        list.Clear();

                        channel = null;
                    }
                } // End of itemタグの中にいる場合
            }
            finally
            {
                channelsMatchesToFilterCache   = null;
                channelsUnmatchesToFilterCache = null;
                if (reader != null)
                {
                    reader.Close();
                }
                if (input != null)
                {
                    input.Close();
                }
            }
        }