示例#1
0
        private void Channel_DownloadCompleted(object sender, ItemsDownloadedEventArgs e)
        {
            TChannel CurrentChannel = sender as TChannel;

            CurrentChannel.DownloadCompleted -= new EventHandler <ItemsDownloadedEventArgs>(Channel_DownloadCompleted);
            Save();
            if (DownloadCompleted != null)
            {
                ItemsDownloadedEventArgs Args = new ItemsDownloadedEventArgs(e.RawData, e.ResponseHeaders, e.Data);
                DownloadCompleted(this, Args);
            }
        }
示例#2
0
        public void DownloadItems()
        {
            try {
                Trace.WriteLine(string.Format("Downloading items for channel {0} ...", Title));
                Trace.Indent();
                WebClient CurrentClient = new WebClient();

                CurrentClient.Headers.Add("user-agent", "RssWatcher/1.0");
                byte[] Data = CurrentClient.DownloadData(Link.Value);

                #region Discover the encoding
                if (ChannelEncoding == null || string.IsNullOrWhiteSpace(ChannelEncoding.WebName))
                {
                    string    ContentType      = CurrentClient.ResponseHeaders.Get("Content-Type");
                    string[]  ContentTypeItems = ContentType.Split(';');
                    SplitArgs oArgs            = new SplitArgs(ContentTypeItems);
                    ChannelEncoding = Encoding.GetEncoding(oArgs.GetValue <string>("charset", DefaultChannelEncoding));
                }
                Trace.WriteLine(string.Format("Encoding for {0} : {1}", Name, ChannelEncoding.WebName));
                #endregion Discover the encoding

                #region If content is gzipped, then decompress
                string ContentEncoding = CurrentClient.ResponseHeaders.Get("Content-Encoding");
                if (!string.IsNullOrWhiteSpace(ContentEncoding) && ContentEncoding.ToLower() == "gzip")
                {
                    using (MemoryStream TempStream = new MemoryStream()) {
                        using (GZipStream CompressedStream = new GZipStream(new MemoryStream(Data), CompressionMode.Decompress)) {
                            CompressedStream.CopyTo(TempStream);
                        }
                        TempStream.Flush();
                        Data = new byte[TempStream.Length];
                        Buffer.BlockCopy(TempStream.GetBuffer(), 0, Data, 0, (int)TempStream.Length);
                    }
                }
                #endregion If content is gzipped, then decompress

                #region Read and parse the data
                XDocument SourceXml;
                try {
                    #region Skip the BOM code if it exists
                    string TempXml = ChannelEncoding.GetString(Data);
                    if (TempXml.IndexOf('<') > 0)
                    {
                        TempXml = TempXml.Substring(TempXml.IndexOf("<"));
                    }
                    #endregion Skip the BOM code if it exists
                    SourceXml = XDocument.Parse(TempXml);
                    XElement        XChannel          = SourceXml.Element("rss").Element("channel");
                    TItemCollection TempItems         = new TItemCollection(XChannel.Elements("item"));
                    DateTime        TempLastBuildDate = LastBuildDate;
                    Trace.WriteLine(string.Format("Found {0} item(s)", TempItems.Count));
                    List <TItem> NewItems = TempItems.Where(i => i.PubDate > LastBuildDate).ToList();
                    Trace.WriteLine(string.Format("{0} new item(s)", NewItems.Count));
                    foreach (TItem ItemItem in NewItems)
                    {
                        if (ItemItem.PubDate > TempLastBuildDate)
                        {
                            TempLastBuildDate = ItemItem.PubDate;
                        }
                        Items.Add(ItemItem);
                    }
                    LastBuildDate = TempLastBuildDate;

                    Description = XChannel.SafeReadElementValue <string>("description", "");
                    if (XChannel.Elements().Any(x => x.Name == "image"))
                    {
                        Picture = new TChannelImage(XChannel.SafeReadElement("image"));
                    }
                    if (XChannel.Elements().Any(x => x.Name == "category"))
                    {
                        Category = XChannel.SafeReadElementValue <string>("category", "");
                    }
                    if (XChannel.Elements().Any(x => x.Name == "language"))
                    {
                        Language = XChannel.SafeReadElementValue <string>("language", "");
                    }
                    if (XChannel.Elements().Any(x => x.Name == "copyright"))
                    {
                        Copyright = XChannel.SafeReadElementValue <string>("copyright", "");
                    }
                    Title = XChannel.SafeReadElementValue <string>("title", "");
                } catch (Exception ex) {
                    Trace.WriteLine(string.Format("Error while reading items for channel {0} : {1}", Name, ex.Message), Severity.Error);
                }
                #endregion Read and parse the data

                if (DownloadCompleted != null)
                {
                    ItemsDownloadedEventArgs Args = new ItemsDownloadedEventArgs(Data, CurrentClient.ResponseHeaders, Encoding.UTF8.GetString(Data));
                    DownloadCompleted(this, Args);
                }

                return;
            } finally {
                Trace.Unindent();
                Trace.WriteLine("Done.");
            }
        }