예제 #1
0
        public void FormatSampleFeedEmpty()
        {
            var feed = new Rss20Feed
            {
                Channel = new Rss20Channel
                {
                    Items = new List <Rss20Item>
                    {
                        new Rss20Item(),
                    },
                },
            };

            var tryFormatResult = Rss20FeedFormatter.TryFormatRss20Feed(feed, out var document);

            Assert.True(tryFormatResult);

            var targetEncoding    = Encoding.UTF8;
            var xmlWriterSettings = new XmlWriterSettings
            {
                Encoding = targetEncoding,
                Indent   = true,
            };

            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream, targetEncoding))
                    using (var xmlWriter = XmlWriter.Create(streamWriter, xmlWriterSettings))
                    {
                        document.WriteTo(xmlWriter);
                        xmlWriter.Flush();

                        var xmlString = targetEncoding.GetString(memoryStream.ToArray());
                        Assert.NotEmpty(xmlString);
                    }
        }
예제 #2
0
        protected virtual void ParseRss20Header(Rss20Feed rssFeed, string xml)
        {
            var document = XDocument.Parse(xml);
            var channel  = document.Root.Element("channel");

            rssFeed.Title       = channel.Element("title").Value;
            rssFeed.Description = channel.Element("description").Value;

            var linkNode = channel.Element("link");

            rssFeed.Link = linkNode == null ? string.Empty : linkNode.Value;

            var dateTimeNode = (from dateSelector in channel.Elements("pubDate")
                                select dateSelector).FirstOrDefault();

            if (dateTimeNode == null)
            {
                rssFeed.LastUpdated = DateTime.UtcNow;
            }
            else
            {
                DateTime timeOut;
                DateTime.TryParse(dateTimeNode.Value, out timeOut);
                rssFeed.LastUpdated = timeOut.ToUniversalTime();
            }

            var generatorNode = channel.Element("generator");

            rssFeed.Generator = generatorNode == null ? string.Empty : generatorNode.Value;

            var languageNode = channel.Element("language");

            rssFeed.Language = languageNode == null ? string.Empty : languageNode.Value;
        }
예제 #3
0
        private IFeed CreateFeed(FeedType feedtype, string feedxml)
        {
            try
            {
                IFeed returnFeed;
                if (feedtype == FeedType.Atom10)
                {
                    returnFeed = new Atom10Feed(feedxml);
                }
                else
                {
                    returnFeed = new Rss20Feed(feedxml);
                }

                try
                {
                    this.parser.ParseFeed(returnFeed, feedxml);
                }
                catch (System.Xml.XmlException ex)
                {
                    throw new InvalidFeedXmlException("The xml for feed is invalid", ex);
                }

                return(returnFeed);
            }
            catch (XmlException ex)
            {
                throw new InvalidFeedXmlException("Invalid XML for feed", ex);
            }
        }
예제 #4
0
        public static bool TryParseRss20Feed(XDocument document, out Rss20Feed parsedFeed, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            parsedFeed = default;

            var rssElement = document?.Element("rss");

            if (rssElement == null)
            {
                return(false);
            }

            var rssVersion = rssElement.Attribute("version")?.Value;

            if (!Rss20Constants.RecognizedVersions.Contains(rssVersion))
            {
                return(false);
            }

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForRss;
            }

            if (!TryParseRss20Channel(rssElement.Element("channel"), extensionManifestDirectory, out var parsedChannel))
            {
                return(false);
            }

            parsedFeed         = new Rss20Feed();
            parsedFeed.Channel = parsedChannel;
            return(true);
        }
예제 #5
0
        public override BaseFeed Parse(string feedXml, XDocument feedDoc)
        {
            var       rss     = feedDoc.Root;
            var       channel = rss.GetElement("channel");
            Rss20Feed feed    = new Rss20Feed(feedXml, channel);

            return(feed);
        }
예제 #6
0
        private void ParseRss20Items(Rss20Feed rssFeed, string xml)
        {
            var document      = XDocument.Parse(xml);
            var feedItemNodes = document.Root.Element("channel").Elements("item");

            foreach (var item in feedItemNodes)
            {
                rssFeed.Items.Add(ParseRss20SingleItem(item));
            }
        }
예제 #7
0
        protected override void TestFeedParseCorrectness(IFeedKnownValueTest testcase, IFeed resultantItem)
        {
            Rss20Feed             rssItem = (Rss20Feed)resultantItem;
            RssFeedKnownValueTest rssTest = (RssFeedKnownValueTest)testcase;

            base.TestFeedParseCorrectness(testcase, resultantItem);

            //Assert that the two descriptions are equivalent
            Assert.That(rssItem.Description == rssTest.Description);

            //Assert that the two languages are equivalent
            Assert.That(rssItem.Language == rssTest.Language);
        }
예제 #8
0
        private void ParseRss20Header(Rss20Feed rssFeed, string xml)
        {
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xml);
            var titleNode = xmlDoc.SelectSingleNode("/rss/channel/title");

            rssFeed.Title = titleNode.InnerText;

            var descriptionNode = xmlDoc.SelectSingleNode("/rss/channel/description");

            rssFeed.Description = descriptionNode == null ? string.Empty : descriptionNode.InnerText;

            var linkNode = xmlDoc.SelectSingleNode("/rss/channel/link");

            rssFeed.Link = linkNode == null ? string.Empty : linkNode.InnerText;

            var dateTimeNode = xmlDoc.SelectSingleNode("//pubDate[1]");

            if (dateTimeNode == null) //We have to have a date, so we'll use the date/time when we polled the RSS feed as the default.
            {
                rssFeed.LastUpdated = DateTime.UtcNow;
            }
            else
            {
                DateTime timeOut;
                DateTime.TryParse(dateTimeNode.InnerText, out timeOut);
                rssFeed.LastUpdated = timeOut.ToUniversalTime();
            }

            var generatorNode = xmlDoc.SelectSingleNode("/rss/channel/generator");

            rssFeed.Generator = generatorNode == null ? string.Empty : generatorNode.InnerText;

            var languageNode = xmlDoc.SelectSingleNode("/rss/channel/language");

            rssFeed.Language = languageNode == null ? string.Empty : languageNode.InnerText;
        }
예제 #9
0
        public static bool TryFormatRss20Feed(Rss20Feed feed, out XDocument document, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            document = default;

            if (feed == null)
            {
                return(false);
            }

            document = new XDocument();

            var rssElement = new XElement("rss", new XAttribute("version", Rss20Constants.Version));

            document.Add(rssElement);

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForRss;
            }

            var namespaceAliases = new XNamespaceAliasSet();

            if (!TryFormatRss20Channel(feed.Channel, namespaceAliases, extensionManifestDirectory, out var channelElement))
            {
                return(false);
            }

            rssElement.Add(channelElement);

            foreach (var namespaceAlias in namespaceAliases.OrderBy(x => x.Name.LocalName))
            {
                rssElement.Add(namespaceAlias);
            }

            return(true);
        }
예제 #10
0
        public void FormatSampleFeed()
        {
            var feed = new Rss20Feed
            {
                Channel = new Rss20Channel
                {
                    Generator  = "My Awesome Generator 3000",
                    Title      = "This is my feed title",
                    Categories = new List <Rss20Category> {
                        new Rss20Category {
                            Name = "Alpha"
                        }, new Rss20Category {
                            Name = "Beta"
                        }, new Rss20Category {
                            Name = "Gama", Domain = "https://example.org/category-domain"
                        },
                    },
                    Copyright      = "Copyright 2019, All rights reserved",
                    WebMaster      = "[email protected] (John Doe)",
                    ManagingEditor = "[email protected] (Jane Doe)",
                    Language       = "en-US",
                    PubDate        = new DateTimeOffset(2019, 01, 01, 12, 30, 00, TimeSpan.FromHours(2)),
                    Cloud          = new Rss20Cloud
                    {
                        Domain            = "https://example.org/cloud",
                        RegisterProcedure = "abc",
                        Protocol          = "def",
                        Path = "/ghi",
                        Port = "9000",
                    },
                    Description   = "Description of my feed",
                    LastBuildDate = new DateTimeOffset(2019, 01, 01, 4, 30, 00, TimeSpan.FromHours(0)),
                    Ttl           = TimeSpan.FromHours(3),
                    TextInput     = new Rss20TextInput
                    {
                        Name = "TextInput", Title = "My text input", Description = "Description of my text input", Link = "https://example.org/my-text-input",
                    },
                    SkipHours = new List <int> {
                        1, 2, 3
                    },
                    Docs     = "https://example.org/docs",
                    SkipDays = new List <DayOfWeek> {
                        DayOfWeek.Friday, DayOfWeek.Monday,
                    },
                    Link  = "https://example.org/channel",
                    Image = new Rss20Image
                    {
                        Title       = "My channel image",
                        Width       = 100,
                        Height      = 50,
                        Description = "My channel image description",
                        Link        = "https://example.org/channel",
                        Url         = "https://example.org/image.png",
                    },
                    Items = new List <Rss20Item>
                    {
                        new Rss20Item
                        {
                            Title      = "My awesome article",
                            Categories = new List <Rss20Category> {
                                new Rss20Category {
                                    Name = "Delta", Domain = "https://example.org/category-domain"
                                },
                            },
                            PubDate     = new DateTimeOffset(2019, 01, 01, 5, 30, 00, TimeSpan.FromHours(0)),
                            Description = "My awesome article description...",
                            Link        = "https://example.org/article",
                            Author      = "[email protected] (John Doe)",
                            Comments    = "https://example.org/comments",
                            Guid        = new Rss20Guid {
                                Value = "https://example.org/article-permalink", IsPermaLink = true,
                            },
                            Source = new Rss20Source {
                                Name = "Article source X", Url = "https://example.org/article-source-x",
                            },
                            Enclosures = new List <Rss20Enclosure> {
                                new Rss20Enclosure {
                                    Url = "https://example.org/enclosure-video.mp4", Length = "3000", Type = "video/mp4",
                                },
                            },
                        },
                    },
                },
            };

            var tryFormatResult = Rss20FeedFormatter.TryFormatRss20Feed(feed, out var document);

            Assert.True(tryFormatResult);

            var targetEncoding    = Encoding.UTF8;
            var xmlWriterSettings = new XmlWriterSettings
            {
                Encoding = targetEncoding,
                Indent   = true,
            };

            using (var memoryStream = new MemoryStream())
                using (var streamWriter = new StreamWriter(memoryStream, targetEncoding))
                    using (var xmlWriter = XmlWriter.Create(streamWriter, xmlWriterSettings))
                    {
                        document.WriteTo(xmlWriter);
                        xmlWriter.Flush();

                        var xmlString = targetEncoding.GetString(memoryStream.ToArray());
                        Assert.NotEmpty(xmlString);
                    }
        }
        private void ParseRss20Header(Rss20Feed rssFeed, string xml)
        {

            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            var titleNode = xmlDoc.DocumentElement.GetAttributeNode("/rss/channel/title");
            rssFeed.Title = titleNode.InnerText;

            var descriptionNode = xmlDoc.DocumentElement.GetAttributeNode("/rss/channel/description");
            rssFeed.Description = descriptionNode == null ? string.Empty : descriptionNode.InnerText;

            var linkNode = xmlDoc.DocumentElement.GetAttributeNode("/rss/channel/link");
            rssFeed.Link = linkNode == null ? string.Empty : linkNode.InnerText;

            var dateTimeNode = xmlDoc.DocumentElement.GetAttributeNode("//pubDate[1]");
            if (dateTimeNode == null) //We have to have a date, so we'll use the date/time when we polled the RSS feed as the default.
            {
                rssFeed.LastUpdated = DateTime.UtcNow;
            }
            else
            {
                DateTime timeOut;
                DateTime.TryParse(dateTimeNode.InnerText, out timeOut);
                rssFeed.LastUpdated = timeOut.ToUniversalTime();
            }

            var generatorNode = xmlDoc.DocumentElement.GetAttributeNode("/rss/channel/generator");
            rssFeed.Generator = generatorNode == null ? string.Empty : generatorNode.InnerText;

            var languageNode = xmlDoc.DocumentElement.GetAttributeNode("/rss/channel/language");
            rssFeed.Language = languageNode == null ? string.Empty : languageNode.InnerText;
        }
예제 #12
0
 private void ParseRss20Items(Rss20Feed rssFeed, string xml)
 {
     var document = XDocument.Parse(xml);
     var feedItemNodes = document.Root.Element("channel").Elements("item");
     foreach (var item in feedItemNodes)
     {
         rssFeed.Items.Add(ParseRss20SingleItem(item));
     }
 }
예제 #13
0
        protected virtual void ParseRss20Header(Rss20Feed rssFeed, string xml)
        {
            var document = XDocument.Parse(xml);
            var channel = document.Root.Element("channel");

            rssFeed.Title = channel.Element("title").Value;
            rssFeed.Description = channel.Element("description").Value;

            var linkNode = channel.Element("link");
            rssFeed.Link = linkNode == null ? string.Empty : linkNode.Value;

            var dateTimeNode = (from dateSelector in channel.Elements("pubDate")
                                select dateSelector).FirstOrDefault();
            if (dateTimeNode == null)
            {
                rssFeed.LastUpdated = DateTime.UtcNow;
            }
            else
            {
                DateTime timeOut;
                DateTime.TryParse(dateTimeNode.Value, out timeOut);
                rssFeed.LastUpdated = timeOut.ToUniversalTime();
            }

            var generatorNode = channel.Element("generator");
            rssFeed.Generator = generatorNode == null ? string.Empty : generatorNode.Value;

            var languageNode = channel.Element("language");
            rssFeed.Language = languageNode == null ? string.Empty : languageNode.Value;
        }