Esempio n. 1
0
        public static async Task <string> ToAtom(this IList <Page> pages)
        {
            using (var stringWriter = new StringWriter())
            {
                using (var xmlWriter = XmlWriter.Create(stringWriter,
                                                        new XmlWriterSettings
                {
                    Async = true,
                    Indent = true,
                    Encoding = Encoding.UTF8,
                    WriteEndDocumentOnClose = true
                }))
                {
                    var atomWriter = new AtomFeedWriter(xmlWriter);
                    var tasks      = new List <Task>
                    {
                        atomWriter.WriteTitle(Settings.Title),
                        atomWriter.WriteSubtitle(Settings.Description),
                        atomWriter.WriteId(Settings.Domain),
                        atomWriter.Write(new SyndicationLink(new Uri(Settings.Domain))),
                        atomWriter.WriteUpdated(pages.First().Timestamp.ToLocalTime())
                    };
                    tasks.AddRange(pages.Select(p => atomWriter.Write(p.ToSyndicationItem())));
                    Task.WaitAll(tasks.ToArray());
                    await atomWriter.Flush();
                }

                return(stringWriter.ToString());
            }
        }
        public async Task WritePerson()
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            var p1 = new SyndicationPerson("John Doe", "*****@*****.**");
            var p2 = new SyndicationPerson("Jane Doe", "*****@*****.**", AtomContributorTypes.Contributor)
            {
                Uri = "www.contoso.com/janedoe"
            };

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(p1);

                await writer.Write(p2);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<author><name>{p1.Name}</name><email>{p1.Email}</email></author><contributor><name>{p2.Name}</name><email>{p2.Email}</email><uri>{p2.Uri}</uri></contributor>"));
        }
Esempio n. 3
0
        /// <summary>
        /// Write default meta data for an atom feed
        /// </summary>
        /// <param name="atomFeedWriter">the AtomFeedWriter</param>
        /// <param name="atomFeedConfiguration">the configuration of the atom feed</param>
        /// <returns></returns>
        public static async Task WriteDefaultMetadata(
            this AtomFeedWriter atomFeedWriter,
            AtomFeedConfiguration atomFeedConfiguration)
        {
            await atomFeedWriter.WriteId(atomFeedConfiguration.Id);

            await atomFeedWriter.WriteTitle(atomFeedConfiguration.Title);

            await atomFeedWriter.WriteSubtitle(atomFeedConfiguration.Subtitle);

            await atomFeedWriter.WriteGenerator(atomFeedConfiguration.GeneratorTitle, atomFeedConfiguration.GeneratorUri, atomFeedConfiguration.GeneratorVersion);

            await atomFeedWriter.WriteRights(atomFeedConfiguration.Rights);

            await atomFeedWriter.WriteUpdated(atomFeedConfiguration.Updated);

            await atomFeedWriter.Write(atomFeedConfiguration.Author);

            await atomFeedWriter.Write(atomFeedConfiguration.SelfUri);

            foreach (var alternateUri in atomFeedConfiguration.AlternateUris)
            {
                await atomFeedWriter.Write(alternateUri);
            }

            foreach (var relatedUri in atomFeedConfiguration.RelatedUris)
            {
                await atomFeedWriter.Write(relatedUri);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Write default meta data for an atom feed
        /// </summary>
        /// <param name="atomFeedWriter">the AtomFeedWriter</param>
        /// <param name="id">the id of the feed</param>
        /// <param name="title">the title of the feed</param>
        /// <param name="version">the version of the feed</param>
        /// <param name="selfUri">the self referencing uri of the feed</param>
        /// <param name="relatedUrls">optional related urls</param>
        /// <returns></returns>
        public static async Task WriteDefaultMetadata(
            this AtomFeedWriter atomFeedWriter,
            string id,
            string title,
            string version,
            Uri selfUri,
            params string[] relatedUrls)
        {
            await atomFeedWriter.WriteId(id);

            await atomFeedWriter.WriteTitle(title);

            await atomFeedWriter.WriteSubtitle("Basisregisters Vlaanderen stelt u in staat om alles te weten te komen rond: de Belgische gemeenten; de Belgische postcodes; de Vlaamse straatnamen; de Vlaamse adressen; de Vlaamse gebouwen en gebouweenheden; de Vlaamse percelen; de Vlaamse organisaties en organen; de Vlaamse dienstverlening.");

            await atomFeedWriter.WriteGenerator("Basisregisters Vlaanderen", "https://basisregisters.vlaanderen.be", version);

            await atomFeedWriter.WriteRights($"Copyright (c) 2017-{DateTime.Now.Year}, Informatie Vlaanderen");

            await atomFeedWriter.WriteUpdated(DateTimeOffset.UtcNow);

            await atomFeedWriter.Write(new SyndicationPerson("agentschap Informatie Vlaanderen", "*****@*****.**", AtomContributorTypes.Author));

            await atomFeedWriter.Write(new SyndicationLink(selfUri, AtomLinkTypes.Self));

            foreach (var relatedUrl in relatedUrls)
            {
                await atomFeedWriter.Write(new SyndicationLink(new Uri(relatedUrl), AtomLinkTypes.Related));
            }
        }
Esempio n. 5
0
        public override async Task ExecuteResultAsync(ActionContext context)
        {
            using (XmlWriter xmlWriter = XmlWriter.Create(context.HttpContext.Response.Body, new XmlWriterSettings {
                Async = true, Indent = true
            }))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                var uniqueFeedIdBuilder = new UriBuilder(m_feedRequestUrl)
                {
                    Scheme = Uri.UriSchemeHttp,
                    Query  = string.Empty
                };
                var uniqueFeedId = uniqueFeedIdBuilder.ToString();
                await writer.WriteId(uniqueFeedId);

                await writer.WriteTitle(m_feedTitle);

                //await writer.WriteDescription(m_feedTitle);
                await writer.Write(new SyndicationLink(m_feedRequestUrl));

                //await writer.WriteUpdated(DateTimeOffset.UtcNow);

                foreach (var syndicationItem in m_feedItems)
                {
                    syndicationItem.Id = $"{uniqueFeedId}?itemId={syndicationItem.Id}";
                    await writer.Write(syndicationItem);
                }

                xmlWriter.Flush();
            }
        }
Esempio n. 6
0
    public async Task <string> WriteAtomAsync()
    {
        var feed = GetItemCollection(FeedItemCollection);

        var sw = new StringWriterWithEncoding(Encoding.UTF8);

        await using (var xmlWriter = XmlWriter.Create(sw, new() { Async = true }))
        {
            var writer = new AtomFeedWriter(xmlWriter);

            await writer.WriteTitle(HeadTitle);

            await writer.WriteSubtitle(HeadDescription);

            await writer.WriteRights(Copyright);

            await writer.WriteUpdated(DateTime.UtcNow);

            await writer.WriteGenerator(Generator, HostUrl, GeneratorVersion);

            foreach (var item in feed)
            {
                await writer.Write(item);
            }

            await xmlWriter.FlushAsync();

            xmlWriter.Close();
        }

        var xml = sw.ToString();

        return(xml);
    }
Esempio n. 7
0
        public async Task WriteAtom10FileAsync(string absolutePath)
        {
            var feed     = GetSyndicationItemCollection(FeedItemCollection);
            var settings = new XmlWriterSettings
            {
                Async    = true,
                Encoding = Encoding.UTF8,
                Indent   = true
            };

            using var xmlWriter = XmlWriter.Create(absolutePath, settings);
            var writer = new AtomFeedWriter(xmlWriter);

            await writer.WriteTitle(HeadTitle);

            await writer.WriteSubtitle(HeadDescription);

            await writer.WriteRights(Copyright);

            await writer.WriteUpdated(DateTime.UtcNow);

            await writer.WriteGenerator(Generator, HostUrl, GeneratorVersion);

            foreach (var item in feed)
            {
                await writer.Write(item);
            }

            await xmlWriter.FlushAsync();

            xmlWriter.Close();
        }
        public async Task WriteEntry()
        {
            var link      = new SyndicationLink(new Uri("https://contoso.com/alternate"));
            var related   = new SyndicationLink(new Uri("https://contoso.com/related"), AtomLinkTypes.Related);
            var self      = new SyndicationLink(new Uri("https://contoso.com/28af09b3"), AtomLinkTypes.Self);
            var enclosure = new SyndicationLink(new Uri("https://contoso.com/podcast"), AtomLinkTypes.Enclosure)
            {
                Title     = "Podcast",
                MediaType = "audio/mpeg",
                Length    = 4123
            };
            var source = new SyndicationLink(new Uri("https://contoso.com/source"), AtomLinkTypes.Source)
            {
                Title       = "Blog",
                LastUpdated = DateTimeOffset.UtcNow.AddDays(-10)
            };
            var author   = new SyndicationPerson("John Doe", "*****@*****.**");
            var category = new SyndicationCategory("Lorem Category");

            //
            // Construct entry
            var entry = new AtomEntry()
            {
                Id          = "https://contoso.com/28af09b3",
                Title       = "Lorem Ipsum",
                Description = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit...",
                LastUpdated = DateTimeOffset.UtcNow,
                ContentType = "text/html",
                Summary     = "Proin egestas sem in est feugiat, id laoreet massa dignissim",
                Rights      = $"copyright (c) {DateTimeOffset.UtcNow.Year}"
            };

            entry.AddLink(link);
            entry.AddLink(enclosure);
            entry.AddLink(related);
            entry.AddLink(source);
            entry.AddLink(self);

            entry.AddContributor(author);

            entry.AddCategory(category);

            //
            // Write
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(entry);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<entry><id>{entry.Id}</id><title>{entry.Title}</title><updated>{entry.LastUpdated.ToString("r")}</updated><link href=\"{link.Uri}\" /><link title=\"{enclosure.Title}\" href=\"{enclosure.Uri}\" rel=\"{enclosure.RelationshipType}\" type=\"{enclosure.MediaType}\" length=\"{enclosure.Length}\" /><link href=\"{related.Uri}\" rel=\"{related.RelationshipType}\" /><source><title>{source.Title}</title><link href=\"{source.Uri}\" /><updated>{source.LastUpdated.ToString("r")}</updated></source><link href=\"{self.Uri}\" rel=\"{self.RelationshipType}\" /><author><name>{author.Name}</name><email>{author.Email}</email></author><category term=\"{category.Name}\" /><content type=\"{entry.ContentType}\">{entry.Description}</content><summary>{entry.Summary}</summary><rights>{entry.Rights}</rights></entry>"));
        }
Esempio n. 9
0
        public async Task ExecuteResultAsync(ActionContext context)
        {
            var response = context.HttpContext.Response;
            var request  = context.HttpContext.Request;

            response.ContentType = "application/atom+xml";

            var host = request.Scheme + "://" + request.Host + "/";

            using (var xmlWriter = XmlWriter.Create(response.Body, new XmlWriterSettings()
            {
                Async = true, Indent = true, Encoding = new UTF8Encoding(true)
            }))
            {
                var atom = new AtomFeedWriter(xmlWriter);
                await atom.WriteTitle(_options.Value.Title);

                await atom.WriteId(host);

                await atom.WriteSubtitle(_options.Value.Description);

                await atom.WriteRaw($"\n  <link rel=\"self\" type=\"application/atom+xml\" href=\"{host}feed/atom\"/>");

                await atom.WriteGenerator(_options.Value.GeneratorDescription, _options.Value.SourceCodeLink, "1.0");

                var lastPost = _page.Items.FirstOrDefault();
                if (lastPost != null)
                {
                    await atom.WriteValue("updated", lastPost.PublishedDate.Value.ToString("yyyy-MM-ddTHH:mm:ssZ"));
                }


                foreach (var post in _page.Items)
                {
                    var item = new AtomEntry
                    {
                        Title       = post.Title,
                        Description = post.GetContentWithoutDataSrc(),
                        Id          = $"{host}/{post.Alias}",
                        Published   = post.PublishedDate.Value,
                        LastUpdated = post.ModifiedDate,
                        ContentType = "html",
                    };

                    foreach (var tag in post.BlogStoryTags)
                    {
                        item.AddCategory(new SyndicationCategory(tag.Tag.Name));
                    }

                    item.AddContributor(new SyndicationPerson(_options.Value.AuthorName, _options.Value.Email, "author"));
//                    item.AddLink(new SyndicationLink(new Uri($"{host}/{post.Alias}")));

                    await atom.Write(item);
                }
            }
        }
Esempio n. 10
0
        public async Task <string> GenerateFeed(IEnumerable <ReleaseViewModel> releases)
        {
            const string title   = "Changelog Feed";
            var          id      = new UniqueId();
            var          updated = DateTimeOffset.UtcNow;

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

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

                await writer.WriteTitle(title);

                await writer.WriteId(id.ToString());

                await writer.WriteUpdated(updated);

                foreach (var release in releases)
                {
                    var item = new SyndicationItem()
                    {
                        Title       = release.ReleaseVersion,
                        Id          = new UniqueId(release.ReleaseId).ToString(),
                        LastUpdated = release.ReleaseDate
                    };

                    release.Authors.ForEach(x =>
                                            item.AddContributor(new SyndicationPerson(x, $"{x}@test.com"))
                                            );

                    var sb = new StringBuilder();
                    foreach (var workItemViewModel in release.WorkItems)
                    {
                        sb.Append(workItemViewModel.WorkItemTypeString)
                        .Append(": ")
                        .AppendLine(workItemViewModel.Description)
                        ;
                    }
                    item.Description = sb.ToString();

                    await writer.Write(item);
                }

                xmlWriter.Flush();
            }

            return(sw.GetStringBuilder().ToString());
        }
        public async Task WriteImage()
        {
            var icon = new SyndicationImage(new Uri("http://contoso.com/icon.ico"), AtomImageTypes.Icon);
            var logo = new SyndicationImage(new Uri("http://contoso.com/logo.png"), AtomImageTypes.Logo);

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(icon);

                await writer.Write(logo);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<icon>{icon.Url}</icon><logo>{logo.Url}</logo>"));
        }
        private static async Task <string> BuildAtomFeed(
            DateTimeOffset lastUpdate,
            PagedQueryable <MunicipalitySyndicationQueryResult> pagedMunicipalities,
            IOptions <ResponseOptions> responseOptions,
            IConfiguration configuration)
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {
                Async = true, Indent = true, Encoding = sw.Encoding
            }))
            {
                var formatter = new AtomFormatter(null, xmlWriter.Settings)
                {
                    UseCDATA = true
                };
                var writer = new AtomFeedWriter(xmlWriter, null, formatter);
                var syndicationConfiguration = configuration.GetSection("Syndication");
                var atomConfiguration        = AtomFeedConfigurationBuilder.CreateFrom(syndicationConfiguration, lastUpdate);

                await writer.WriteDefaultMetadata(atomConfiguration);

                var municipalities = await pagedMunicipalities.Items.ToListAsync();

                var highestPosition = municipalities.Any()
                    ? municipalities.Max(x => x.Position)
                    : (long?)null;

                var nextUri = BuildNextSyncUri(
                    pagedMunicipalities.PaginationInfo.Limit,
                    highestPosition + 1,
                    syndicationConfiguration["NextUri"]);

                if (nextUri != null)
                {
                    await writer.Write(new SyndicationLink(nextUri, GrArAtomLinkTypes.Next));
                }

                foreach (var municipality in municipalities)
                {
                    await writer.WriteMunicipality(responseOptions, formatter, syndicationConfiguration["Category"], municipality);
                }

                xmlWriter.Flush();
            }

            return(sw.ToString());
        }
Esempio n. 13
0
        private static async Task <string> BuildAtomFeed(
            DateTimeOffset lastFeedUpdate,
            PagedQueryable <AddressSyndicationQueryResult> pagedAddresses,
            IOptions <ResponseOptions> responseOptions,
            IConfiguration configuration)
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {
                Async = true, Indent = true, Encoding = sw.Encoding
            }))
            {
                var formatter = new AtomFormatter(null, xmlWriter.Settings)
                {
                    UseCDATA = true
                };
                var writer = new AtomFeedWriter(xmlWriter, null, formatter);
                var syndicationConfiguration = configuration.GetSection("Syndication");
                var atomFeedConfig           = AtomFeedConfigurationBuilder.CreateFrom(syndicationConfiguration, lastFeedUpdate);

                await writer.WriteDefaultMetadata(atomFeedConfig);

                var addresses = pagedAddresses.Items.ToList();

                var nextFrom = addresses.Any()
                    ? addresses.Max(x => x.Position) + 1
                    : (long?)null;

                var nextUri = BuildNextSyncUri(pagedAddresses.PaginationInfo.Limit, nextFrom, syndicationConfiguration["NextUri"]);
                if (nextUri != null)
                {
                    await writer.Write(new SyndicationLink(nextUri, "next"));
                }

                foreach (var address in addresses)
                {
                    await writer.WriteAddress(responseOptions, formatter, syndicationConfiguration["Category"], address);
                }

                xmlWriter.Flush();
            }

            return(sw.ToString());
        }
Esempio n. 14
0
        public async Task AddSource(FeedSource source)
        {
            await Console.Error.WriteLineAsync("[Builder] Downloading content");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(source.Feed.Url);

            request.UserAgent = UserAgentHelper.UserAgent;
            WebResponse response = await request.GetResponseAsync();

            await Console.Error.WriteLineAsync("[Builder] Generating feed header");

            // Write the header
            await feed.WriteGenerator("Polyfeed", "https://github.com/sbrl/PolyFeed.git", Program.GetProgramVersion());

            await feed.WriteId(source.Feed.Url);

            await feed.Write(new SyndicationLink(new Uri(source.Feed.Url), AtomLinkTypes.Self));

            string lastModified = response.Headers.Get("last-modified");

            if (string.IsNullOrWhiteSpace(lastModified))
            {
                await feed.WriteUpdated(DateTimeOffset.Now);
            }
            else
            {
                await feed.WriteUpdated(DateTimeOffset.Parse(lastModified));
            }

            string contentType = response.Headers.Get("content-type");

            IParserProvider provider = GetProvider(source.Feed.SourceType);

            if (provider == null)
            {
                throw new ApplicationException($"Error: A provider for the source type {source.Feed.SourceType} wasn't found.");
            }

            provider.SetOutputFeed(feed, xml);
            await provider.ParseWebResponse(source, response);

            await Console.Error.WriteLineAsync("[Builder] Done!");
        }
        public async Task WriteCategory()
        {
            var category = new SyndicationCategory("Test Category");

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(category);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<category term=\"{category.Name}\" />"));
        }
        private static async Task <string> BuildAtomFeed(
            PagedQueryable <PostalInformationSyndicationQueryResult> pagedPostalInfoItems,
            IOptions <ResponseOptions> responseOptions,
            IConfiguration configuration)
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            using (var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {
                Async = true, Indent = true, Encoding = sw.Encoding
            }))
            {
                var formatter = new AtomFormatter(null, xmlWriter.Settings)
                {
                    UseCDATA = true
                };
                var writer = new AtomFeedWriter(xmlWriter, null, formatter);
                var syndicationConfiguration = configuration.GetSection("Syndication");

                await writer.WriteDefaultMetadata(
                    syndicationConfiguration["Id"],
                    syndicationConfiguration["Title"],
                    Assembly.GetEntryAssembly().GetName().Version.ToString(),
                    new Uri(syndicationConfiguration["Self"]),
                    syndicationConfiguration.GetSection("Related").GetChildren().Select(c => c.Value).ToArray());

                var nextUri = BuildVolgendeUri(pagedPostalInfoItems.PaginationInfo, syndicationConfiguration["NextUri"]);
                if (nextUri != null)
                {
                    await writer.Write(new SyndicationLink(nextUri, GrArAtomLinkTypes.Next));
                }

                foreach (var postalInfo in pagedPostalInfoItems.Items)
                {
                    await writer.WritePostalInfo(responseOptions, formatter, syndicationConfiguration["Category"], postalInfo);
                }

                xmlWriter.Flush();
            }

            return(sw.ToString());
        }
Esempio n. 17
0
        public async Task <string> WriteAtomAsync()
        {
            var feed     = GetItemCollection(FeedItemCollection);
            var settings = new XmlWriterSettings
            {
                Async = true
            };

            var sb = new StringBuilder();

            await using (var xmlWriter = XmlWriter.Create(sb, settings))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.WriteTitle(HeadTitle);

                await writer.WriteSubtitle(HeadDescription);

                await writer.WriteRights(Copyright);

                await writer.WriteUpdated(DateTime.UtcNow);

                await writer.WriteGenerator(Generator, HostUrl, GeneratorVersion);

                foreach (var item in feed)
                {
                    await writer.Write(item);
                }

                await xmlWriter.FlushAsync();

                xmlWriter.Close();
            }

            var xml = sb.ToString();

            return(xml);
        }
        public async Task WriteLink()
        {
            var sw = new StringWriterWithEncoding(Encoding.UTF8);

            var link = new SyndicationLink(new Uri("http://contoso.com"))
            {
                Title     = "Test title",
                Length    = 123,
                MediaType = "mp3/video"
            };

            using (var xmlWriter = XmlWriter.Create(sw))
            {
                var writer = new AtomFeedWriter(xmlWriter);

                await writer.Write(link);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<link title=\"{link.Title}\" href=\"{link.Uri}\" type=\"{link.MediaType}\" length=\"{link.Length}\" />"));
        }
Esempio n. 19
0
        private async Task addEntry(FeedSource source, HtmlNode nextNode)
        {
            HtmlNode urlNode = nextNode.QuerySelector(source.Entries.Url.Selector);

            if (urlNode == null)
            {
                throw new ApplicationException("Error: Failed to match entry url selector against an element.");
            }

            string url = source.Entries.Url.Attribute == string.Empty ?
                         urlNode.InnerText : urlNode.Attributes[source.Entries.Url.Attribute].DeEntitizeValue;

            Uri       entryUri = new Uri(new Uri(source.Feed.Url), new Uri(url));
            AtomEntry nextItem = new AtomEntry()
            {
                Id          = entryUri.ToString(),
                Title       = ReferenceSubstitutor.Replace(source.Entries.Title, nextNode),
                Description = ReferenceSubstitutor.Replace(source.Entries.Content, nextNode),
                ContentType = "html"
            };

            nextItem.AddLink(new SyndicationLink(entryUri, AtomLinkTypes.Alternate));

            if (source.Entries.Published != null)
            {
                nextItem.Published = DateTime.Parse(
                    nextNode.QuerySelectorAttributeOrText(
                        source.Entries.Published
                        )
                    );
            }

            if (source.Entries.LastUpdated != null)
            {
                nextItem.LastUpdated = DateTime.Parse(
                    nextNode.QuerySelectorAttributeOrText(
                        source.Entries.LastUpdated
                        )
                    );
            }
            else if (source.Entries.Published != null)             // Use the publish date if available
            {
                nextItem.LastUpdated = nextItem.Published;
            }
            else             // It requires one, apparently
            {
                nextItem.LastUpdated = DateTimeOffset.Now;
            }


            if (source.Entries.AuthorName != null)
            {
                SyndicationPerson author = new SyndicationPerson(
                    nextNode.QuerySelectorAttributeOrText(source.Entries.AuthorName).Trim(),
                    ""
                    );
                if (source.Entries.AuthorUrl != null)
                {
                    author.Uri = nextNode.QuerySelectorAttributeOrText(source.Entries.AuthorUrl);
                }

                nextItem.AddContributor(author);
            }
            else
            {
                nextItem.AddContributor(new SyndicationPerson("Unknown", ""));
            }


            await feed.Write(nextItem);
        }
Esempio n. 20
0
        private async Task <string> GetSyndicationItems(string id)
        {
            // See if we already have the items in the cache
            if (_cache.TryGetValue($"{id}_items", out string s))
            {
                Log.Information("CACHE HIT: Returning {bytes} bytes", s.Length);
                return(s);
            }

            Log.Information("CACHE MISS: Loading feed items for {id}", id);
            var sb           = new StringBuilder();
            var stringWriter = new StringWriterWithEncoding(sb, Encoding.UTF8);
            int days         = 5;
            var feed         = GetFeed(id);

            using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings()
            {
                Async = true, Indent = true, Encoding = Encoding.UTF8
            }))
            {
                var rssWriter = new AtomFeedWriter(xmlWriter);

                await rssWriter.WriteTitle(feed.title);

                await rssWriter.Write(new SyndicationLink(new Uri(feed.url)));

                await rssWriter.WriteUpdated(DateTimeOffset.UtcNow);

                // Add Items
                foreach (var item in await GetFeedItems(id.ToLowerInvariant(), days))
                {
                    try
                    {
                        var si = new SyndicationItem()
                        {
                            Id          = item.Id,
                            Title       = item.Title.Replace("\u0008", "").Replace("\u0003", "").Replace("\u0010", "").Replace("\u0012", "").Replace("\u0002", "").Replace("\u001f", ""),
                            Description = item.ArticleText.Replace("\u0008", "").Replace("\u0003", "").Replace("\u0010", "").Replace("\u0012", "").Replace("\u0002", "").Replace("\u001f", ""),
                            Published   = item.DateAdded,
                            LastUpdated = item.DateAdded
                        };

                        si.AddLink(new SyndicationLink(new Uri(item.Url)));
                        si.AddContributor(new SyndicationPerson(string.IsNullOrWhiteSpace(item.SiteName) ? item.HostName : item.SiteName, feed.authoremail, AtomContributorTypes.Author));

                        await rssWriter.Write(si);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex, "Error building item {urlHash}:{url}", item.UrlHash, item.Url);
                    }
                }

                xmlWriter.Flush();
            }

            // Add the items to the cache before returning
            s = stringWriter.ToString();
            _cache.Set <string>($"{id}_items", s, TimeSpan.FromMinutes(60));
            Log.Information("CACHE SET: Storing feed items for {id} for {minutes} minutes", id, 60);

            return(s);
        }