コード例 #1
0
 private ISyndicationContent CreateCommentsContent(ISyndicationLink link)
 {
     return(new SyndicationContent(link.RelationshipType)
     {
         Value = FormatValue(link.Uri)
     });
 }
コード例 #2
0
        private ISyndicationContent CreateEnclosureContent(ISyndicationLink link)
        {
            var content = new SyndicationContent(RssElementNames.Enclosure);

            //
            // Url
            content.AddAttribute(new SyndicationAttribute(RssElementNames.Url, FormatValue(link.Uri)));

            //
            // Length
            if (link.Length == 0)
            {
                throw new ArgumentException("Enclosure requires length attribute");
            }

            content.AddAttribute(new SyndicationAttribute(RssConstants.Length, FormatValue(link.Length)));

            //
            // MediaType
            if (string.IsNullOrEmpty(link.MediaType))
            {
                throw new ArgumentNullException("Enclosure requires a MediaType");
            }

            content.AddAttribute(new SyndicationAttribute(RssConstants.Type, link.MediaType));
            return(content);
        }
コード例 #3
0
        public virtual ISyndicationContent CreateContent(ISyndicationLink link)
        {
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

            if (link.Uri == null)
            {
                throw new ArgumentNullException("Invalid link uri");
            }

            switch (link.RelationshipType)
            {
            case RssElementNames.Enclosure:
                return(CreateEnclosureContent(link));

            case RssElementNames.Comments:
                return(CreateCommentsContent(link));

            case RssElementNames.Source:
                return(CreateSourceContent(link));

            default:
                return(CreateLinkContent(link));
            }
        }
コード例 #4
0
        private ISyndicationContent CreateFromSourceLink(ISyndicationLink link)
        {
            //
            // source
            var result = new SyndicationContent(AtomElementNames.Source);

            //
            // title
            if (!string.IsNullOrEmpty(link.Title))
            {
                result.AddField(new SyndicationContent(AtomElementNames.Title, link.Title));
            }

            //
            // link
            result.AddField(CreateFromLink(new SyndicationLink(link.Uri)
            {
                MediaType = link.MediaType,
                Length    = link.Length
            }));

            //
            // updated
            if (link.LastUpdated != default(DateTimeOffset))
            {
                result.AddField(new SyndicationContent(AtomElementNames.Updated, FormatValue(link.LastUpdated)));
            }

            return(result);
        }
コード例 #5
0
        private async Task <ISyndicationLink> ReadLink(Rss20FeedReader reader)
        {
            ISyndicationLink link = null;

            link = await reader.ReadLink();

            return(link);
        }
コード例 #6
0
    // Read an RssFeed
    public static async Task CreateRssFeedReaderExample(string filePath)
    {
        // Create an XmlReader
        // Example: ..\tests\TestFeeds\rss20-2items.xml
        using (var xmlReader = XmlReader.Create(filePath, new XmlReaderSettings()
        {
            Async = true
        }))
        {
            // Instantiate an Rss20FeedReader using the XmlReader.
            // This will assign as default an Rss20FeedParser as the parser.
            var feedReader = new RssFeedReader(xmlReader);

            //
            // Read the feed
            while (await feedReader.Read())
            {
                switch (feedReader.ElementType)
                {
                // Read category
                case SyndicationElementType.Category:
                    ISyndicationCategory category = await feedReader.ReadCategory();

                    break;

                // Read Image
                case SyndicationElementType.Image:
                    ISyndicationImage image = await feedReader.ReadImage();

                    break;

                // Read Item
                case SyndicationElementType.Item:
                    ISyndicationItem item = await feedReader.ReadItem();

                    break;

                // Read link
                case SyndicationElementType.Link:
                    ISyndicationLink link = await feedReader.ReadLink();

                    break;

                // Read Person
                case SyndicationElementType.Person:
                    ISyndicationPerson person = await feedReader.ReadPerson();

                    break;

                // Read content
                default:
                    ISyndicationContent content = await feedReader.ReadContent();

                    break;
                }
            }
        }
    }
コード例 #7
0
        public static async Task TestReadFeedElements(XmlReader outerXmlReader)
        {
            using (var xmlReader = outerXmlReader)
            {
                var reader = new RssFeedReader(xmlReader);
                int items  = 0;
                while (await reader.Read())
                {
                    switch (reader.ElementType)
                    {
                    case SyndicationElementType.Person:
                        ISyndicationPerson person = await reader.ReadPerson();

                        Assert.True(person.Email == "John Smith");
                        break;

                    case SyndicationElementType.Link:
                        ISyndicationLink link = await reader.ReadLink();

                        Assert.True(link.Length == 123);
                        Assert.True(link.MediaType == "testType");
                        Assert.True(link.Uri.OriginalString == "http://example.com/");
                        break;

                    case SyndicationElementType.Image:
                        ISyndicationImage image = await reader.ReadImage();

                        Assert.True(image.Title == "Microsoft News");
                        Assert.True(image.Description == "Test description");
                        Assert.True(image.Url.OriginalString == "http://2.bp.blogspot.com/-NA5Jb-64eUg/URx8CSdcj_I/AAAAAAAAAUo/eCx0irI0rq0/s1600/bg_Microsoft_logo3-20120824073001907469-620x349.jpg");
                        break;

                    case SyndicationElementType.Item:
                        items++;
                        ISyndicationItem item = await reader.ReadItem();

                        if (items == 1)
                        {
                            Assert.True(item.Title == "Lorem ipsum 2017-07-06T20:25:00+00:00");
                            Assert.True(item.Description == "Exercitation sit dolore mollit et est eiusmod veniam aute officia veniam ipsum.");
                            Assert.True(item.Links.Count() == 3);
                        }
                        else if (items == 2)
                        {
                            Assert.True(item.Title == "Lorem ipsum 2017-07-06T20:24:00+00:00");
                            Assert.True(item.Description == "Do ipsum dolore veniam minim est cillum aliqua ea.");
                            Assert.True(item.Links.Count() == 3);
                        }

                        break;

                    default:
                        break;
                    }
                }
            }
        }
コード例 #8
0
        private async Task <bool> retrievePosts(Feed feed)
        {
            using (var xmlReader = XmlReader.Create("https://azurecomcdn.azureedge.net/en-us/blog/feed/", new XmlReaderSettings()
            {
                Async = true
            }))
            {
                // Instantiate an Rss20FeedReader using the XmlReader.
                // This will assign as default an Rss20FeedParser as the parser.
                var feedReader = new RssFeedReader(xmlReader);

                //
                // Read the feed
                while (await feedReader.Read())
                {
                    switch (feedReader.ElementType)
                    {
                    // Read category
                    case SyndicationElementType.Category:
                        ISyndicationCategory category = await feedReader.ReadCategory();

                        break;

                    // Read Image
                    case SyndicationElementType.Image:
                        ISyndicationImage image = await feedReader.ReadImage();

                        break;

                    // Read Item
                    case SyndicationElementType.Item:
                        ISyndicationItem item = await feedReader.ReadItem();

                        break;

                    // Read link
                    case SyndicationElementType.Link:
                        ISyndicationLink link = await feedReader.ReadLink();

                        break;

                    // Read Person
                    case SyndicationElementType.Person:
                        ISyndicationPerson person = await feedReader.ReadPerson();

                        break;

                    // Read content
                    default:
                        ISyndicationContent content = await feedReader.ReadContent();

                        break;
                    }
                }
            }
            return(true);
        }
コード例 #9
0
        private static async Task <List <object> > ParseExport(XmlReader xmlReader)
        {
            List <object> result = new List <object>();
            {
                var feedReader = new AtomFeedReader(xmlReader);
                while (await feedReader.Read())
                {
                    switch (feedReader.ElementType)
                    {
                    // Read category
                    case SyndicationElementType.Category:
                        ISyndicationCategory category = await feedReader.ReadCategory();

                        result.Add(category);
                        break;

                    // Read Image
                    case SyndicationElementType.Image:
                        ISyndicationImage image = await feedReader.ReadImage();

                        result.Add(image);
                        break;

                    // Read Item
                    case SyndicationElementType.Item:
                        ISyndicationItem item = await feedReader.ReadItem();

                        result.Add(item);
                        break;

                    // Read link
                    case SyndicationElementType.Link:
                        ISyndicationLink link = await feedReader.ReadLink();

                        result.Add(link);
                        break;

                    // Read Person
                    case SyndicationElementType.Person:
                        ISyndicationPerson person = await feedReader.ReadPerson();

                        result.Add(person);
                        break;

                    // Read content
                    default:
                        ISyndicationContent content = await feedReader.ReadContent();

                        result.Add(content);
                        break;
                    }
                }

                return(result);
            }
        }
コード例 #10
0
        public FeedItem(ISyndicationItem item, DateTimeOffset recent, Uri website, bool truncateDescription, IExecutionContext context)
        {
            Title = item.Title;
            ISyndicationLink firstLink = item.Links.FirstOrDefault(x => x.RelationshipType == RssLinkTypes.Alternate);

            if (firstLink != null)
            {
                Link = firstLink.Uri.IsAbsoluteUri ? firstLink.Uri.AbsoluteUri : new Uri(website, firstLink.Uri).AbsoluteUri;
            }
            else
            {
                Link = item.Id;
            }

            Published = item.Published != default ? item.Published : item.LastUpdated;
            Recent    = Published > recent;
            Links     = item.Links
                        .Where(x => !string.IsNullOrEmpty(x.MediaType))
                        .GroupBy(x => x.MediaType)
                        .Select(x => x.First())
                        .ToDictionary(x => x.MediaType, x => x.Uri.ToString());

            ISyndicationPerson person = item.Contributors.FirstOrDefault(x => x.RelationshipType == "author");

            if (person != null)
            {
                Author = person.Name ?? person.Email;
            }

            Description = item.Description;
            AtomEntry atom = item as AtomEntry;

            if (atom != null && !string.IsNullOrEmpty(atom.Summary))
            {
                Description = atom.Summary;
            }

            if (truncateDescription)
            {
                try
                {
                    // Use the first <p> if one is found
                    HtmlParser    htmlParser   = new HtmlParser();
                    IHtmlDocument htmlDocument = htmlParser.ParseDocument(Description);
                    IElement      element      = htmlDocument.QuerySelector("p");
                    if (element is object && !string.IsNullOrWhiteSpace(element.OuterHtml))
                    {
                        Description = element.OuterHtml;
                    }
                }
                catch (Exception ex)
                {
                    context.LogWarning($"Error parsing HTML description for feed {Title}: {ex.Message}");
                }
            }
        }
コード例 #11
0
        private ISyndicationContent CreateLinkContent(ISyndicationLink link)
        {
            SyndicationContent content;

            if (string.IsNullOrEmpty(link.RelationshipType) ||
                link.RelationshipType == RssLinkTypes.Alternate)
            {
                // Regular <link>
                content = new SyndicationContent(RssElementNames.Link);
            }
            else
            {
                // Custom
                content = new SyndicationContent(link.RelationshipType);
            }

            //
            // title
            if (!string.IsNullOrEmpty(link.Title))
            {
                content.Value = link.Title;
            }

            //
            // url
            string url = FormatValue(link.Uri);

            if (content.Value != null)
            {
                content.AddAttribute(new SyndicationAttribute(RssElementNames.Url, url));
            }
            else
            {
                content.Value = url;
            }

            //
            // Type
            if (!string.IsNullOrEmpty(link.MediaType))
            {
                content.AddAttribute(new SyndicationAttribute(RssConstants.Type, link.MediaType));
            }

            //
            // Lenght
            if (link.Length != 0)
            {
                content.AddAttribute(new SyndicationAttribute(RssConstants.Length, FormatValue(link.Length)));
            }

            return(content);
        }
コード例 #12
0
ファイル: Class1.cs プロジェクト: JaikeMCD/RssReader
    public RssContent()
    {
        using (var xmlReader = XmlReader.Create("C:/Users/Developer/Desktop.brisbane-city-council.rss", new XmlReaderSettings()
        {
            Async = true
        }))
        {
            var feedReader = new RssFeedReader(xmlReader);

            while (await feedReader.Read())
            {
                switch (feedReader.ElementType)
                {
                // Read category
                case SyndicationElementType.Category:
                    ISyndicationCategory category = await feedReader.ReadCategory();

                    break;

                // Read Image
                case SyndicationElementType.Image:
                    ISyndicationImage image = await feedReader.ReadImage();

                    break;

                // Read Item
                case SyndicationElementType.Item:
                    ISyndicationItem item = await feedReader.ReadItem();

                    break;

                // Read link
                case SyndicationElementType.Link:
                    ISyndicationLink link = await feedReader.ReadLink();

                    break;

                // Read Person
                case SyndicationElementType.Person:
                    ISyndicationPerson person = await feedReader.ReadPerson();

                    break;

                // Read content
                default:
                    ISyndicationContent content = await feedReader.ReadContent();

                    break;
                }
            }
        }
    }
コード例 #13
0
        public void AddLink(ISyndicationLink link)
        {
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

            if (_links.IsReadOnly)
            {
                _links = _links.ToList();
            }

            _links.Add(link);
        }
コード例 #14
0
        private ISyndicationContent CreateFromContentLink(ISyndicationLink link)
        {
            //
            // content
            var result = new SyndicationContent(AtomElementNames.Content);

            //
            // src
            result.AddAttribute(new SyndicationAttribute(AtomConstants.Source, FormatValue(link.Uri)));

            //
            // type
            if (!string.IsNullOrEmpty(link.MediaType))
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Type, link.MediaType));
            }

            return(result);
        }
コード例 #15
0
        private async Task ReadWhile()
        {
            using (var xmlReader = XmlReader.Create(@"..\..\..\TestFeeds\rss20.xml", new XmlReaderSettings()
            {
                Async = true
            }))
            {
                var reader = new RssFeedReader(xmlReader);

                while (await reader.Read())
                {
                    switch (reader.ElementType)
                    {
                    case SyndicationElementType.Link:
                        ISyndicationLink link = await reader.ReadLink();

                        break;

                    case SyndicationElementType.Item:
                        ISyndicationItem item = await reader.ReadItem();

                        break;

                    case SyndicationElementType.Person:
                        ISyndicationPerson person = await reader.ReadPerson();

                        break;

                    case SyndicationElementType.Image:
                        ISyndicationImage image = await reader.ReadImage();

                        break;

                    default:
                        ISyndicationContent content = await reader.ReadContent();

                        break;
                    }
                }
            }
        }
コード例 #16
0
        public async Task ReadLink()
        {
            using (XmlReader xmlReader = XmlReader.Create(@"..\..\..\TestFeeds\simpleAtomFeed.xml", new XmlReaderSettings {
                Async = true
            }))
            {
                var           reader = new AtomFeedReader(xmlReader);
                List <string> hrefs  = new List <string>();
                while (await reader.Read())
                {
                    if (reader.ElementType == SyndicationElementType.Link)
                    {
                        ISyndicationLink link = await reader.ReadLink();

                        hrefs.Add(link.Uri.OriginalString);
                    }
                }
                Assert.True(hrefs[0] == "http://example.org/");
                Assert.True(hrefs[1] == "http://example.org/feed.atom");
            }
        }
コード例 #17
0
        private ISyndicationContent CreateFromLink(ISyndicationLink link)
        {
            //
            // link
            var result = new SyndicationContent(AtomElementNames.Link);

            //
            // title
            if (!string.IsNullOrEmpty(link.Title))
            {
                result.AddAttribute(new SyndicationAttribute(AtomElementNames.Title, link.Title));
            }

            //
            // href
            result.AddAttribute(new SyndicationAttribute(AtomConstants.Href, FormatValue(link.Uri)));

            //
            // rel
            if (!string.IsNullOrEmpty(link.RelationshipType))
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Rel, link.RelationshipType));
            }

            //
            // type
            if (!string.IsNullOrEmpty(link.MediaType))
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Type, link.MediaType));
            }

            //
            // length
            if (link.Length > 0)
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Length, FormatValue(link.Length)));
            }

            return(result);
        }
コード例 #18
0
        private ISyndicationContent CreateSourceContent(ISyndicationLink link)
        {
            var content = new SyndicationContent(link.RelationshipType);

            //
            // Url
            string url = FormatValue(link.Uri);

            if (link.Title != url)
            {
                content.AddAttribute(new SyndicationAttribute(RssElementNames.Url, url));
            }

            //
            // Title
            if (!string.IsNullOrEmpty(link.Title))
            {
                content.Value = link.Title;
            }

            return(content);
        }
コード例 #19
0
        public FeedItem(ISyndicationItem item, DateTimeOffset recent, Uri website)
        {
            Title = item.Title;
            ISyndicationLink firstLink = item.Links.FirstOrDefault(x => x.RelationshipType == RssLinkTypes.Alternate);

            if (firstLink != null)
            {
                Link = firstLink.Uri.IsAbsoluteUri ? firstLink.Uri.AbsoluteUri : new Uri(website, firstLink.Uri).AbsoluteUri;
            }
            else
            {
                Link = item.Id;
            }

            Published   = item.Published != default ? item.Published : item.LastUpdated;
            Recent      = Published > recent;
            Description = item.Description;
            Links       = item.Links
                          .Where(x => !string.IsNullOrEmpty(x.MediaType))
                          .GroupBy(x => x.MediaType)
                          .Select(x => x.First())
                          .ToDictionary(x => x.MediaType, x => x.Uri.ToString());

            ISyndicationPerson person = item.Contributors.FirstOrDefault(x => x.RelationshipType == "author");

            if (person != null)
            {
                Author = person.Name ?? person.Email;
            }

            AtomEntry atom = item as AtomEntry;

            if (atom != null && !string.IsNullOrEmpty(atom.Summary))
            {
                Description = atom.Summary;
            }
        }
コード例 #20
0
        public virtual ISyndicationContent CreateContent(ISyndicationLink link)
        {
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

            if (link.Uri == null)
            {
                throw new ArgumentNullException("Uri");
            }

            switch (link.RelationshipType)
            {
            case AtomLinkTypes.Content:
                return(CreateFromContentLink(link));

            case AtomLinkTypes.Source:
                return(CreateFromSourceLink(link));

            default:
                return(CreateFromLink(link));
            }
        }
コード例 #21
0
        public async Task ConsumeFeed(string filePath)
        {
            bool verbose = false;

            // Information to display.
            double size        = new FileInfo(filePath).Length;
            double currentSize = 0;
            int    itemsRead   = 0;

            // Transform the size of the file to Kb - Mb - Gb.
            Tuple <double, string> sizeInfo = utils.ConvertBytesToSize(size);

            // Display the Size of the feed and ask for verbose.
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("Size of the Feed: {0:N2} {1}", sizeInfo.Item1, sizeInfo.Item2);
            Console.Write("Verbose Items (Y/N): ");

            Console.ForegroundColor = ConsoleColor.White;
            string input = Console.ReadLine();

            verbose = utils.ValidateVerbose(input);
            Console.CursorVisible = false;
            Stopwatch stopWatch = null;

            using (var xmlReader = XmlReader.Create(filePath, new XmlReaderSettings()
            {
                Async = true
            }))
            {
                var reader = new Rss20FeedReader(xmlReader);
                stopWatch = new Stopwatch();
                stopWatch.Start();
                ElementDisplayer displayer = new ElementDisplayer();

                while (await reader.Read())
                {
                    if (verbose)
                    {
                        utils.ClearInformation();
                    }

                    switch (reader.ElementType)
                    {
                    case SyndicationElementType.Content:
                        ISyndicationContent content = await reader.ReadContent();

                        if (verbose)
                        {
                            displayer.DisplayContent(content);
                        }
                        break;

                    case SyndicationElementType.Item:
                        ISyndicationItem item = await ReadItem(reader);

                        itemsRead++;
                        if (verbose)
                        {
                            displayer.DisplayItem(item);
                        }
                        currentSize += _sizeOfItem;
                        break;

                    case SyndicationElementType.Person:
                        ISyndicationPerson person = await ReadPerson(reader);

                        if (verbose)
                        {
                            displayer.DisplayPerson(person);
                        }
                        break;

                    case SyndicationElementType.Image:
                        ISyndicationImage image = await ReadImage(reader);

                        if (verbose)
                        {
                            displayer.DisplayImage(image);
                        }
                        break;

                    case SyndicationElementType.Link:
                        ISyndicationLink link = await ReadLink(reader);

                        if (verbose)
                        {
                            displayer.DisplayLink(link);
                        }
                        break;

                    case SyndicationElementType.Category:
                        ISyndicationCategory category = await ReadCategory(reader);

                        if (verbose)
                        {
                            displayer.DisplayCategory(category);
                        }
                        break;
                    }

                    double percentage = ((currentSize * 100) / size);

                    if (itemsRead % 200 == 0)
                    {
                        utils.WriteInformation(percentage, itemsRead, stopWatch.Elapsed);
                    }
                }
            }
            utils.ClearInformation();

            //Print end of reading
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Finished Reading, press enter to close.\n\n");
            utils.WriteInformation(100, itemsRead, stopWatch.Elapsed);
            Console.ReadLine();
        }
コード例 #22
0
        private async Task <List <BlogPost> > GetFeed()
        {
            var xmlReader = XmlReader.Create(_settings.Value.FeedUrl);
            var reader    = new AtomFeedReader(xmlReader);

            var posts = new List <BlogPost>();

            //
            // Read the feed
            while (await reader.Read())
            {
                //
                // Check the type of the current element.
                switch (reader.ElementType)
                {
                //
                // Read category
                case SyndicationElementType.Category:
                    ISyndicationCategory category = await reader.ReadCategory();

                    break;

                //
                // Read image
                case SyndicationElementType.Image:
                    ISyndicationImage image = await reader.ReadImage();

                    break;

                //
                // Read entry
                case SyndicationElementType.Item:
                    IAtomEntry entry = await reader.ReadEntry();

                    // these are the only ones we need for now
                    posts.Add(new BlogPost
                    {
                        Title         = entry.Title,
                        PublishedDate = entry.Published.DateTime,
                        Categories    = entry.Categories?.Select(c => c.Name)?.ToList()
                    });
                    break;

                //
                // Read link
                case SyndicationElementType.Link:
                    ISyndicationLink link = await reader.ReadLink();

                    break;

                //
                // Read person
                case SyndicationElementType.Person:
                    ISyndicationPerson person = await reader.ReadPerson();

                    break;

                //
                // Read content
                default:
                    ISyndicationContent content = await reader.ReadContent();

                    break;
                }
            }

            return(posts);
        }
コード例 #23
0
        public async Task <Channel9RssResult> Parse(Uri rssUri)
        {
            var result = new Channel9RssResult();

            result.SourceUrl = rssUri;

            try
            {
                using (var client = new HttpClient())
                {
                    result.RawXml = await client.GetStringAsync(rssUri);

                    using (var xmlReader = XmlReader.Create(new StringReader(result.RawXml), new XmlReaderSettings()
                    {
                        Async = false
                    }))
                    {
                        var feedReader = new RssFeedReader(xmlReader);

                        while (await feedReader.Read())
                        {
                            switch (feedReader.ElementType)
                            {
                            // Read category
                            case SyndicationElementType.Category:
                                ISyndicationCategory category = await feedReader.ReadCategory();

                                break;

                            // Read Image
                            case SyndicationElementType.Image:
                                ISyndicationImage image = await feedReader.ReadImage();

                                break;

                            // Read Item
                            case SyndicationElementType.Item:

                                // parse the syndication item
                                ISyndicationItem item = await feedReader.ReadItem();

                                result.SyndicationItems.Add(item);

                                // then construct a session info
                                var si = new SessionInfo();
                                si.SessionID   = item.Id.Substring(item.Id.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1);
                                si.Title       = item.Title;
                                si.SessionSite = new Uri(item.Id);
                                si.PublishDate = item.Published.DateTime;
                                si.Presenter   = item.Contributors.FirstOrDefault()?.Name;

                                result.Sessions.Add(si);

                                foreach (var v in item.Links)
                                {
                                    if (!string.IsNullOrWhiteSpace(v.MediaType))
                                    {
                                        si.VideoRecordings.Add(new VideoRecording()
                                        {
                                            SessionInfo = si, Url = v.Uri, MediaType = v.MediaType, Length = v.Length
                                        });
                                    }
                                }

                                break;

                            // Read link
                            case SyndicationElementType.Link:
                                ISyndicationLink link = await feedReader.ReadLink();

                                break;

                            // Read Person
                            case SyndicationElementType.Person:
                                ISyndicationPerson person = await feedReader.ReadPerson();

                                break;

                            // Read content
                            default:
                                ISyndicationContent content = await feedReader.ReadContent();

                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Exceptions = new List <Exception>();
                result.Exceptions.Add(ex);
            }

            return(result);
        }
コード例 #24
0
        public virtual ISyndicationImage CreateImage(ISyndicationContent content)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            string           title       = null;
            string           description = null;
            Uri              url         = null;
            ISyndicationLink link        = null;

            foreach (var field in content.Fields)
            {
                if (field.Namespace != RssConstants.Rss20Namespace)
                {
                    continue;
                }

                switch (field.Name)
                {
                //
                // Title
                case RssElementNames.Title:
                    title = field.Value;
                    break;

                //
                // Url
                case RssElementNames.Url:
                    if (!TryParseValue(field.Value, out url))
                    {
                        throw new FormatException($"Invalid image url '{field.Value}'");
                    }
                    break;

                //
                // Link
                case RssElementNames.Link:
                    link = CreateLink(field);
                    break;

                //
                // Description
                case RssElementNames.Description:
                    description = field.Value;
                    break;

                default:
                    break;
                }
            }

            if (url == null)
            {
                throw new FormatException("Image url not found");
            }

            return(new SyndicationImage(url, RssElementNames.Image)
            {
                Title = title,
                Description = description,
                Link = link
            });
        }
コード例 #25
0
        public string Format(ISyndicationLink link)
        {
            ISyndicationContent content = CreateContent(link);

            return(Format(content));
        }
コード例 #26
0
 public virtual Task Write(ISyndicationLink link)
 {
     return(WriteRaw(Formatter.Format(link ?? throw new ArgumentNullException(nameof(link)))));
 }
コード例 #27
0
        public async Task <AtomPage> ReadAtomPage(Stream stream)
        {
            using var xmlReader = XmlReader.Create(stream, new XmlReaderSettings { Async = true });

            var feedReader = new AtomFeedReader(xmlReader);
            var entries = new List <AtomEntry>();
            Uri?nextPageUri = null, previousPageUri = null, realTimeNotificationUri = null;

            while (await feedReader.Read())
            {
                await ProcessElementAsync();
            }

            return(new AtomPage(
                       nextArchivePageUri: nextPageUri,
                       previousArchivePageUri: previousPageUri,
                       realTimeNotificationUri: realTimeNotificationUri,
                       entries: entries
                       ));

            async Task ProcessElementAsync()
            {
                switch (feedReader.ElementType)
                {
                case SyndicationElementType.Item:
                    await ProcessItemElementAsync();

                    break;

                case SyndicationElementType.Link:
                    await ProcessLinkElementAsync();

                    break;
                }
            }

            async Task ProcessItemElementAsync()
            {
                var item = (Microsoft.SyndicationFeed.Atom.AtomEntry) await feedReader.ReadItem();

                var atomEntry = new AtomEntry(
                    id: item.Id,
                    published: item.Published,
                    contentType: item.ContentType,
                    payload: item.Description
                    );

                entries.Insert(0, atomEntry);
            }

            async Task ProcessLinkElementAsync()
            {
                ISyndicationLink link = await feedReader.ReadLink();

                switch (link.RelationshipType)
                {
                case "next-archive" when nextPageUri == null:
                    nextPageUri = link.Uri;
                    break;

                case "prev-archive" when previousPageUri == null:
                    previousPageUri = link.Uri;
                    break;

                case "notifications" when realTimeNotificationUri == null:
                    realTimeNotificationUri = link.Uri;
                    break;
                }
            }
        }
コード例 #28
0
 internal void DisplayLink(ISyndicationLink link)
 {
     Console.WriteLine("--- Link Read ---");
     Console.WriteLine("Link: " + link.Uri.AbsoluteUri);
     Console.WriteLine();
 }
コード例 #29
0
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            string feed = input.GetString("Feed");

            if (!string.IsNullOrEmpty(feed))
            {
                try
                {
                    // Download the feed
                    context.LogInformation($"Getting feed for {feed}");
                    Uri    website                = null;
                    string title                  = null;
                    string author                 = null;
                    string description            = null;
                    string image                  = null;
                    List <ISyndicationItem> items = new List <ISyndicationItem>();
                    using (HttpClient httpClient = context.CreateHttpClient())
                    {
                        httpClient.DefaultRequestHeaders.Add("User-Agent", "Wyam");

                        var response = await httpClient.GetAsync(feed);

                        if (response.StatusCode == HttpStatusCode.Redirect ||
                            response.StatusCode == HttpStatusCode.MovedPermanently)
                        {
                            context.LogWarning($"Attempting to follow redirect for {feed}");
                            feed     = response.Headers.Location.OriginalString;
                            response = await httpClient.GetAsync(feed);
                        }

                        if (response.IsSuccessStatusCode)
                        {
                            using (Stream stream = await response.Content.ReadAsStreamAsync())
                                using (StreamReader streamReader = new XmlStreamReader(stream))
                                    using (XmlReader xmlReader = XmlReader.Create(streamReader, new XmlReaderSettings {
                                        Async = true, DtdProcessing = DtdProcessing.Ignore
                                    }))
                                    {
                                        xmlReader.MoveToContent();
                                        bool atom = xmlReader.Name == "feed";
                                        context.LogInformation($"Reading {feed} as " + (atom ? "Atom" : "RSS"));
                                        XmlFeedReader feedReader = atom
                                    ? (XmlFeedReader) new AtomFeedReader(xmlReader, new FixedAtomParser())
                                    : new RssFeedReader(xmlReader);
                                        while (await feedReader.Read())
                                        {
                                            try
                                            {
                                                switch (feedReader.ElementType)
                                                {
                                                case SyndicationElementType.Person:
                                                    ISyndicationPerson person = await feedReader.ReadPerson();

                                                    if (person.RelationshipType == "author")
                                                    {
                                                        author = person.Name ?? person.Email;
                                                    }
                                                    break;

                                                case SyndicationElementType.Image:
                                                    ISyndicationImage img = await feedReader.ReadImage();

                                                    image = img.Url.ToString();
                                                    break;

                                                case SyndicationElementType.Link:
                                                    ISyndicationLink link = await feedReader.ReadLink();

                                                    website = link.Uri;
                                                    break;

                                                case SyndicationElementType.Item:
                                                    ISyndicationItem item = await feedReader.ReadItem();

                                                    items.Add(item);
                                                    break;

                                                case SyndicationElementType.None:
                                                    break;

                                                default:
                                                    ISyndicationContent content = await feedReader.ReadContent();

                                                    if (string.Equals(content.Name, "title", StringComparison.OrdinalIgnoreCase))
                                                    {
                                                        title = content.Value;
                                                    }
                                                    else if (string.Equals(content.Name, "description", StringComparison.OrdinalIgnoreCase) ||
                                                             string.Equals(content.Name, "subtitle", StringComparison.OrdinalIgnoreCase))
                                                    {
                                                        description = content.Value;
                                                    }
                                                    break;
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                context.LogWarning($"Exception while processing {feedReader.ElementType} in {feed}: {ex.Message}");
                                            }
                                        }
                                    }
                        }

                        // Get a new document with feed metadata
                        MetadataItems metadata = new MetadataItems();
                        if (items.Count > 0)
                        {
                            BlogFeedItem[] feedItems = items
                                                       .Select(x => new BlogFeedItem(x, _recent, website))
                                                       .OrderByDescending(x => x.Published)
                                                       .Take(50) // Only take the 50 most recent items
                                                       .ToArray();
                            metadata.Add(SiteKeys.BlogFeedItems, feedItems);
                            return(input.Clone(metadata).Yield());
                        }
                    }
                }
                catch (Exception ex)
                {
                    context.LogWarning($"Error getting feed for {feed}: {ex.Message}");
                }
            }
            else
            {
                var speaker = input.GetString("Title");
                if (!string.IsNullOrWhiteSpace(speaker))
                {
                    context.LogWarning($"No RSS specified for {speaker}.");
                }
                else
                {
                    context.LogWarning($"No RSS specified for unknown speaker.");
                }
            }
            return(input.Yield());
        }
コード例 #30
0
        public virtual ISyndicationContent CreateContent(ISyndicationItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            // Spec requires to have at least one title or description
            if (string.IsNullOrEmpty(item.Title) && string.IsNullOrEmpty(item.Description))
            {
                throw new ArgumentNullException("RSS Item requires a title or a description");
            }

            // Write <item> tag
            var content = new SyndicationContent(RssElementNames.Item);

            //
            // Title
            if (!string.IsNullOrEmpty(item.Title))
            {
                content.AddField(new SyndicationContent(RssElementNames.Title, item.Title));
            }

            //
            // Links
            ISyndicationLink guidLink = null;

            if (item.Links != null)
            {
                foreach (var link in item.Links)
                {
                    if (link.RelationshipType == RssElementNames.Guid)
                    {
                        guidLink = link;
                    }

                    content.AddField(CreateContent(link));
                }
            }

            //
            // Description
            if (!string.IsNullOrEmpty(item.Description))
            {
                content.AddField(new SyndicationContent(RssElementNames.Description, item.Description));
            }

            //
            // Authors (persons)
            if (item.Contributors != null)
            {
                foreach (var person in item.Contributors)
                {
                    content.AddField(CreateContent(person));
                }
            }

            //
            // Cathegory
            if (item.Categories != null)
            {
                foreach (var category in item.Categories)
                {
                    content.AddField(CreateContent(category));
                }
            }

            //
            // Guid (id)
            if (guidLink == null && !string.IsNullOrEmpty(item.Id))
            {
                var guid = new SyndicationContent(RssElementNames.Guid, item.Id);

                guid.AddAttribute(new SyndicationAttribute(RssConstants.IsPermaLink, "false"));

                content.AddField(guid);
            }

            //
            // PubDate
            if (item.Published != DateTimeOffset.MinValue)
            {
                content.AddField(new SyndicationContent(RssElementNames.PubDate, FormatValue(item.Published)));
            }

            return(content);
        }