예제 #1
0
        public virtual ISyndicationContent CreateContent(ISyndicationCategory category)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            if (string.IsNullOrEmpty(category.Name))
            {
                throw new ArgumentNullException("Name");
            }

            var result = new SyndicationContent(AtomElementNames.Category);

            //
            // term
            result.AddAttribute(new SyndicationAttribute(AtomConstants.Term, category.Name));

            //
            // scheme
            if (!string.IsNullOrEmpty(category.Scheme))
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Scheme, category.Scheme));
            }

            //
            // label
            if (!string.IsNullOrEmpty(category.Label))
            {
                result.AddAttribute(new SyndicationAttribute(AtomConstants.Label, category.Label));
            }

            return(result);
        }
        private async Task <ISyndicationCategory> ReadCategory(Rss20FeedReader reader)
        {
            ISyndicationCategory category = null;

            category = await reader.ReadCategory();

            return(category);
        }
예제 #3
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;
                }
            }
        }
    }
        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);
        }
예제 #5
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);
            }
        }
예제 #6
0
    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;
                }
            }
        }
    }
예제 #7
0
        public virtual ISyndicationContent CreateContent(ISyndicationCategory category)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            if (string.IsNullOrEmpty(category.Name))
            {
                throw new FormatException("Invalid category name");
            }

            return(new SyndicationContent(RssElementNames.Category, category.Name));
        }
        public void AddCategory(ISyndicationCategory category)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

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

            _categories.Add(category);
        }
예제 #9
0
        public async Task ReadCategory()
        {
            using (XmlReader xmlReader = XmlReader.Create(@"..\..\..\TestFeeds\simpleAtomFeed.xml", new XmlReaderSettings {
                Async = true
            }))
            {
                var reader = new AtomFeedReader(xmlReader);
                while (await reader.Read())
                {
                    if (reader.ElementType == SyndicationElementType.Category)
                    {
                        ISyndicationCategory category = await reader.ReadCategory();

                        Assert.True(category.Name == "sports");
                        Assert.True(category.Label == "testLabel");
                        Assert.True(category.Scheme == "testScheme");
                    }
                }
            }
        }
        public virtual ISyndicationContent CreateContent(ISyndicationCategory category)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            if (string.IsNullOrEmpty(category.Name))
            {
                throw new FormatException("Invalid category name");
            }

            var content = new SyndicationContent(RssElementNames.Category, category.Name);

            if (category.Scheme != null)
            {
                content.AddAttribute(new SyndicationAttribute(RssConstants.Domain, category.Scheme));
            }

            return(content);
        }
        public async Task ReadCategory()
        {
            using (var xmlReader = XmlReader.Create(@"..\..\..\TestFeeds\rss20.xml", new XmlReaderSettings()
            {
                Async = true
            }))
            {
                var reader = new RssFeedReader(xmlReader);

                while (await reader.Read())
                {
                    if (reader.ElementType == SyndicationElementType.Category)
                    {
                        ISyndicationCategory category = await reader.ReadCategory();

                        Assert.True(category.Name == "Newspapers");
                        Assert.True(category.Scheme == "http://example.com/news");
                    }
                }
            }
        }
예제 #12
0
        public async Task <Podcast> ParseRssFeed(Podcast podcastData, bool isUpdate = false)
        {
            string title                           = string.Empty,
                   imageUrl                        = string.Empty,
                   description                     = string.Empty,
                   author                          = string.Empty;
            DateTime pubDate                       = DateTime.MinValue,
                     lastUpdateDate                = DateTime.MinValue;
            List <ISyndicationItem>   items        = new List <ISyndicationItem>();
            IEnumerable <PodcastItem> podcastItems = Enumerable.Empty <PodcastItem>();
            Podcast podcast                        = null;

            using (var xmlReader = XmlReader.Create(podcastData.Url, new XmlReaderSettings {
                Async = true
            }))
            {
                var feedReader = new RssFeedReader(xmlReader);

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

                        break;

                    case SyndicationElementType.Content:
                        ISyndicationContent content = await feedReader.ReadContent();

                        if (content.Name == "title")
                        {
                            title = content.Value;
                        }
                        if (content.Name == "description")
                        {
                            description = content.Value;
                        }
                        if (content.Name == "author")
                        {
                            author = content.Value;
                        }
                        break;

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

                        imageUrl = image.Url?.AbsoluteUri;
                        break;

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

                        items.Add(item);
                        break;

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

                        break;

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

                        break;

                    case SyndicationElementType.None:
                    default:
                        break;
                    }
                }

                pubDate = items.Max(item => item.Published.DateTime);

                if (isUpdate)
                {
                    lastUpdateDate             = podcastData.LastUpdateDate;
                    podcastData.Author         = author;
                    podcastData.Title          = title;
                    podcastData.ImageUrl       = imageUrl;
                    podcastData.Description    = description;
                    podcastData.LastUpdateDate = pubDate;
                    podcast = podcastData;
                    await dataService.Update <Podcast>(podcast);
                }
                else
                {
                    podcast = new Podcast(title, podcastData.Url, imageUrl, description, author)
                    {
                        LastUpdateDate = pubDate == DateTime.MinValue ? DateTime.Now : pubDate
                    };
                    await dataService.Insert <Podcast>(podcast);
                }

                podcastItems = items.Select(item => new
                {
                    item.Title,
                    item.Description,
                    Enclosure   = item.Links.FirstOrDefault(linkItem => linkItem.RelationshipType == "enclosure"),
                    PublishDate = item.Published.DateTime
                }).Select(data => new PodcastItem(data.Title, data.Description, data.Enclosure.Uri.OriginalString,
                                                  data.Enclosure.Length, data.PublishDate, podcast.Id))
                               .Where(item => item.PublishDate > lastUpdateDate)
                               .ToList();

                await dataService.Insert(podcastItems);

                podcast.PodcastItems = podcastItems.ToList();
                await dataService.Update(podcast);
            }

            return(podcast);
        }
        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);
        }
예제 #14
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);
        }
예제 #15
0
        async static Task <List <Tuple <string, string, DateTimeOffset> > > Latest5PostsFromRssAsync(string filePath)
        {
            List <Tuple <string, string, DateTimeOffset> > result = new List <Tuple <string, string, DateTimeOffset> >();

            using (var xmlReader = XmlReader.Create(filePath, 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();

                        result.Add(new Tuple <string, string, DateTimeOffset>(item.Title, item.Links.First().Uri.ToString(), item.Published));

                        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;
                    }

                    if (result.Count == 5)
                    {
                        break;
                    }
                }
            }

            return(result);
        }
예제 #16
0
        public string Format(ISyndicationCategory category)
        {
            ISyndicationContent content = CreateContent(category);

            return(Format(content));
        }
예제 #17
0
    public static async Task ReadAtomFeed(string filePath)
    {
        //
        // Create an XmlReader from file
        // Example: ..\tests\TestFeeds\simpleAtomFeed.xml
        using (XmlReader xmlReader = XmlReader.Create(filePath, new XmlReaderSettings()
        {
            Async = true
        }))
        {
            //
            // Create an AtomFeedReader
            var reader = new AtomFeedReader(xmlReader);

            //
            // 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();

                    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;
                }
            }
        }
    }
        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();
        }
예제 #19
0
 public string Format(ISyndicationCategory category)
 {
     return(Format(CreateContent(category)));
 }
        public static async Task ReadFeed()
        {
            using (XmlReader xmlReader = XmlReader.Create(@"..\DemoTest1\TestFeeds\rss20.xml", new XmlReaderSettings()
            {
                Async = true
            }))
                using (XmlWriter xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings()
                {
                    Indent = true, Async = true
                }))
                {
                    Rss20FeedReader reader = new Rss20FeedReader(xmlReader);
                    Rss20FeedWriter writer = new Rss20FeedWriter(xmlWriter);

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

                            await writer.Write(category);

                            break;

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

                            await writer.Write(image);

                            break;

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

                            await writer.Write(item);

                            break;

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

                            await writer.Write(link);

                            break;

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

                            await writer.Write(person);

                            break;

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

                            await writer.Write(content);

                            break;
                        }

                        xmlWriter.Flush();
                        // Simulate a slow stream.
                        Thread.Sleep(300);
                    }
                }
        }
예제 #21
0
 internal void DisplayCategory(ISyndicationCategory category)
 {
     Console.WriteLine("--- Category Read ---");
     Console.WriteLine("Category: " + category.Name);
     Console.WriteLine();
 }
 public virtual Task Write(ISyndicationCategory category)
 {
     return(WriteRaw(Formatter.Format(category ?? throw new ArgumentNullException(nameof(category)))));
 }