Example #1
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));
            }
        }
Example #2
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();
        }
Example #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);
            }
        }
Example #4
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 WriteValue()
        {
            const string   title   = "Example Feed";
            Guid           id      = Guid.NewGuid();
            DateTimeOffset updated = DateTimeOffset.UtcNow.AddDays(-21);

            var sw = new StringWriterWithEncoding(Encoding.UTF8);

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

                await writer.WriteTitle(title);

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

                await writer.WriteUpdated(updated);

                await writer.Flush();
            }

            string res = sw.ToString();

            Assert.True(CheckResult(res, $"<title>{title}</title><id>{id}</id><updated>{updated.ToString("r")}</updated>"));
        }
Example #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);
    }
Example #7
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!");
        }
Example #8
0
        private XmlFeedWriter InitializeAtomFeedWriter(XmlWriter xmlWriter)
        {
            var result = new AtomFeedWriter(xmlWriter);

            result.WriteTitle(_siteSettings.SiteName);
            result.WriteSubtitle(_siteSettings.Description);
            result.WriteRights(_siteSettings.Copyright);
            result.WriteUpdated(_blogPostsConfig.Blogs.Where(x => x.Published).First().CreateDate.Date);
            result.WriteGenerator(_siteSettings.Nickname, _siteSettings.PersonalSiteURL, _siteSettings.Version);
            return(result);
        }
Example #9
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());
        }
Example #10
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);
        }
Example #11
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);
        }