Ejemplo n.º 1
0
 public TChannelImage(TChannelImage image)
 {
     Title  = image.Title;
     Url    = new TUri(image.Url);
     Width  = image.Width;
     Height = image.Height;
 }
Ejemplo n.º 2
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();
 }
Ejemplo n.º 3
0
 public TChannel()
 {
     Name            = "";
     Title           = "";
     Link            = new TUri();
     Description     = "";
     Category        = "";
     Language        = "";
     Copyright       = "";
     LastBuildDate   = DateTime.MinValue;
     Picture         = new TChannelImage();
     Items           = new TItemCollection();
     ChannelEncoding = null;
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
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.");
 }
Ejemplo n.º 6
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.");
            }
        }