Skip to content

Instantly share code, notes, and snippets.

@nul800sebastiaan
Created February 12, 2012 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nul800sebastiaan/1809445 to your computer and use it in GitHub Desktop.
Save nul800sebastiaan/1809445 to your computer and use it in GitHub Desktop.
MyEpisodes C# asp.net RSS to iCal
@using System.Globalization
@using System.ServiceModel.Syndication
@using System.Text
@using System.Xml
@using HtmlAgilityPack
@{
var feedUrl = string.Format("http://myepisodes.com/rss.php?feed={0}&uid={1}&pwdmd5={2}", Request["feed"], Request["uid"], Request["pwdmd5"]);
var reader = XmlReader.Create(feedUrl);
var feed = SyndicationFeed.Load(reader);
if (feed == null)
{
return;
}
var sb = new StringBuilder();
sb.AppendFormat("BEGIN:VCALENDAR{0}", Environment.NewLine);
sb.AppendFormat("VERSION:2.0{0}", Environment.NewLine);
sb.AppendFormat("PRODID:-//myepisodes.com ICS//EN{0}", Environment.NewLine);
sb.AppendFormat("CALSCALE:GREGORIAN{0}", Environment.NewLine);
sb.AppendFormat("METHOD:PUBLISH{0}", Environment.NewLine);
sb.AppendFormat("X-WR-CALNAME:TV Shows{0}", Environment.NewLine);
foreach (var item in feed.Items)
{
var title = "";
for (var index = 0; index < item.Title.Text.Split(']').Length; index++)
{
var titlePart = item.Title.Text.Split(']')[index];
if (titlePart.Length == 0)
{
continue;
}
switch (index)
{
case 0:
title = titlePart.Substring(1).Trim();
break;
case 1:
title += string.Format("{0} {1}", title, titlePart.Substring(1).Trim());
break;
}
}
var doc = new HtmlDocument();
var htmlDoc = string.Format("<!DOCTYPE html><html><body>{0}</body></html>", Html.Raw(item.Summary.Text));
doc.LoadHtml(htmlDoc);
var time = string.Format("{0}00Z", doc.DocumentNode.SelectSingleNode("//table/tr[4]/td[2]").InnerText.Replace(":", ""));
var date = string.Format("{0}T", doc.DocumentNode.SelectSingleNode("//table/tr[3]/td[2]").InnerText.Replace("-", ""));
var summaryTitle = string.Format("{0} - {1} - {2}", doc.DocumentNode.SelectSingleNode("//h3").InnerText, doc.DocumentNode.SelectSingleNode("//table/tr[2]/td[2]").InnerText, doc.DocumentNode.SelectSingleNode("//table/tr[1]/td[2]").InnerText);
var link = doc.DocumentNode.SelectSingleNode("//a[2]").Attributes["href"].Value;
sb.AppendFormat("BEGIN:VEVENT{0}", Environment.NewLine);
sb.AppendFormat("DTSTAMP:{0}{1}{2}", date, time, Environment.NewLine);
sb.AppendFormat("DTSTART:{0}{1}{2}", date, time, Environment.NewLine);
sb.AppendFormat("DTEND:{0}{1}{2}", date, time, Environment.NewLine);
sb.AppendFormat("UID:{0}@myepisodes.com{1}", item.Id, Environment.NewLine);
sb.AppendFormat("SUMMARY:{0}{1}", summaryTitle, Environment.NewLine);
sb.AppendFormat("DESCRIPTION:{0} - {1} - {2}", summaryTitle, link, Environment.NewLine);
sb.AppendFormat("END:VEVENT{0}", Environment.NewLine);
}
sb.Append("END:VCALENDAR");
var enc = new UTF8Encoding();
var arrBytData = enc.GetBytes(sb.ToString());
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=vCalendar.ics");
Response.AppendHeader("Content-Length", arrBytData.Length.ToString(CultureInfo.InvariantCulture));
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(arrBytData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment