Beispiel #1
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);
        }
Beispiel #2
0
        private ISyndicationContent GetOwner()
        {
            var ownerContent = new SyndicationContent("owner", _itunesNs, null);

            ownerContent.AddField(new SyndicationContent("name", _itunesNs, "Michael Friis"));
            ownerContent.AddField(new SyndicationContent("email", _itunesNs, "*****@*****.**"));

            return(ownerContent);
        }
 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);
     }
 }
Beispiel #4
0
        public virtual ISyndicationContent CreateContent(ISyndicationPerson person)
        {
            if (person == null)
            {
                throw new ArgumentNullException(nameof(person));
            }

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

            string contributorType = person.RelationshipType ?? AtomContributorTypes.Author;

            if (contributorType != AtomContributorTypes.Author &&
                contributorType != AtomContributorTypes.Contributor)
            {
                throw new ArgumentException("RelationshipType");
            }

            var result = new SyndicationContent(contributorType);

            //
            // name
            result.AddField(new SyndicationContent(AtomElementNames.Name, person.Name));

            //
            // email
            if (!string.IsNullOrEmpty(person.Email))
            {
                result.AddField(new SyndicationContent(AtomElementNames.Email, person.Email));
            }

            //
            // uri
            if (person.Uri != null)
            {
                result.AddField(new SyndicationContent(AtomElementNames.Uri, FormatValue(person.Uri)));
            }

            return(result);
        }
        private static ISyndicationContent ReadSyndicationContent(XmlReader reader)
        {
            var content = new SyndicationContent(reader.LocalName, reader.NamespaceURI, null);

            //
            // Attributes
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    ISyndicationAttribute attr = reader.ReadSyndicationAttribute();

                    if (attr != null)
                    {
                        content.AddAttribute(attr);
                    }
                }

                reader.MoveToContent();
            }

            //
            // Content
            if (!reader.IsEmptyElement)
            {
                reader.ReadStartElement();

                //
                // Value
                if (reader.HasValue)
                {
                    content.Value = reader.ReadContentAsString();
                }
                //
                // Children
                else
                {
                    while (reader.IsStartElement())
                    {
                        content.AddField(ReadSyndicationContent(reader));
                    }
                }

                reader.ReadEndElement(); // end
            }
            else
            {
                reader.Skip();
            }

            return(content);
        }
Beispiel #6
0
        public virtual ISyndicationContent CreateContent(ISyndicationImage image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            // Required URL - Title - Link
            if (string.IsNullOrEmpty(image.Title))
            {
                throw new ArgumentNullException("Image requires a title");
            }

            if (image.Link == null)
            {
                throw new ArgumentNullException("Image requires a link");
            }

            if (image.Url == null)
            {
                throw new ArgumentNullException("Image requires an url");
            }

            var content = new SyndicationContent(RssElementNames.Image);

            // Write required contents of image
            content.AddField(new SyndicationContent(RssElementNames.Url, FormatValue(image.Url)));
            content.AddField(new SyndicationContent(RssElementNames.Title, image.Title));
            content.AddField(CreateContent(image.Link));


            // Write optional elements
            if (!string.IsNullOrEmpty(image.Description))
            {
                content.AddField(new SyndicationContent(RssElementNames.Description, image.Description));
            }

            return(content);
        }
        public virtual Task WriteSkipDays(IEnumerable <DayOfWeek> days)
        {
            if (days == null)
            {
                throw new ArgumentNullException(nameof(days));
            }

            var skipDays = new SyndicationContent(RssElementNames.SkipDays);

            foreach (var d in days)
            {
                skipDays.AddField(new SyndicationContent("day", Formatter.FormatValue(d)));
            }

            return(Write(skipDays));
        }
Beispiel #8
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());
    }
Beispiel #9
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();
        }
        public virtual Task WriteSkipHours(IEnumerable <byte> hours)
        {
            if (hours == null)
            {
                throw new ArgumentNullException(nameof(hours));
            }

            var skipHours = new SyndicationContent(RssElementNames.SkipHours);

            foreach (var h in hours)
            {
                if (h < 0 || h > 23)
                {
                    throw new ArgumentOutOfRangeException("Hour value must be between 0 and 23");
                }

                skipHours.AddField(new SyndicationContent("hour", Formatter.FormatValue(h)));
            }

            return(Write(skipHours));
        }
Beispiel #11
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);
        }
Beispiel #12
0
        public virtual ISyndicationContent CreateContent(ISyndicationItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (string.IsNullOrEmpty(item.Id))
            {
                throw new ArgumentNullException("Id");
            }

            if (string.IsNullOrEmpty(item.Title))
            {
                throw new ArgumentNullException("Title");
            }

            if (item.LastUpdated == default(DateTimeOffset))
            {
                throw new ArgumentException("LastUpdated");
            }

            var result = new SyndicationContent(AtomElementNames.Entry);

            //
            // id
            result.AddField(new SyndicationContent(AtomElementNames.Id, item.Id));

            //
            // title
            result.AddField(new SyndicationContent(AtomElementNames.Title, item.Title));

            //
            // updated
            result.AddField(new SyndicationContent(AtomElementNames.Updated, FormatValue(item.LastUpdated)));

            //
            // published
            if (item.Published != default(DateTimeOffset))
            {
                result.AddField(new SyndicationContent(AtomElementNames.Published, FormatValue(item.Published)));
            }

            //
            // link
            bool hasContentLink   = false;
            bool hasAlternateLink = false;

            if (item.Links != null)
            {
                foreach (var link in item.Links)
                {
                    if (link.RelationshipType == AtomLinkTypes.Content)
                    {
                        if (hasContentLink)
                        {
                            throw new ArgumentNullException("Multiple content links are not allowed");
                        }

                        hasContentLink = true;
                    }
                    else if (link.RelationshipType == null || link.RelationshipType == AtomLinkTypes.Alternate)
                    {
                        hasAlternateLink = true;
                    }

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

            //
            // author/contributor
            bool hasAuthor = false;

            if (item.Contributors != null)
            {
                foreach (var c in item.Contributors)
                {
                    if (c.RelationshipType == null || c.RelationshipType == AtomContributorTypes.Author)
                    {
                        hasAuthor = true;
                    }

                    result.AddField(CreateContent(c));
                }
            }

            if (!hasAuthor)
            {
                throw new ArgumentException("Author is required");
            }

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

            IAtomEntry entry = item as IAtomEntry;

            //
            // content
            if (!string.IsNullOrEmpty(item.Description))
            {
                if (hasContentLink)
                {
                    throw new ArgumentException("Description and content link are not allowed simultaneously");
                }

                var content = new SyndicationContent(AtomElementNames.Content, item.Description);

                //
                // type
                if (entry != null &&
                    !(string.IsNullOrEmpty(entry.ContentType) || entry.ContentType.Equals(AtomConstants.PlainTextContentType, StringComparison.OrdinalIgnoreCase)))
                {
                    content.AddAttribute(new SyndicationAttribute(AtomConstants.Type, entry.ContentType));
                }

                result.AddField(content);
            }
            else
            {
                if (!(hasContentLink || hasAlternateLink))
                {
                    throw new ArgumentException("Description or alternate link is required");
                }
            }

            if (entry != null)
            {
                //
                // summary
                if (!string.IsNullOrEmpty(entry.Summary))
                {
                    result.AddField(new SyndicationContent(AtomElementNames.Summary, entry.Summary));
                }

                //
                // rights
                if (!string.IsNullOrEmpty(entry.Rights))
                {
                    result.AddField(new SyndicationContent(AtomElementNames.Rights, entry.Rights));
                }
            }

            return(result);
        }
Beispiel #13
0
        private static ISyndicationContent ReadSyndicationContent(XmlReader reader)
        {
            string type = null;

            var content = new SyndicationContent(reader.LocalName, reader.NamespaceURI, null);

            //
            // Attributes
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    ISyndicationAttribute attr = reader.ReadSyndicationAttribute();

                    if (attr != null)
                    {
                        if (type == null && attr.IsAtom(AtomConstants.Type))
                        {
                            type = attr.Value;
                        }

                        content.AddAttribute(attr);
                    }
                }

                reader.MoveToContent();
            }

            //
            // Content
            if (!reader.IsEmptyElement)
            {
                //
                // Xml (applies to <content>)
                if (XmlUtils.IsXmlMediaType(type) && content.IsAtom(AtomElementNames.Content))
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        throw new FormatException($"Invalid Xml element");
                    }

                    content.Value = reader.ReadInnerXml();
                }
                else
                {
                    reader.ReadStartElement();

                    //
                    // Xhtml
                    if (XmlUtils.IsXhtmlMediaType(type) && content.IsAtom())
                    {
                        if (reader.NamespaceURI != AtomConstants.XhtmlNamespace)
                        {
                            throw new FormatException($"Invalid Xhtml namespace");
                        }

                        content.Value = reader.ReadInnerXml();
                    }
                    //
                    // Text/Html
                    else if (reader.HasValue)
                    {
                        content.Value = reader.ReadContentAsString();
                    }
                    //
                    // Children
                    else
                    {
                        while (reader.IsStartElement())
                        {
                            content.AddField(ReadSyndicationContent(reader));
                        }
                    }

                    reader.ReadEndElement(); // end
                }
            }
            else
            {
                reader.Skip();
            }

            return(content);
        }
Beispiel #14
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());
        }
Beispiel #16
0
    public static async Task WriteFeed()
    {
        var sw = new StringWriterWithEncoding(Encoding.UTF8);

        using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings()
        {
            Async = true, Indent = true
        }))
        {
            var writer = new RssFeedWriter(xmlWriter);

            //
            // Add Title
            await writer.WriteTitle("Example of RssFeedWriter");

            //
            // Add Description
            await writer.WriteDescription("Hello World, RSS 2.0!");

            //
            // Add Link
            await writer.Write(new SyndicationLink(new Uri("https://github.com/dotnet/SyndicationFeedReaderWriter")));

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

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

            //
            // Add custom element
            var customElement = new SyndicationContent("customElement");

            customElement.AddAttribute(new SyndicationAttribute("attr1", "true"));
            customElement.AddField(new SyndicationContent("Company", "Contoso"));

            await writer.Write(customElement);

            //
            // Add Items
            for (int i = 0; i < 5; ++i)
            {
                var item = new SyndicationItem()
                {
                    Id          = "https://www.nuget.org/packages/Microsoft.SyndicationFeed.ReaderWriter",
                    Title       = $"Item #{i + 1}",
                    Description = "The new Microsoft.SyndicationFeed.ReaderWriter is now available as a NuGet package!",
                    Published   = DateTimeOffset.UtcNow
                };

                item.AddLink(new SyndicationLink(new Uri("https://github.com/dotnet/SyndicationFeedReaderWriter")));
                item.AddCategory(new SyndicationCategory("Technology"));
                item.AddContributor(new SyndicationPerson("user", "*****@*****.**"));

                await writer.Write(item);
            }

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

        //
        // Ouput the feed
        Console.WriteLine(sw.ToString());
    }