// GET api/rss public Feed Get() { var feed = new Feed { Id = 0, Title = "LDS Torrents", Url = "http://lds.org", Category = "All feeds", Host = "lds.org" }; var items = new List<Url>(); var channels = Feeds.All(); foreach (var item in channels) { items.Add(new Url { Title = item.Title, Address = new Uri(Request.RequestUri, String.Format("/api/rss/{0}", item.ChannelID)).AbsoluteUri, CreatedBy = item.Host, CreatedAt = item.DateCreated, UpdatedAt = item.LastUpdated, Description = String.Format("Generated torrents of the '{0}' channel hosted by {1}", item.Title, item.Host), }); } feed.Items = items; return feed; }
// GET api/rss/5 public Feed Get(int id) { var feed = Feeds.First(ChannelID: id); var result = new Feed { Id = feed.ChannelID, Title = feed.Title, Url = feed.Url, Category = feed.Category, Host = feed.Host }; var torrents = Items.Find(ChannelID: id, OrderBy: "DatePublished DESC"); var items = new List<Url>(); foreach (var item in torrents) { if (item.FileByteLength == 0) continue; items.Add(new Url { UrlId = item.TorrentID, Title = item.Title, Address = item.TorrentUri, CreatedBy = feed.Host, CreatedAt = item.DatePublished, UpdatedAt = item.DatePublished, FileSize = item.FileByteLength, TransmissionPath = String.Format("{0}/{1}/{2}/{3}", feed.Host, feed.Category, feed.Title, item.Title) .Replace(' ', '_').Replace(",", String.Empty).Replace("'", String.Empty) }); } result.Items = items; return result; }
private void BuildSyndicationFeed(Feed model, Stream stream, string contenttype) { XmlQualifiedName n = new XmlQualifiedName("media", "http://www.w3.org/2000/xmlns/"); List<SyndicationItem> items = new List<SyndicationItem>(); var feed = new SyndicationFeed() { Title = new TextSyndicationContent(model.Title), }; feed.Links.Add(new SyndicationLink(new Uri(model.Url))); feed.Authors.Add(new SyndicationPerson { Name = model.Host, Uri = String.Format("http://{0}", model.Host) }); feed.Categories.Add(new SyndicationCategory { Name = model.Category }); feed.AttributeExtensions.Add(n, medians); var enumerator = model.Items.GetEnumerator(); while (enumerator.MoveNext()) { items.Add(BuildSyndicationItem(enumerator.Current)); } feed.Items = items; using (XmlWriter writer = XmlWriter.Create(stream)) { if (string.Equals(contenttype, atom)) { Atom10FeedFormatter atomformatter = new Atom10FeedFormatter(feed); atomformatter.WriteTo(writer); } else { Rss20FeedFormatter rssformatter = new Rss20FeedFormatter(feed); rssformatter.WriteTo(writer); } } }