<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using System.Xml;
using NuGetFeed.NuGetService;

namespace NuGetFeed.Controllers
{
    public class ListController : Controller
    {
        public ActionResult Packages(string id)
        {
            var allItems = new List<SyndicationItem>();
            var packages = id.Split(',');
            foreach (string package in packages)
            {
                allItems.AddRange(CreateListOfItems(package.Trim()));
            }

            allItems = allItems.OrderByDescending(x => x.LastUpdatedTime).ToList();
            
            var feed = CreateFeed("Recent releases of " + id, "Recent NuGet package releases of " + id, id);
            feed.Items = allItems;

            return new RssActionResult { Feed = feed };
        }

        private IList<SyndicationItem> CreateListOfItems(string packageId)
        {
            //if (HttpContext.Cache[packageId.ToLower()] != null)
            //{
            //    return HttpContext.Cache[packageId.ToLower()] as List<SyndicationItem>;
            //}

            var context = new GalleryFeedContext(new Uri("http://packages.nuget.org/v1/FeedService.svc/"));
            var packages = from p in context.Packages
                           where p.Id == packageId
                           orderby p.LastUpdated descending 
                           select new SyndicationItem(p.Title + " " + p.Version, p.Title + " " + p.Version, new Uri(p.GalleryDetailsUrl), p.Id + p.Version, p.LastUpdated);

            //HttpContext.Cache.Add(packageId.ToLower(), packages.ToList(), null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            return packages.Take(5).ToList();
        }

        private static SyndicationFeed CreateFeed(string title, string description, string id)
        {
            var feed = new SyndicationFeed(string.Format("nugetfeed.org - {0}", id), description, new Uri("http://nugetfeed.org"), id, DateTime.Now)
            {
                Language = "EN",
                Copyright = new TextSyndicationContent("Copyright " + DateTime.Today.Year + ", nugetfeed.org")
            };
            return feed;
        }
    }

    public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {<commit_msg>Image maybe added to RSS feed<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;
using NuGetFeed.NuGetService;

namespace NuGetFeed.Controllers
{
    public class ListController : Controller
    {
        public ActionResult Packages(string id)
        {
            var allItems = new List<SyndicationItem>();
            var packages = id.Split(',');
            foreach (string package in packages)
            {
                allItems.AddRange(CreateListOfItems(package.Trim()));
            }

            allItems = allItems.OrderByDescending(x => x.LastUpdatedTime).ToList();
            
            var feed = CreateFeed("Recent releases of " + id, "Recent NuGet package releases of " + id, id);
            feed.Items = allItems;

            return new RssActionResult { Feed = feed };
        }

        private IEnumerable<SyndicationItem> CreateListOfItems(string packageId)
        {
            var context = new GalleryFeedContext(new Uri("http://packages.nuget.org/v1/FeedService.svc/"));
            var packages = from p in context.Packages
                           where p.Id == packageId
                           orderby p.LastUpdated descending
                           select p; // new SyndicationItem(p.Title + " " + p.Version, p.Title + " " + p.Version, new Uri(p.GalleryDetailsUrl), p.Id + p.Version, p.LastUpdated).ElementExtensions.Add(new XElement("image", p.IconUrl));

            foreach (var p in packages.Take(5))
            {
                var item = new SyndicationItem(p.Title + " " + p.Version, p.Title + " " + p.Version, new Uri(p.GalleryDetailsUrl), p.Id + p.Version, p.LastUpdated);
                if(p.IconUrl != null)
                {
                    var imageElement = new XElement("image");
                    imageElement.Add(new XElement("title", p.Title));
                    imageElement.Add(new XElement("link", p.GalleryDetailsUrl));
                    imageElement.Add(new XElement("url", p.IconUrl));
                    imageElement.Add(new XElement("height", "50"));
                    imageElement.Add(new XElement("width", "50"));

                    item.ElementExtensions.Add(imageElement);
                }

                yield return item;
            }
        }

        private static SyndicationFeed CreateFeed(string title, string description, string id)
        {
            var feed = new SyndicationFeed(string.Format("nugetfeed.org - {0}", id), description, new Uri("http://nugetfeed.org"), id, DateTime.Now)
            {
                Language = "EN",
                Copyright = new TextSyndicationContent("Copyright " + DateTime.Today.Year + ", nugetfeed.org")
            };
            return feed;
        }
    }

    public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
{'fraction_non_alphanumeric': 0.10557184750733138, 'fraction_numerical': 0.0018975332068311196, 'mean_word_length': 2.107181136120043, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1674540', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1511, 'n_words': 358}