public static string GetSyndicationFeed(this IEnumerable<Notification> notifications, string contentType)
        {
            var feed = new SyndicationFeed("System notification", "Publishes system notifications", new Uri("http://localhost"));
            feed.Authors.Add(new SyndicationPerson("[email protected]", "Testor Testorsson", "http://localhost"));
            feed.Links.Add(SyndicationLink.CreateSelfLink(new Uri(HttpContext.Current.Request.Url.AbsoluteUri), "application/atom+xml"));
            feed.Items = notifications.ASyndicationItems(feed);

            var stringWriter = new StringWriter();

            XmlWriter feedWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings
            {
                OmitXmlDeclaration = true
            });

            feed.Copyright = SyndicationContent.CreatePlaintextContent("Copyright hygia");
            feed.Language = "en-us";

            if (contentType == ContentTypes.Atom)
                feed.SaveAsAtom10(feedWriter);
            else
                feed.SaveAsRss20(feedWriter);

            feedWriter.Close();

            return stringWriter.ToString();
        }
Example #2
0
File: Rss.cs Project: the404/yqblog
 public override void ExecuteResult(ControllerContext context)
 {
     var rss = new SyndicationFeed(
         _title,
         _desc,
         _altLink,
         _items
     );
     var settings = new XmlWriterSettings {Indent = true, NewLineHandling = NewLineHandling.Entitize};
     using (var writer = XmlWriter.Create(context.HttpContext.Response.OutputStream, settings))
     {
         rss.SaveAsAtom10(writer);
     }
 }
        protected override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
        {
            var bloglist = value as IEnumerable<Blog>;
            if (bloglist != null)
            {
                var syndicationFeed =
                    new SyndicationFeed("Blogs", "all blogs", new Uri(this.ServiceURI + "/blogs"))
                        {
                            Items = bloglist
                                .Select(blogItem => BlogAtomConverter
                                                        .BlogToSyndicationItem(blogItem, this.ServiceURI))
                        };

                using (var xmlWriter = XmlWriter.Create(stream))
                {
                    syndicationFeed.SaveAsAtom10(xmlWriter);
                }
            }
        }
        protected override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
        {
            var post = value as Post;
            if (post == null) return;

            var syndicationFeed =
                new SyndicationFeed(post.Title, "Post " + post.Title
                                    , new Uri(this.ServiceURI + "/blogs/" + post.BlogId + "/posts/" +
                                              post.Id))
                    {
                        Items = new List<SyndicationItem>()
                                    {
                                        PostAtomConverter.PostToSyndicationItem(post, this.ServiceURI)
                                    }
                    };

            using (var xmlWriter = XmlWriter.Create(stream))
            {
                syndicationFeed.SaveAsAtom10(xmlWriter);
            }
        }
        HttpResponseMessage GetAllCommits(HttpRequestMessage msg)
        {
            string uri1;
            IEnumerable<CommitInfo> commits;
            GetCommits(out uri1, out commits);

            var feedTree = new SyndicationFeed("Commit List", "Commits from GITHub", new Uri(uri1));
            var items = new List<SyndicationItem>();
            foreach (var commitInfo in commits)
            {
                var item = new SyndicationItem();
                item.Title = new TextSyndicationContent("item");
                item.BaseUri = new Uri(commitInfo.url);
                if (commitInfo.commit.author != null)
                {
                    item.PublishDate = System.Convert.ToDateTime(commitInfo.commit.author.date);
                    item.Authors.Add(new SyndicationPerson() { Name = commitInfo.commit.author.name });
                }
                items.Add(item);
            }
            feedTree.Items = items;

            //var str = new StringBuilder();
            //var wr = XmlWriter.Create(str);

            var mem = new MemoryStream();
            var wr = XmlWriter.Create(mem);
            
            feedTree.SaveAsAtom10(wr);
            wr.Flush();

            //var content = new StringContent(str.ToString());
            var content = new StreamContent(mem);
            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/atom+xml");

            var resp = new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = content };
            //var resp = new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("teste") };
            resp.Headers.Date = DateTimeOffset.Now;
            return resp;
        }
        protected override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
        {
            var postList = value as IEnumerable<Post>;
            if (postList == null) return;

            var syndicationFeed =
                new SyndicationFeed("Posts from Blog " + postList.FirstOrDefault().Blog.Title
                                    , postList.FirstOrDefault().Blog.Description
                                    ,
                                    new Uri(this.ServiceURI + "/blogs/" + postList.FirstOrDefault().BlogId +
                                            "/posts"))
                    {
                        Items = postList
                            .Select(postItem => PostAtomConverter
                                                    .PostToSyndicationItem(postItem, this.ServiceURI))
                    };

            using (var xmlWriter = XmlWriter.Create(stream))
            {
                syndicationFeed.SaveAsAtom10(xmlWriter);
            }
        }
        public void Execute(IList<IFilter> filters, PipelineSettings pipelineSettings)
        {
            IEnumerable<SyndicationItem> items;

            if (pipelineSettings.Inputs != null)
            {
                // Get a list of feed items
                items = from feed in ParallelCrawl(pipelineSettings.Inputs)
                                                        from i in feed.Items
                                                        select i;
            }
            else
            {
                items = new SyndicationItem[0];
            }

            // Filter
            IEnumerable<SyndicationItem> filtered = items
                .Where(x => ApplyFilters(filters, x) == FilterAction.Include);

            // Remove duplicates
            IEnumerable<SyndicationItem> merged = Dedup(filtered);

            if (pipelineSettings.Output != null)
            {
                // Save results
                var newFeed = new SyndicationFeed(merged.OrderBy(i => i.PublishDate));

                newFeed.Title = SyndicationContent.CreatePlaintextContent(pipelineSettings.Title);
                newFeed.Description = SyndicationContent.CreatePlaintextContent(pipelineSettings.Description);
                newFeed.LastUpdatedTime = DateTimeOffset.Now;

                using (XmlWriter writer = XmlWriter.Create(pipelineSettings.Output))
                {
                    newFeed.SaveAsAtom10(writer);
                }
            }
        }
         internal string GetUpdateFeedString(SyndicationFeed feed, XmlWriterSettings xmlWriterSettings)
         {
            using (var stringWriter = new StringWriterWithEncoding(Encoding.UTF8))
            {
               using (var writer = XmlWriter.Create(stringWriter, xmlWriterSettings))
               {
                  feed.SaveAsAtom10(writer);
               }

               var feedString = stringWriter.ToString();

               return feedString;
            }
         }
Example #9
0
        public static Stream GetServiceStream(string format, string callback, DataTable dt, SyndicationFeed sf)
        {
            MemoryStream stream = new MemoryStream();
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
            if (format == "xml")
            {
                XmlSerializer xmls = new XmlSerializer(typeof(DataTable));
                xmls.Serialize(writer, dt);
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            }
            else if (format == "json")
            {
                var toJSON = new JavaScriptSerializer();
                toJSON.RegisterConverters(new JavaScriptConverter[] { new JavaScriptDataTableConverter() });
                writer.Write(toJSON.Serialize(dt));
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/json";
            }
            else if (format == "jsonp")
            {
                var toJSON = new JavaScriptSerializer();
                toJSON.RegisterConverters(new JavaScriptConverter[] { new JavaScriptDataTableConverter() });
                writer.Write(callback + "( " + toJSON.Serialize(dt) + " );");
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/json";
            }
            else if (format == "rss")
            {
                XmlWriter xmlw = new XmlTextWriter(writer);
                sf.SaveAsRss20(xmlw);
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            }
            else if (format == "atom")
            {
                XmlWriter xmlw = new XmlTextWriter(writer);
                sf.SaveAsAtom10(xmlw);
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            }
            else if (format == "help")
            {
                StreamReader sr = new StreamReader(ConfigurationManager.AppSettings["ServiceDocumentation"].ToString());
                writer.Write(sr.ReadToEnd());
                sr.Close();
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            }
            else
            {
                writer.Write("Invalid formatting specified.");
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            }

            writer.Flush();
            stream.Position = 0;
            return stream;
        }
Example #10
0
        /// <summary>
        /// Takes a syndication feed and returns an Atom10 string
        /// </summary>
        /// <param name="feed">Syndication Feed</param>
        /// <returns>RSS formated string</returns>
        public static string SerializeToAtom10(SyndicationFeed feed)
        {
            using (StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = new UTF8Encoding(false);
                //settings.Indent = true;

                using (XmlWriter xWriter = XmlWriter.Create(writer, settings))
                {
                    feed.SaveAsAtom10(xWriter);
                    xWriter.Flush();
                    return writer.ToString();
                }
            }
        }
Example #11
0
        /// <summary>
        /// The write feed xml.
        /// </summary>
        /// <param name="feedType">
        /// The feed Type.
        /// </param>
        private void WriteFeedXml(FeedType feedType)
        {
            Settings app = SettingsComp.GetSettings();
            string url = app.Url;
            if (url == string.Empty)
            {
                Uri uri = System.Web.HttpContext.Current.Request.Url;
                string dnsPath = uri.Scheme + "://" + uri.Host;
                if (!uri.IsDefaultPort)
                {
                    dnsPath = dnsPath + ":" + uri.Port.ToString();
                }

                string appPath = System.Web.HttpContext.Current.Request.ApplicationPath;
                url = dnsPath + appPath;
            }

            if (url.Substring(url.Length - 1, 1) != "/")
            {
                url += "/";
            }

            List<PostInfo> posts = BlogComp.GetPosts();
            List<SyndicationItem> items = new List<SyndicationItem>();

            int count = 0;
            foreach (PostInfo post in posts)
            {
                if (++count > 20)
                {
                    break;
                }

                SyndicationItem item = new SyndicationItem();
                item.Title = SyndicationContent.CreatePlaintextContent(post.Title);

                TimeSpan ts = DateTime.UtcNow - DateTime.Now;
                item.PublishDate = new DateTimeOffset(post.Time, ts);

                string url2 = string.Format("{0}Post/{1}", url, post.FileID);
                SyndicationLink link = SyndicationLink.CreateAlternateLink(new Uri(url2));

                item.Links.Add(link);

                Post post2 = PostComp.GetPost(post.FileID);
                item.Content = SyndicationContent.CreateHtmlContent(post2.Contents);

                string catName = CategoryComp.GetCategoryName(post.CatID);
                item.Categories.Add(new SyndicationCategory(catName));

                items.Add(item);
            }

            SyndicationFeed feed = new SyndicationFeed(items);

            feed.Title = SyndicationContent.CreatePlaintextContent(app.Name);
            feed.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(url)));

            List<Author> authors = AuthorComp.GetAuthors();
            foreach (Author author in authors)
            {
                feed.Authors.Add(new SyndicationPerson(author.Email, author.Name, author.Url));
            }

            List<Category> catList = CategoryComp.GetCategories();
            foreach (Category cat in catList)
            {
                feed.Categories.Add(new SyndicationCategory(cat.Name));
            }

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = System.Text.Encoding.UTF8;
            settings.OmitXmlDeclaration = true;
            settings.Indent = true;

            if (feedType == FeedType.RSS)
            {
                XmlWriter writer = XmlWriter.Create(this.Server.MapPath("~/App_Data/Rss.xml"), settings);
                feed.SaveAsRss20(writer);
                writer.Flush();
                writer.Close();
            }
            else
            {
                XmlWriter writer = XmlWriter.Create(this.Server.MapPath("~/App_Data/Atom.xml"), settings);
                feed.SaveAsAtom10(writer);
                writer.Flush();
                writer.Close();
            }
        }
Example #12
0
        public ActionResult Search(string terms, string source, string locale, int index = 1, int size = 20, string format = "")
        {
            //App.Get().SetCulture(locale);
            var src = string.IsNullOrEmpty(source) ? App.Get().Searcher.Sources.First().Name : source;
            if (terms == null)
            {
                ViewBag.Query = new SearchQuery()
                {
                    Source = src,
                    Locale = locale
                };
                return View();
            }

            var searchQuery = new SearchQuery()
            {
                Index = index - 1,
                Size = size,
                Source = src,
                Terms = terms,
                Locale = locale
            };

            var model = App.Get().Searcher.Search(searchQuery);
            var feed = new SyndicationFeed(string.Format("Search for {0}", terms), src, Request.Url, model);
            feed.Generator = "DotNetAge";

            if (format.Equals("rss", System.StringComparison.OrdinalIgnoreCase) ||
                format.Equals("atom", System.StringComparison.OrdinalIgnoreCase))
            {
                var sb = new StringBuilder();
                using (var writer = System.Xml.XmlWriter.Create(sb))
                {
                    if (format.Equals("rss", System.StringComparison.OrdinalIgnoreCase))
                    {
                        feed.SaveAsRss20(writer);
                        writer.Flush();
                        return Content(sb.ToString(), "text/xml", Encoding.UTF8);
                    }

                    if (format.Equals("atom", System.StringComparison.OrdinalIgnoreCase))
                    {
                        feed.SaveAsAtom10(writer);
                        writer.Flush();
                        return Content(sb.ToString(), "text/xml", Encoding.UTF8);
                    }
                }
            }
            ViewBag.Query = searchQuery;

            return Json(feed,JsonRequestBehavior.AllowGet);
        }
Example #13
0
        public ActionResult Feed(string format)
        {
            var posts = _index.GetBlogPosts().OrderByDescending(x => x.PostedAt);

            // Create feed items.
            var items = new List<SyndicationItem>();
            foreach (var post in posts)
            {
                var feedItem = new SyndicationItem
                {
                    Content = new TextSyndicationContent(post.FeedBody, TextSyndicationContentKind.Html),
                    Id = post.Id,
                    PublishDate = post.PostedAt,
                    Title = new TextSyndicationContent(post.Title, TextSyndicationContentKind.Plaintext),
                };

                var url = string.Concat("http://cakebuild.net", LinkHelper.GetLink(post));
                feedItem.Links.Add(new SyndicationLink(new Uri(url)));
                items.Add(feedItem);
            }

            // Create the feed.
            var feed = new SyndicationFeed(items);
            feed.Title = new TextSyndicationContent("Cake", TextSyndicationContentKind.Plaintext);
            feed.Description = new TextSyndicationContent("The Cake blog feed", TextSyndicationContentKind.Plaintext);

            // Write the feed as a response.
            // TODO: Cache this at start up?
            using (var ms = new MemoryStream())
            {
                var writer = XmlWriter.Create(ms);

                var contentType = "application/atom+xml";
                if (string.Equals("rss", format, StringComparison.InvariantCultureIgnoreCase))
                {
                    feed.SaveAsRss20(writer);
                    contentType = "application/rss+xml";
                }
                else
                {
                    feed.SaveAsAtom10(writer);
                }

                writer.Flush();
                var text = Encoding.UTF8.GetString(ms.ToArray());
                return Content(text, contentType, Encoding.UTF8);
            }
        }
Example #14
0
        public ActionResult Feed(string format)
        {
            var posts = _blogRepository.GetAll().OrderByDescending(b => b.PublishedDate);
            var items = new List<SyndicationItem>();

            foreach (var post in posts)
            {
                var feedItem = new SyndicationItem
                {
                    Content = new TextSyndicationContent(post.Content),
                    Id = post.Id.ToString(),
                    PublishDate = post.PublishedDate,
                    Title = new TextSyndicationContent(post.Title),
                };

                var url = String.Format(_configuration.Settings["Blog:UrlTemplate"],
                    post.PublishedDate.Year,
                    post.PublishedDate.Month.ToString().PadLeft(2, '0'),
                    post.Slug);

                feedItem.Links.Add(new SyndicationLink(new Uri(url)));

                items.Add(feedItem);
            }

            var feed = new SyndicationFeed(items);
            feed.Title = new TextSyndicationContent("Hadouken", TextSyndicationContentKind.Plaintext);
            feed.Description = new TextSyndicationContent("The Hadouken BitTorrent client blog feed",
                TextSyndicationContentKind.Plaintext);

            using (var ms = new MemoryStream())
            {
                var writer = XmlWriter.Create(ms);
                var contentType = "application/atom+xml";

                if (String.Equals("rss", format, StringComparison.InvariantCultureIgnoreCase))
                {
                    feed.SaveAsRss20(writer);
                    contentType = "application/rss+xml";
                }
                else
                {
                    feed.SaveAsAtom10(writer);
                }

                writer.Flush();

                var text = Encoding.UTF8.GetString(ms.ToArray());

                return Content(text, contentType, Encoding.UTF8);
            }
        }