Beispiel #1
0
 public TItemCollection(TItemCollection items)
 {
     foreach (TItem ItemItem in items)
     {
         this.Add(new TItem(ItemItem));
     }
 }
Beispiel #2
0
 public TRssChannel(TRssChannel rssChannel) : this()
 {
     Name          = rssChannel.Name;
     Title         = rssChannel.Title;
     Link          = new Uri(rssChannel.Link.ToString());
     Description   = rssChannel.Description;
     Category      = rssChannel.Category;
     Language      = rssChannel.Language;
     Copyright     = rssChannel.Copyright;
     LastBuildDate = rssChannel.LastBuildDate;
     Image         = new TRssChannelImage(rssChannel.Image);
     RssItems      = new TRssItemCollection(rssChannel.RssItems);
 }
Beispiel #3
0
 public TRssChannel(XElement rssChannel) : this()
 {
     Name          = "";
     Title         = rssChannel.SafeReadElementValue <string>("title", "");
     Link          = new Uri(rssChannel.SafeReadElementValue <string>("link", "http://127.0.0.1"));
     Description   = rssChannel.SafeReadElementValue <string>("description", "");
     Category      = rssChannel.SafeReadElementValue <string>("category", "");
     Language      = rssChannel.SafeReadElementValue <string>("language", "");
     Copyright     = rssChannel.SafeReadElementValue <string>("copyright", "");
     LastBuildDate = rssChannel.SafeReadElementValue <DateTime>("lastBuildDate", DateTime.MinValue);
     Image         = new TRssChannelImage(rssChannel.SafeReadElement("image"));
     RssItems      = new TRssItemCollection(rssChannel.Elements("item"));
 }
Beispiel #4
0
 public TRssChannel()
 {
     Name          = "";
     Title         = "";
     Link          = new Uri("http://127.0.0.1");
     Description   = "";
     Category      = "";
     Language      = "";
     Copyright     = "";
     LastBuildDate = DateTime.MinValue;
     Image         = new TChannelImage();
     RssItems      = new TItemCollection();
 }
Beispiel #5
0
 public TChannel()
 {
     Name            = "";
     Title           = "";
     Link            = new TUri();
     Description     = "";
     Category        = "";
     Language        = "";
     Copyright       = "";
     LastBuildDate   = DateTime.MinValue;
     Picture         = new TChannelImage();
     Items           = new TItemCollection();
     ChannelEncoding = null;
 }
Beispiel #6
0
 public TChannel(TChannel channel)
     : this()
 {
     Name            = channel.Name;
     Title           = channel.Title;
     Link            = new TUri(channel.Link);
     Description     = channel.Description;
     Category        = channel.Category;
     Language        = channel.Language;
     Copyright       = channel.Copyright;
     LastBuildDate   = channel.LastBuildDate;
     Picture         = new TChannelImage(channel.Picture);
     Items           = new TItemCollection(channel.Items);
     ChannelEncoding = channel.ChannelEncoding;
 }
Beispiel #7
0
 public TChannel(XElement channel)
     : this()
 {
     Title = channel.SafeReadElementValue <string>("title", "");
     Trace.WriteLine(string.Format("Building TChannel {0}...", Title));
     Link        = new TUri(channel.SafeReadElementValue <string>("link", ""));
     Description = channel.SafeReadElementValue <string>("description", "");
     if (channel.Elements().Any(x => x.Name == "category"))
     {
         Category = channel.SafeReadElementValue <string>("category", "");
     }
     if (channel.Elements().Any(x => x.Name == "language"))
     {
         Language = channel.SafeReadElementValue <string>("language", "");
     }
     if (channel.Elements().Any(x => x.Name == "copyright"))
     {
         Copyright = channel.SafeReadElementValue <string>("copyright", "");
     }
     LastBuildDate = channel.SafeReadElementValue <DateTime>("lastbuilddate", DateTime.MinValue);
     if (channel.Elements().Any(x => x.Name == "image"))
     {
         Picture = new TChannelImage(channel.SafeReadElement("image"));
     }
     if (channel.Elements().Any(x => x.Name == "items"))
     {
         Trace.WriteLine(string.Format("  Reading {0} item(s)", channel.Element("items").Elements().Count(x => x.Name == "item")));
         Items = new TItemCollection(channel.SafeReadElement("items"));
     }
     if (channel.Attributes().Any(x => x.Name == "encoding"))
     {
         string EncodingValue = channel.SafeReadAttribute <string>("encoding", DefaultChannelEncoding);
         if (EncodingValue != "")
         {
             ChannelEncoding = Encoding.GetEncoding(EncodingValue);
         }
     }
     if (channel.Attributes().Any(x => x.Name == "name"))
     {
         Name = channel.SafeReadAttribute <string>("name", "");
     }
     else
     {
         Name = Title;
     }
     Trace.WriteLine(string.Format("  {0}", this.ToString()));
     Trace.WriteLine("Done.");
 }
Beispiel #8
0
        public TRssItemCollection DownloadItems()
        {
            WebClient CurrentClient = new WebClient();

            CurrentClient.Headers.Add("user-agent", "RssWatcher/1.0");
            string    XmlString = Encoding.Default.GetString(Encoding.GetEncoding("ISO-8859-1").GetBytes(new StreamReader(CurrentClient.OpenRead(Link), Encoding.GetEncoding("ISO-8859-1")).ReadToEnd()));
            XDocument SourceXml;

            try {
                SourceXml = XDocument.Parse(XmlString, LoadOptions.PreserveWhitespace);
                RssItems  = new TRssItemCollection(SourceXml.Element("rss").Element("channel").Elements("item"));
            } catch {
                RssItems = new TRssItemCollection();
            }

            if (DownloadCompleted != null)
            {
                DownloadCompleted(this, EventArgs.Empty);
            }
            return(RssItems);
        }
Beispiel #9
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.");
            }
        }