예제 #1
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();
                }
            }
        }
예제 #2
0
        /// <summary>
        /// ヘッドラインをネットから取得する
        /// </summary>
        public virtual void FetchHeadline()
        {
            if (setting.RssUrl == null)
            {
                return;
            }

            // 時刻をセットする
            lastCheckTime = DateTime.Now;

            WebStream     st     = null;
            XmlTextReader reader = null;

            try
            {
                // 番組のリスト
                ArrayList alChannels = new ArrayList();

                // チャンネル
                Channel channel = null;
                // itemタグの中にいるか
                bool inItemFlag = false;

                // Enclosureの一時リスト
                ArrayList alTempEnclosure = new ArrayList();

                st = PocketLadioUtility.GetWebStream(setting.RssUrl);

                reader = new XmlTextReader(st);

                // 解析したヘッドラインの個数
                int analyzedCount = 0;

                OnHeadlineAnalyze(new HeadlineAnalyzeEventArgs(0, HeadlineAnalyzeEventArgs.UNKNOWN_WHOLE_COUNT));

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.LocalName == "item")
                        {
                            inItemFlag = true;
                            channel    = new Channel(this);
                        } // End of item
                        // itemタグの中にいる場合
                        else if (inItemFlag == true)
                        {
                            if (reader.LocalName == "title")
                            {
                                channel.Title = reader.ReadString();
                            } // End of title
                            else if (reader.LocalName == "description")
                            {
                                channel.Description = reader.ReadString();
                            } // End of description
                            else if (reader.LocalName == "link")
                            {
                                try
                                {
                                    channel.Link = new Uri(reader.ReadString());
                                }
                                catch (UriFormatException)
                                {
                                    ;
                                }
                            } // End of link
                            else if (reader.LocalName == "pubDate")
                            {
                                channel.SetDate(reader.ReadString());
                            } // End of pubDate
                            else if (reader.LocalName == "category")
                            {
                                channel.Category = reader.ReadString();
                            } // End of category
                            else if (reader.LocalName == "author")
                            {
                                channel.Author = reader.ReadString();
                            } // End of author
                            else if (reader.LocalName == "guid")
                            {
                                try
                                {
                                    channel.Link = new Uri(reader.ReadString());
                                }
                                catch (UriFormatException)
                                {
                                    ;
                                }
                            } // End of guid
                            else if (reader.LocalName == "enclosure")
                            {
                                Uri    enclosureUrl    = null;
                                string enclosureLength = string.Empty;
                                string enclosureType   = string.Empty;

                                try
                                {
                                    if (reader.MoveToFirstAttribute())
                                    {
                                        enclosureUrl    = new Uri(reader.GetAttribute("url"));
                                        enclosureLength = reader.GetAttribute("length");
                                        enclosureType   = reader.GetAttribute("type");
                                    }

                                    if (enclosureLength == null)
                                    {
                                        enclosureLength = string.Empty;
                                    }
                                    if (enclosureType == null)
                                    {
                                        enclosureType = string.Empty;
                                    }

                                    // Enclosureタグの数だけ、 Enclosure一時リストにEnclosureの内容を追加していく
                                    Enclosure enclosure = new Enclosure(enclosureUrl, enclosureLength, enclosureType);
                                    if (enclosure.IsPodcast() == true)
                                    {
                                        alTempEnclosure.Add(enclosure);
                                    }
                                }
                                catch (UriFormatException)
                                {
                                    ;
                                }
                            } // End of enclosure
                        }     // End of itemタグの中にいる場合
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.LocalName == "item")
                        {
                            inItemFlag = false;
                            // Enclosureの要素の数だけ、Channelの複製を作る
                            if (alTempEnclosure.Count != 0)
                            {
                                foreach (Enclosure enclosure in alTempEnclosure)
                                {
                                    Channel clonedChannel = (Channel)channel.Clone(this);
                                    clonedChannel.Url    = enclosure.Url;
                                    clonedChannel.Length = enclosure.Length;
                                    clonedChannel.Type   = enclosure.Type;
                                    alChannels.Add(clonedChannel);
                                    OnHeadlineAnalyzing(new HeadlineAnalyzeEventArgs(++analyzedCount, HeadlineAnalyzeEventArgs.UNKNOWN_WHOLE_COUNT));
                                }
                            }

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

                OnHeadlineAnalyzed(new HeadlineAnalyzeEventArgs(analyzedCount, analyzedCount));

                channels = (Channel[])alChannels.ToArray(typeof(Channel));
            }
            finally
            {
                if (st != null)
                {
                    st.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }
예제 #3
0
        protected override void FetchHeadline()
        {
            channels.Clear();
            // フィルターのクリア
            channelsMatchesToFilterCache   = null;
            channelsUnmatchesToFilterCache = null;

            Stream    st     = null;
            XmlReader reader = null;

            try
            {
                // 番組
                Channel channel = null;
                // itemタグの中にいるか
                bool inItemFlag = false;
                // Enclosureの一時リスト
                List <Enclosure> enclosuresTemp = new List <Enclosure>();

                st     = connectionSetting.CreateStream(Setting.RssUrl);
                reader = new XmlTextReader(st);

                while (reader.Read())
                {
                    if (fetchCancel == true)
                    {
                        return;
                    }

                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.LocalName == "item")
                        {
                            inItemFlag = true;
                            channel    = new Channel();
                        } // End of item
                        // itemタグの中にいる場合
                        else if (inItemFlag == true)
                        {
                            if (reader.LocalName == "title")
                            {
                                // "itunes::*"などで上書きされるのを防ぐ
                                if (channel.Title == string.Empty || reader.Name == "title")
                                {
                                    channel.Title = reader.ReadString();
                                }
                            } // End of title
                            else if (reader.LocalName == "description")
                            {
                                // "itunes::*"などで上書きされるのを防ぐ
                                if (channel.Description == string.Empty || reader.Name == "description")
                                {
                                    channel.Description = reader.ReadString();
                                }
                            } // End of description
                            else if (reader.LocalName == "link")
                            {
                                // "itunes::*"などで上書きされるのを防ぐ
                                if (channel.WebSiteUrl == null || reader.Name == "link")
                                {
                                    try
                                    {
                                        channel.WebSiteUrl = new Uri(reader.ReadString().Trim());
                                    }
                                    catch (UriFormatException) {; }
                                }
                            } // End of link
                            else if (reader.LocalName == "pubDate")
                            {
                                // "itunes::*"などで上書きされるのを防ぐ
                                if (channel.Date == null || reader.Name == "pubDate")
                                {
                                    string dateString = reader.ReadString();
                                    try
                                    {
                                        channel.Date = DateTime.ParseExact(dateString, "ddd, d MMM yyyy HH':'mm':'ss zzz",
                                                                           System.Globalization.DateTimeFormatInfo.InvariantInfo,
                                                                           System.Globalization.DateTimeStyles.None);
                                    }
                                    catch (FormatException)
                                    {
                                        try
                                        {
                                            channel.Date = DateTime.Parse(dateString,
                                                                          System.Globalization.DateTimeFormatInfo.InvariantInfo,
                                                                          System.Globalization.DateTimeStyles.None);
                                        }
                                        catch (FormatException)
                                        {
                                            channel.Date = DateTime.Now;
                                        }
                                    }
                                }
                            } // End of pubDate
                            else if (reader.LocalName == "category")
                            {
                                // "itunes::*"などで上書きされるのを防ぐ
                                if (channel.Category == string.Empty || reader.Name == "category")
                                {
                                    channel.Category = reader.ReadString();
                                }
                            } // End of category
                            else if (reader.LocalName == "author")
                            {
                                // "itunes::*"などで上書きされるのを防ぐ
                                if (channel.Author == string.Empty || reader.Name == "author")
                                {
                                    channel.Author = reader.ReadString();
                                }
                            } // End of author
                            else if (reader.LocalName == "guid")
                            {
                                // "itunes::*"などで上書きされるのを防ぐ
                                if (channel.WebSiteUrl == null || reader.Name == "guid")
                                {
                                    try
                                    {
                                        channel.WebSiteUrl = new Uri(reader.ReadString());
                                    }
                                    catch (UriFormatException) {; }
                                }
                            } // End of guid
                            else if (reader.LocalName == "enclosure")
                            {
                                Uri    enclosureUrl    = null;
                                string enclosureLength = string.Empty;
                                string enclosureType   = string.Empty;

                                try
                                {
                                    if (reader.MoveToFirstAttribute())
                                    {
                                        enclosureUrl    = new Uri(reader.GetAttribute("url"));
                                        enclosureLength = reader.GetAttribute("length");
                                        enclosureType   = reader.GetAttribute("type");
                                    }

                                    if (enclosureLength == null)
                                    {
                                        enclosureLength = string.Empty;
                                    }
                                    if (enclosureType == null)
                                    {
                                        enclosureType = string.Empty;
                                    }

                                    // Enclosureタグの数だけ、 Enclosure一時リストにEnclosureの内容を追加していく
                                    Enclosure enclosure = new Enclosure(enclosureUrl, enclosureLength, enclosureType);
                                    if (enclosure.IsPodcast() == true)
                                    {
                                        enclosuresTemp.Add(enclosure);
                                    }
                                }
                                catch (UriFormatException) {; }
                            } // End of enclosure
                        }     // End of itemタグの中にいる場合
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.LocalName == "item")
                        {
                            inItemFlag = false;
                            if (channel != null)
                            {
                                channel.DislpayFormat = Setting.DisplayFormat;
                                // Enclosureの要素の数だけ、Channelの複製を作る
                                if (enclosuresTemp.Count != 0)
                                {
                                    foreach (Enclosure enclosure in enclosuresTemp)
                                    {
                                        Channel clonedChannel = (Channel)channel.Clone();
                                        clonedChannel.PlayUrl = enclosure.Url;
                                        clonedChannel.Length  = enclosure.Length;
                                        clonedChannel.Type    = enclosure.Type;
                                        channels.Add(clonedChannel);
                                        OnChannelAdded(clonedChannel);
                                    }
                                }
                            }

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

                            channel = null;
                        }
                    }
                }
            }
            finally
            {
                // フィルターのクリア

                /*
                 * 本メソッドの先頭でもフィルターキャッシュをクリアしているが、本メソッドの実行中にフィルターを
                 * 使用した場合に、フィルターキャッシュの整合性がとれなくなるため、本メソッドの終了時に
                 * フィルターのキャッシュを削除してしまう。
                 */
                channelsMatchesToFilterCache   = null;
                channelsUnmatchesToFilterCache = null;

                if (reader != null)
                {
                    reader.Close();
                }
                if (st != null)
                {
                    st.Close();
                }
            }
        }