Ejemplo n.º 1
0
        public static async Task <List <ComplexSyndication> > Get(params Uri[] url)
        {
            var parser = new RssParser();

            var toBeProcessed = new List <Task <HttpResponseMessage> >();

            var httpClient = HttpClientFactory.Get();

            foreach (var u in url)
            {
                var t = httpClient.GetAsync(u);
                toBeProcessed.Add(t);
            }

            Task.WaitAll(toBeProcessed.ToArray());

            var syndications = new List <ComplexSyndication>();

            foreach (var result in toBeProcessed)
            {
                var res           = result.Result;
                var resultContent = await res.Content.ReadAsStringAsync();

                using (var xmlReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(resultContent))))
                {
                    var feedReader = new RssFeedReader(xmlReader);

                    var syndication = new ComplexSyndication();

                    while (await feedReader.Read())
                    {
                        switch (feedReader.ElementType)
                        {
                        case SyndicationElementType.Item:
                            //ISyndicationContent is a raw representation of the feed
                            ISyndicationContent content = await feedReader.ReadContent();

                            ISyndicationItem    item    = parser.CreateItem(content);
                            ISyndicationContent outline = content.Fields.FirstOrDefault(f => f.Name == "source:outline");

                            var i = new ComplexSyndicationItem(item, outline);
                            Replay.OnNext(i);
                            syndication.Items.Add(i);
                            break;

                        default:
                            break;
                        }
                    }

                    syndications.Add(syndication);
                }
            }

            Replay.OnCompleted();

            return(syndications);
        }
Ejemplo n.º 2
0
        public static List <ComplexSyndication> GetObservable(params Uri[] url)
        {
            var httpClient = HttpClientFactory.Get();

            var feeds = url.ToObservable()
                        .Select(u => Observable.FromAsync(() => httpClient.GetAsync(u)))
                        .Merge(2);

            var syndications = new List <ComplexSyndication>();
            var parser       = new RssParser();

            feeds.Subscribe(async msg =>
            {
                var resultContent = await msg.Content.ReadAsStringAsync();

                using (var xmlReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(resultContent))))
                {
                    var feedReader = new RssFeedReader(xmlReader);

                    var syndication = new ComplexSyndication();

                    while (await feedReader.Read())
                    {
                        switch (feedReader.ElementType)
                        {
                        case SyndicationElementType.Item:
                            //ISyndicationContent is a raw representation of the feed
                            ISyndicationContent content = await feedReader.ReadContent();

                            ISyndicationItem item       = parser.CreateItem(content);
                            ISyndicationContent outline = content.Fields.FirstOrDefault(f => f.Name == "source:outline");

                            syndication.Items.Add(new ComplexSyndicationItem(item, outline));
                            break;

                        default:
                            break;
                        }
                    }

                    syndications.Add(syndication);
                }
            }, ex => {
            },
                            () => {
            });

            return(syndications);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Feed Reader
        /// https://github.com/dotnet/SyndicationFeedReaderWriter/blob/master/examples/ReadRssItemWithCustomFieldsExample.cs
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static async Task ReadAtomFeed(string filepath)
        {
            try
            {
                //
                // Create an XmlReader from file
                using (var xmlReader = XmlReader.Create(filepath, new XmlReaderSettings()
                {
                    Async = true
                }))
                {
                    var parser     = new RssParser();
                    var feedReader = new RssFeedReader(xmlReader, parser);

                    //
                    // Read the feed
                    while (await feedReader.Read())
                    {
                        if (feedReader.ElementType == SyndicationElementType.Item)
                        {
                            //
                            // Read the item as generic content
                            ISyndicationContent content = await feedReader.ReadContent();

                            //
                            // Parse the item if needed (unrecognized tags aren't available)
                            // Utilize the existing parser
                            ISyndicationItem item = parser.CreateItem(content);

                            Console.WriteLine($"Item: {item.Title}");

                            //
                            // Get <example:customElement> field
                            ISyndicationContent customElement = content.Fields.FirstOrDefault(f => f.Name == "example:customElement");

                            if (customElement != null)
                            {
                                Console.WriteLine($"{customElement.Name}: {customElement.Value}");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.InnerException.Message}{Environment.NewLine}{ex.InnerException.ToString()}");
            }
        }
Ejemplo n.º 4
0
    public static async Task ReadFeed(string filepath)
    {
        //
        // Create an XmlReader from file
        // Example: ..\tests\TestFeeds\rss20-2items.xml
        using (var xmlReader = XmlReader.Create(filepath, new XmlReaderSettings()
        {
            Async = true
        }))
        {
            var parser     = new RssParser();
            var feedReader = new RssFeedReader(xmlReader, parser);

            //
            // Read the feed
            while (await feedReader.Read())
            {
                if (feedReader.ElementType == SyndicationElementType.Item)
                {
                    //
                    // Read the item as generic content
                    ISyndicationContent content = await feedReader.ReadContent();

                    //
                    // Parse the item if needed (unrecognized tags aren't available)
                    // Utilize the existing parser
                    ISyndicationItem item = parser.CreateItem(content);

                    Console.WriteLine($"Item: {item.Title}");

                    //
                    // Get <example:customElement> field
                    ISyndicationContent customElement = content.Fields.FirstOrDefault(f => f.Name == "example:customElement");

                    if (customElement != null)
                    {
                        Console.WriteLine($"{customElement.Name}: {customElement.Value}");
                    }
                }
            }
        }
    }
Ejemplo n.º 5
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger, IConfiguration configuration)
        {
            //These are the four default services available at Configure
            app.Run(async context =>
            {
                var parser = new RssParser();
                var items  = new List <OutlineSyndicationItem>();

                using (var xmlReader = XmlReader.Create("https://www.nasa.gov/rss/dyn/Houston-We-Have-a-Podcast.rss", new XmlReaderSettings {
                    Async = true
                }))
                {
                    var feedReader = new RssFeedReader(xmlReader);

                    while (await feedReader.Read())
                    {
                        switch (feedReader.ElementType)
                        {
                        case SyndicationElementType.Item:
                            //ISyndicationContent is a raw representation of the feed
                            ISyndicationContent content = await feedReader.ReadContent();

                            ISyndicationItem item       = parser.CreateItem(content);
                            ISyndicationContent outline = content.Fields.FirstOrDefault(f => f.Name == "source:outline");

                            items.Add(new OutlineSyndicationItem(item, outline));
                            break;

                        default:
                            break;
                        }
                    }
                }

                var str = new StringBuilder();
                str.Append("<ul>");
                foreach (var i in items)
                {
                    str.Append($"<li>{i.Item.Published} - {i.Item.Description} - <span style=\"color:red;\">");
                    if (i.Outline != null)
                    {
                        str.Append("<ul>");
                        foreach (var o in i.Outline.Attributes)
                        {
                            str.Append($"<li>{o.Key} - {o.Value}</li>");
                        }
                        str.Append("</ul>");
                    }
                    str.Append("</li>");
                }
                str.Append("</ul>");

                context.Response.Headers.Add("Content-Type", "text/html");
                await context.Response.WriteAsync($@"
                <html>
                    <head>
                        <link rel=""stylesheet"" type=""text/css"" href=""http://fonts.googleapis.com/css?family=Germania+One"">
                        <style>
                         body {{
                            font-family: 'Germania One', serif;
                            font-size: 24px;
                        }}
                        </style>
                    </head>
                    <body>
                        {str.ToString()}
                    </body>
                </html>
                ");
            });
        }
Ejemplo n.º 6
0
        public async Task <IEnumerable <Post> > ReadPostsAsync(string rssFeed, DateTime limit)
        {
            var key = rssFeed;

            if (Cache.TryGet(key, out var posts))
            {
                return(posts);
            }

            var parser = new RssParser();
            var ret    = new List <Post>();
            var page   = 0;

            while (true)
            {
                page++;
                var url = page == 1 ? rssFeed : rssFeed + $"?paged={page}";

                var response = await httpClient.GetAsync(url);

                var feedAsString = string.Empty;
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    break;
                }
                else
                {
                    feedAsString = await response.Content.ReadAsStringAsync();
                }

                using (var memoryFeed = new MemoryStream(Encoding.UTF8.GetBytes(feedAsString)))
                    using (var xmlReader = XmlReader.Create(memoryFeed, new XmlReaderSettings {
                        Async = true
                    }))
                    {
                        var feedReader = new RssFeedReader(xmlReader);
                        while (await feedReader.Read())
                        {
                            if (feedReader.ElementType == SyndicationElementType.Item)
                            {
                                var content = await feedReader.ReadContent();

                                var author      = content.Fields.FirstOrDefault(f => f.Name == "creator");
                                var htmlContent = content.Fields.FirstOrDefault(f => f.Name == "encoded");

                                var item = parser.CreateItem(content);

                                var post = new Post
                                {
                                    Id          = int.Parse(item.Id.Split('=')[1]),
                                    Title       = item.Title,
                                    Content     = Cleaner.CleanAnnoyingOriginalBlogCitation(htmlContent?.Value),
                                    Description = Cleaner.CleanAnnoyingOriginalBlogCitation(item.Description),
                                    Date        = item.Published.DateTime,
                                    Path        = item.Links.First()?.Uri.Segments.Last(),
                                    Tags        = item.Categories.Select(e => e.Name).ToArray(),
                                    Author      = author?.Value ?? string.Empty
                                };

                                if (post.Date >= limit)
                                {
                                    ret.Add(post);
                                }
                            }
                        }

                        if (ret.Any() && ret.Min(e => e.Date) < limit)
                        {
                            break;
                        }
                    }
            }

            Cache.Put(key, ret, TimeSpan.FromHours(1));
            return(ret);
        }
Ejemplo n.º 7
0
        static async void ReadRss()
        {
            string title        = null;
            string description  = null;
            string link         = null;
            string category     = null;
            string localstart   = null;
            string localend     = null;
            bool   allday       = false;
            string age          = null;
            string cost         = null;
            string venue        = null;
            string venueaddress = null;

            using (var xmlReader = XmlReader.Create("C:/Users/Developer/Desktop/mob.rss",
                                                    new XmlReaderSettings()
            {
                Async = true
            }))
            {
                var      parser     = new RssParser();
                var      feedReader = new RssFeedReader(xmlReader, parser);
                string[] attValues  = new string[] { "title", "description", "link", "category", "localstart", "localend", "cdo-alldayevent" };
                string[] custValues = new string[] { "Cost", "Age", "Venue", "Venue address" };

                while (await feedReader.Read())
                {
                    if (feedReader.ElementType == SyndicationElementType.Item)
                    {
                        ISyndicationContent content = await feedReader.ReadContent();

                        ISyndicationItem item = parser.CreateItem(content);

                        for (int i = 0; i < attValues.Length; i++)
                        {
                            ISyndicationContent value = content.Fields.FirstOrDefault(f => f.Name == attValues[i]);

                            if (attValues[i] == "title")
                            {
                                title = value.Value;
                            }
                            else if (attValues[i] == "description")
                            {
                                description = WebUtility.HtmlDecode(value.Value);
                                description = Regex.Replace(description, "<.*?>", String.Empty);
                            }
                            else if (attValues[i] == "link")
                            {
                                link = value.Value;
                            }
                            else if (attValues[i] == "category")
                            {
                                category = value.Value;
                            }
                            else if (attValues[i] == "localstart")
                            {
                                localstart = value.Value;
                            }
                            else if (attValues[i] == "localend")
                            {
                                localend = value.Value;
                            }
                            else if (attValues[i] == "cdo-alldayevent")
                            {
                                allday = Convert.ToBoolean(value.Value);
                            }
                        }

                        var customFields = content.Fields.Where(f => f.Name == "customfield");

                        for (int i = 0; i < custValues.Length; i++)
                        {
                            var customatt = CustomFieldValue(customFields, custValues[i]); {
                                if (custValues[i] == "Cost")
                                {
                                    cost = customatt;
                                }
                                if (custValues[i] == "Age")
                                {
                                    age = customatt;
                                }
                                if (custValues[i] == "Venue")
                                {
                                    venue = customatt;
                                }
                                if (custValues[i] == "Venue address")
                                {
                                    venueaddress = customatt;
                                }
                            }
                        }

                        RssContent.FillContent(title, description, link, category, localstart, localend, allday, cost, age, venue, venueaddress);

                        Console.WriteLine("Title: " + RssContent.Title + Environment.NewLine + "Description: " + RssContent.Description +
                                          Environment.NewLine + "Link: " + RssContent.Link + Environment.NewLine + "Start Date: " + RssContent.StartDate +
                                          Environment.NewLine + "Start DateTime: " + RssContent.StartDateTime + Environment.NewLine + "End DateTime: " +
                                          RssContent.EndDateTime + Environment.NewLine + "All Day: " + RssContent.AllDay + Environment.NewLine + "Cost: " +
                                          RssContent.Cost + Environment.NewLine + "Age: " + RssContent.Age + Environment.NewLine + "Venue: " +
                                          RssContent.Venue + Environment.NewLine + "Address: " + RssContent.VenueAddress + Environment.NewLine);
                    }
                }
            }

            string CustomFieldValue(IEnumerable <ISyndicationContent> customFields, string name)
            {
                var field = GetCustomFieldForName(customFields, name);

                return(field == null ? string.Empty : field.Value);
            }

            ISyndicationContent GetCustomFieldForName(IEnumerable <ISyndicationContent> customFields, string name)
            {
                foreach (var field in customFields)
                {
                    foreach (var attribute in field.Attributes)
                    {
                        if (attribute.Name == "name" && attribute.Value == name)
                        {
                            return(field);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                return(null);
            }
        }