using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using CodeCafe.Web; using System.IO; using System.Security; using System.Xml; /// /// Summary description for RSSGenerator /// public struct FeedItem { public string Title; public string Link; public string Description; public DateTime PubDate; } public class RSSGenerator { protected string RSSFile = ""; protected List Feed = new List(); protected string RSSFilename = ""; private string[] ShortMonth = new string[12] { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; public string Title { get; set; } public string Description { get; set; } public string WebsiteURL { get; set; } public string RSSFileURL { get; set; } public int MaxFeeds { get; set; } /****************************************************************************************************/ /// /// Initializes a new instance of the class. /// /// The filename. public RSSGenerator(string filename) { RSSFilename = AddServerPath(filename); //todo: check if file exists - if it does then load default values from the file LoadDefaultValues(); LoadExistingFeedItems(); } /****************************************************************************************************/ /// /// Loads the default values from web.config . /// protected void LoadDefaultValues() { if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("RSSTitle"))) Title = ConfigurationManager.AppSettings.Get("RSSTitle"); if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("RSSDescription"))) Description = ConfigurationManager.AppSettings.Get("RSSDescription"); if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("RSSWebsiteURL"))) WebsiteURL = ConfigurationManager.AppSettings.Get("RSSWebsiteURL"); if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("RSSFileURL"))) RSSFileURL = ConfigurationManager.AppSettings.Get("RSSFileURL"); if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("RSSMaxFeeds"))) MaxFeeds = Utils.StrToIntDef(ConfigurationManager.AppSettings.Get("RSSMaxFeeds"), 10); else MaxFeeds = 10; // // // // // } /****************************************************************************************************/ /// /// Gets the month. /// /// The month val. /// private int GetMonth(string MonthVal) { int result = DateTime.Today.Month; for (int i = 0; i < 12; i++) { if (MonthVal.ToLower().Equals(ShortMonth[i])) { result = i + 1; break; } } return result; } /****************************************************************************************************/ /// /// Converts to date time. /// /// The RSS date. /// private DateTime ConvertToDateTime(string RSSDate) { DateTime result = DateTime.Now; //Sun, 27 Mar 2011 18:33:22 +0100 int day; int month; int year; int hour; int min; int sec; day = Utils.StrToIntDef(RSSDate.Substring(5, 2).Trim(), result.Day); month = GetMonth(RSSDate.Substring(8, 3).Trim()); year = Utils.StrToIntDef(RSSDate.Substring(12, 4).Trim(), result.Year); hour = Utils.StrToIntDef(RSSDate.Substring(17, 2).Trim(), result.Hour); min = Utils.StrToIntDef(RSSDate.Substring(20, 2).Trim(), result.Minute); sec = Utils.StrToIntDef(RSSDate.Substring(23, 2).Trim(), result.Second); result = new DateTime(year, month, day, hour, min, sec); return result; } /****************************************************************************************************/ /// /// Loads the existing feed items. /// private void LoadExistingFeedItems() { if (File.Exists(RSSFilename)) { try { string xmlData = Utils.LoadFile(RSSFilename); XmlTextReader reader = new XmlTextReader(new StringReader(xmlData)); FeedItem item = new FeedItem(); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (reader.Name.ToLower().Equals("item")) item = new FeedItem(); if (reader.Name.ToLower().Equals("title")) { if (reader.Read()) if (reader.NodeType == XmlNodeType.Text) item.Title = reader.Value; } if (reader.Name.ToLower().Equals("link")) { if (reader.Read()) if (reader.NodeType == XmlNodeType.Text) item.Link = reader.Value; } if (reader.Name.ToLower().Equals("description")) { if (reader.Read()) if (reader.NodeType == XmlNodeType.Text) item.Description = reader.Value; } if (reader.Name.ToLower().Equals("pubdate")) { if (reader.Read()) if (reader.NodeType == XmlNodeType.Text) { item.PubDate = ConvertToDateTime(reader.Value); } } break; case XmlNodeType.Text: break; case XmlNodeType.EndElement: if (reader.Name.ToLower().Equals("item")) { Feed.Add(item); } break; } } } catch (Exception) { } } } /****************************************************************************************************/ private bool ItemExists(FeedItem check) { bool result = false; foreach (FeedItem item in Feed) { result = item.Title.Equals(check.Title) && item.Description.Equals(check.Description) && item.Link.Equals(check.Link); if (result) break; } return result; } /****************************************************************************************************/ /// /// Adds an item to the feed. /// /// The item. public void AddFeedItem(FeedItem item) { if(!ItemExists(item)) Feed.Add(item); } /****************************************************************************************************/ /// /// Exports the RSS File. /// public void ExportRSS() { AddRSSHeader(); ListFeeds(); AddRSSFooter(); if (File.Exists(RSSFilename)) File.Delete(RSSFilename); Utils.SaveFile(RSSFilename, RSSFile); } /****************************************************************************************************/ /// /// Lists the feeds. /// private void ListFeeds() { Feed.Sort(delegate(FeedItem item1, FeedItem item2) { return item2.PubDate.CompareTo(item1.PubDate); }); //sort descending int idx = 1; foreach (FeedItem item in Feed) { if (idx < MaxFeeds) { RSSFile += "\t\t\n"; RSSFile += "\t\t\t" + SecurityElement.Escape(item.Title) + "\n"; RSSFile += "\t\t\t" + SecurityElement.Escape(item.Link) + "\n"; RSSFile += "\t\t\t" + SecurityElement.Escape(item.Description) + "\n"; RSSFile += "\t\t\t" + item.PubDate.ToString("ddd, dd MMM yyyy HH:mm:ss") + " +0200\n"; RSSFile += "\t\t\n"; } idx++; } } /****************************************************************************************************/ /// /// Adds the RSS header. /// private void AddRSSHeader() { //e.g. http://www.freemarketfoundation.com/DynamicData/fmfpodcast.xml RSSFile += "\n"; RSSFile += "\n"; RSSFile += "\t\n"; RSSFile += "\t\t\n"; RSSFile += "\t\t" + Title + "\n"; RSSFile += "\t\t" + Description + "\n"; RSSFile += "\t\t" + WebsiteURL + "\n"; RSSFile += "\t\t" + DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss") + " +0200\n"; RSSFile += "\t\tCode Cafe RSS Feed Generator 1.0.0\n"; } /****************************************************************************************************/ /// /// Adds the RSS footer. /// private void AddRSSFooter() { RSSFile += "\t\n"; RSSFile += "\n"; } /*****************************************************************************************************/ /// /// Adds the server path. /// /// The filename. /// private string AddServerPath(string Filename) { string result = Filename; if (Filename.StartsWith("~/")) result = System.Web.HttpContext.Current.Server.MapPath(Filename); return result; } }