예제 #1
0
파일: RssModel.cs 프로젝트: frapid/frapid
        public static async Task<RssChannel> GetRssChannelAsync(string tenant, HttpContext context, string categoryAlias,
            int pageNumber)
        {
            string url = context.Request.Url.DnsSafeHost;
            string title = Configuration.Get("BlogTitle", tenant);
            string copyright = Configuration.Get("RssCopyrightText", tenant);
            int ttl = Configuration.Get("RssTtl", tenant).To<int>();
            int limit = Configuration.Get("RssPageSize", tenant).To<int>();
            int offset = (pageNumber - 1)*limit;

            List<PublishedContentView> contents;
            string category = string.Empty;

            if (!string.IsNullOrWhiteSpace(categoryAlias))
            {
                contents =
                    (await Contents.GetBlogContentsAsync(tenant, categoryAlias, limit, offset).ConfigureAwait(false))
                        .ToList();
                category = contents.Select(x => x.CategoryName).FirstOrDefault();
            }
            else
            {
                contents = (await Contents.GetBlogContentsAsync(tenant, limit, offset).ConfigureAwait(false)).ToList();
            }


            string domain = TenantConvention.GetBaseDomain(new HttpContextWrapper(context), true);

            var items = contents.Select(view => new RssItem
            {
                Title = view.Title,
                Description = view.Contents,
                Link = UrlHelper.CombineUrl(domain, "/blog/" + view.CategoryAlias + "/" + view.Alias),
                Ttl = ttl,
                Category = view.CategoryName,
                PublishDate = view.PublishOn,
                LastBuildDate = view.LastEditedOn
            }).ToList();

            var channel = new RssChannel
            {
                Title = title,
                Description = "Category: " + category,
                Link = UrlHelper.CombineUrl(domain, "/blog"),
                Items = items,
                Copyright = copyright,
                Category = category
            };

            return channel;
        }
예제 #2
0
파일: RssWriter.cs 프로젝트: frapid/frapid
        public static string Write(RssChannel channel)
        {
            var xml = new MemoryStream();

            var writer = XmlWriter.Create(xml, new XmlWriterSettings
            {
                Encoding = Encoding.UTF8,
                Indent = true
            });

            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            writer.WriteStartElement("channel");

            WriteTag(ref writer, "title", channel.Title);
            WriteTag(ref writer, "link", channel.Link);
            WriteTag(ref writer, "description", channel.Description);
            WriteTag(ref writer, "generator", "Frapid");
            WriteTag(ref writer, "copyright", channel.Copyright);

            if (!string.IsNullOrWhiteSpace(channel.Category))
            {
                WriteTag(ref writer, "category", channel.Category);
            }

            foreach (var item in channel.Items)
            {
                writer.WriteStartElement("item");
                WriteTag(ref writer, "title", item.Title);
                WriteTag(ref writer, "link", item.Link);
                WriteTag(ref writer, "description", item.Description);
                WriteTag(ref writer, "pubDate", item.PublishDate.ToRfc822DateString());
                WriteTag(ref writer, "lastBuildDate", item.LastBuildDate.ToRfc822DateString());
                WriteTag(ref writer, "category", item.Category);

                writer.WriteEndElement();//item
            }

            writer.WriteEndElement();//channel
            writer.WriteEndElement();//rss

            writer.Flush();

            return Encoding.UTF8.GetString(xml.ToArray());
        }