Example #1
0
        private async Task writeSyndicationFeedToResponseAsync(ActionContext context)
        {
            var response  = context.HttpContext.Response;
            var mediaType = new MediaTypeHeaderValue("application/rss+xml")
            {
                CharSet = Encoding.UTF8.WebName
            };

            response.ContentType = mediaType.ToString();

            var xws = new XmlWriterSettings {
                Indent = true, Encoding = Encoding.UTF8, Async = true
            };

#if NETCORE3_1
            using var xmlWriter = XmlWriter.Create(response.Body, xws);
#else
            await using var xmlWriter = XmlWriter.Create(response.Body, xws);
#endif
            var formatter     = new RssFormatter(_attributes, xmlWriter.Settings);
            var rssFeedWriter = await getRssFeedWriterAsync(xmlWriter);
            await writeSyndicationItemsAsync(formatter, rssFeedWriter);

            await xmlWriter.FlushAsync();
        }
 private async Task writeSyndicationItemsAsync(RssFormatter formatter, RssFeedWriter rssFeedWriter)
 {
     foreach (var item in getSyndicationItems())
     {
         var content = new SyndicationContent(formatter.CreateContent(item));
         content.AddField(new SyndicationContent("atom:updated", Atom10Namespace, item.LastUpdated.ToString("r")));
         await rssFeedWriter.Write(content);
     }
 }
Example #3
0
    public static async Task WriteCustomItem()
    {
        const string ExampleNs = "http://contoso.com/syndication/feed/examples";
        var          sw        = new StringWriterWithEncoding(Encoding.UTF8);

        using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings()
        {
            Async = true, Indent = true
        }))
        {
            var attributes = new List <SyndicationAttribute>()
            {
                new SyndicationAttribute("xmlns:example", ExampleNs)
            };

            var formatter = new RssFormatter(attributes, xmlWriter.Settings);
            var writer    = new RssFeedWriter(xmlWriter, attributes, formatter);

            // Create item
            var item = new SyndicationItem()
            {
                Title       = "Rss Writer Available",
                Description = "The new RSS Writer is now available as a NuGet package!",
                Id          = "https://www.nuget.org/packages/Microsoft.SyndicationFeed",
                Published   = DateTimeOffset.UtcNow
            };

            item.AddCategory(new SyndicationCategory("Technology"));
            item.AddContributor(new SyndicationPerson("test", "*****@*****.**"));

            //
            // Format the item as SyndicationContent
            var content = new SyndicationContent(formatter.CreateContent(item));

            // Add custom fields/attributes
            content.AddField(new SyndicationContent("customElement", ExampleNs, "Custom Value"));

            // Write
            await writer.Write(content);

            // Done
            xmlWriter.Flush();
        }

        Console.WriteLine(sw.ToString());
    }
Example #4
0
        public async Task FormatterWriterWithNamespaces()
        {
            const string ExampleNs = "http://contoso.com/syndication/feed/examples";
            var          sw        = new StringWriter();

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var attributes = new SyndicationAttribute[] { new SyndicationAttribute("xmlns:example", ExampleNs) };

                var formatter = new RssFormatter(attributes, xmlWriter.Settings);
                var writer    = new RssFeedWriter(xmlWriter, attributes, formatter);

                // Create item
                var item = new SyndicationItem()
                {
                    Title       = "Rss Writer Available",
                    Description = "The new RSS Writer is now open source!",
                    Id          = "https://github.com/dotnet/wcf/tree/lab/lab/src/Microsoft.SyndicationFeed/src",
                    Published   = DateTimeOffset.UtcNow
                };

                item.AddCategory(new SyndicationCategory("Technology"));
                item.AddContributor(new SyndicationPerson(null, "*****@*****.**"));

                //
                // Format the item as SyndicationContent
                var content = new SyndicationContent(formatter.CreateContent(item));

                // Add custom fields/attributes
                content.AddField(new SyndicationContent("customElement", ExampleNs, "Custom Value"));

                // Write
                await writer.Write(content);

                await writer.Write(content);

                await writer.Flush();
            }

            string res = sw.ToString();
        }
Example #5
0
 public SyndicationFeed Flush(string url)
 {
     try
     {
         RssFormatter formatter = new RssFormatter();
         var          result    = formatter.Formatter(url);
         if (_Feeds.ContainsKey(url) && _Feeds[url]?.items?.Count > 0)
         {
             if (result?.items?.Count > 0)
             {
                 HashSet <string> itemsSet = new HashSet <string>();
                 _Feeds[url].items.InsertRange(0, result.items);
                 result.items.Clear();
                 foreach (var i in _Feeds[url].items)
                 {
                     var key = _Calc.CalcFeedItemKey(i);
                     if (itemsSet.Contains(key))
                     {
                         result.items.Add(i);
                         continue;
                     }
                     itemsSet.Add(key);
                 }
                 _Feeds[url].items = result.items;
                 _Storage.SaveFeed(_Calc.CalcFeedKey(url), _Feeds[url]);
             }
         }
         else
         {
             _Feeds[url] = result;
             _Storage.SaveFeed(_Calc.CalcFeedKey(url), _Feeds[url]);
         }
         return(_Feeds[url]);
     }
     catch
     {
         return(null);
     }
 }
Example #6
0
 public void Init() {
     formatter = new RssFormatter();
 }
Example #7
0
 public void Init()
 {
     formatter = new RssFormatter();
 }
        public async Task <IActionResult> GetRssAsync()
        {
            var result = await Mediator.Send(new GetMaterialListQuery.Request
            {
                CurrentPage = 1,
                PageSize    = 10
            });

            var host     = Request.Scheme + "://" + Request.Host + "/";
            var sw       = new StringWriter();
            var settings = new XmlWriterSettings {
                Async = true, Indent = true
            };

            using (var xmlWriter = XmlWriter.Create(sw, settings))
            {
                var attributes = new List <SyndicationAttribute>
                {
                    new SyndicationAttribute("xmlns:link", host)
                };
                var writer = new RssFeedWriter(xmlWriter);
                await writer.WriteTitle("MyLFC.ru - новостная лента");

                var formatter = new RssFormatter(attributes, xmlWriter.Settings);

                foreach (var material in result.Results)
                {
                    var item = new SyndicationItem
                    {
                        Title       = material.Title,
                        Description = $"<img src='{material.PhotoPreview}' /><br/>{material.Brief}",
                        Id          = material.Id.ToString(),
                        Published   = material.AdditionTime,
                        LastUpdated = material.AdditionTime,
                    };

                    item.AddCategory(new SyndicationCategory(material.CategoryName));
                    item.AddContributor(new SyndicationPerson(material.UserName, material.UserName));
                    item.AddLink(new SyndicationLink(new Uri(host + material.TypeName + "/" + material.Id)));
                    //item.AddLink(new SyndicationLink(new Uri(host + material.TypeName + "/" + material.Id)));


                    // Format the item as SyndicationContent
                    var content = new SyndicationContent(formatter.CreateContent(item));

                    // Add custom fields/attributes
                    //content.AddAttribute(new SyndicationAttribute("img", host + material.Photo));
                    //content.AddField(new SyndicationContent("customElement", "321321", "Custom Value"));

                    await writer.Write(content);
                }
                xmlWriter.Flush();
            }

            var xml = new XmlDocument();

            xml.LoadXml(sw.ToString());

            return(Content(xml.InnerXml, "application/xml"));
            // return Ok(xml.InnerXml);
        }
Example #9
0
        public async Task Get(int id)
        {
            try
            {
                var series = await new Bonanza(_httpClient).GetSeries(id);
                Response.ContentType = "application/rss+xml";

                using (var xmlWriter = XmlWriter.Create(Response.Body, new XmlWriterSettings()
                {
                    Async = true,
                    Encoding = Encoding.UTF8
                }))
                {
                    var attributes = new List <SyndicationAttribute>()
                    {
                        new SyndicationAttribute("xmlns:itunes", _itunesNs),
                    };
                    var formatter  = new RssFormatter(attributes, xmlWriter.Settings);
                    var feedWriter = new RssFeedWriter(xmlWriter, attributes, formatter);

                    await WriteFeedPreamble(feedWriter);

                    await feedWriter.WriteTitle(series.Title);

                    await feedWriter.Write(new SyndicationContent("subtitle", _itunesNs, series.SubTitle));

                    await feedWriter.Write(new SyndicationLink(new Uri(series.Url)));

                    await feedWriter.Write(new SyndicationContent("author", _itunesNs, series.Author));

                    await feedWriter.Write(new SyndicationContent("summary", _itunesNs, series.Description));

                    foreach (var x in series.Episodes.OrderBy(x => x.Published)
                             .Select((value, i) => new { i, value }))
                    {
                        var episode = x.value;

                        var item = new SyndicationItem
                        {
                            Title       = episode.Title,
                            Description = episode.Description,
                            Published   = episode.Published,
                            Id          = $"{episode.WebUri}",
                        };

                        item.AddLink(new SyndicationLink(episode.MediaUri, RssLinkTypes.Enclosure)
                        {
                            MediaType = episode.MediaType,
                            Length    = episode.FileLength,
                        });
                        item.AddLink(new SyndicationLink(episode.WebUri));

                        var content = new SyndicationContent(formatter.CreateContent(item));
                        content.AddField(new SyndicationContent("summary", _itunesNs, episode.Description));
                        content.AddField(new SyndicationContent("author", _itunesNs, string.Join(", ", episode.Authors)));
                        content.AddField(new SyndicationContent("duration", _itunesNs,
                                                                episode.Duration.ToString(@"hh\:mm\:ss")));
                        content.AddField(new SyndicationContent("explicit", _itunesNs, "no"));
                        content.AddField(new SyndicationContent("episode", _itunesNs, $"{x.i + 1}"));
                        content.AddField(new SyndicationContent("language", _itunesNs, $"{_daCulture}"));
                        await feedWriter.Write(content);
                    }
                }
            }
            catch (SeriesNotFoundException)
            {
                Response.StatusCode = 404;
                return;
            }
        }
        public async static Task <string> GetFeedAsync(IEnumerable <Post> posts)
        {
            var          sw        = new StringWriterWithEncoding(Encoding.UTF8);
            const string ExampleNs = "https://feed.lucasteles.net";

            using (var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {
                Async = true, Indent = true
            }))
            {
                var attributes = new List <SyndicationAttribute>
                {
                    new SyndicationAttribute("xmlns:feed", ExampleNs)
                };

                var formatter = new RssFormatter(attributes, xmlWriter.Settings);
                var writer    = new RssFeedWriter(xmlWriter, null, new RssFormatter()
                {
                    UseCDATA = true
                });

                //
                // Add Title
                await writer.WriteTitle("Lucas Teles - Blog");

                //
                // Add Description
                await writer.WriteDescription("Tecnologia e inovação");

                //
                // Add Link
                await writer.Write(new SyndicationLink(new Uri("https://lucasteles.net")));

                //
                // Add managing editor
                await writer.Write(new SyndicationPerson("lucasteles", "*****@*****.**", RssContributorTypes.ManagingEditor));

                //
                // Add publish date
                await writer.WritePubDate(DateTimeOffset.UtcNow);


                //
                // Add Items
                foreach (var post in posts)
                {
                    var item = new SyndicationItem
                    {
                        Id          = $"https://lucasteles.net/p={post.Id}",
                        Title       = post.Title,
                        Description = post.Description,
                        Published   = post.Date,
                    };

                    item.AddLink(new SyndicationLink(new Uri($"https://lucasteles.net/{post.Path}")));
                    item.AddCategory(new SyndicationCategory("Technology"));
                    item.AddContributor(new SyndicationPerson("Lucas Teles", "*****@*****.**"));

                    // Format the item as SyndicationContent
                    var content = new SyndicationContent(formatter.CreateContent(item));
                    content.AddField(new SyndicationContent("content:encoded", string.Empty, post.Content));

                    // Write
                    await writer.Write(content);
                }

                //
                // Done
                xmlWriter.Flush();
            }

            return(sw.ToString());
        }
Example #11
0
 public static SyndicationFeed GetRssFeed(string url)
 {
     RssFormatter formatter = new RssFormatter();
     var result = formatter.Formatter(url);
     return result;
 }