MIX09 Session Videos – How I Did It
On my last post I introduced the feeds I created to subscribe to Mix09 session videos.
In case someone is interested on how I did it, here it is:
<%@ WebHandler Language="C#" Class="mix09" %>using System; using System.IO; using System.Web; using System.Linq; using System.Xml.Linq; using System.Net; using System.Xml;
public class mix09 : IHttpHandler, IHttpAsyncHandler { class WebClientOpenReadAsyncResult : IAsyncResult { private AsyncCallback callback;
<span style="color: blue">public </span>WebClientOpenReadAsyncResult() { <span style="color: blue">this</span>.IsCompleted = <span style="color: blue">true</span>; <span style="color: blue">this</span>.CompletedSynchronously = <span style="color: blue">true</span>; } <span style="color: blue">public </span>WebClientOpenReadAsyncResult(<span style="color: #2b91af">AsyncCallback </span>callback) { <span style="color: blue">this</span>.callback = callback; <span style="color: blue">this</span>.IsCompleted = <span style="color: blue">false</span>; <span style="color: blue">this</span>.CompletedSynchronously = <span style="color: blue">false</span>; } <span style="color: blue">public object </span>AsyncState { <span style="color: blue">get </span>{ <span style="color: blue">return null</span>; } } <span style="color: blue">public bool </span>CompletedSynchronously { <span style="color: blue">get</span>; <span style="color: blue">private set</span>; } <span style="color: blue">public </span>System.Threading.<span style="color: #2b91af">WaitHandle </span>AsyncWaitHandle { <span style="color: blue">get </span>{ <span style="color: blue">throw new </span><span style="color: #2b91af">InvalidOperationException</span>(<span style="color: #a31515">"ASP.NET should not use this property ."</span>); } } <span style="color: blue">public bool </span>IsCompleted { <span style="color: blue">get</span>; <span style="color: blue">private set</span>; } <span style="color: blue">public </span><span style="color: #2b91af">Stream </span>Stream { <span style="color: blue">get</span>; <span style="color: blue">private set</span>; } <span style="color: blue">public void </span>Completed(<span style="color: blue">object </span>sender, <span style="color: #2b91af">OpenReadCompletedEventArgs </span>e) { <span style="color: blue">this</span>.IsCompleted = <span style="color: blue">true</span>; <span style="color: blue">this</span>.Stream = e.Result; <span style="color: blue">if </span>(<span style="color: blue">this</span>.callback != <span style="color: blue">null</span>) { <span style="color: blue">this</span>.callback(<span style="color: blue">this</span>); } } } <span style="color: blue">private static </span><span style="color: #2b91af">Uri </span>mixSessionsUri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">"http://sessions.visitmix.com/RSS"</span>); <span style="color: blue">private </span><span style="color: #2b91af">HttpContext </span>context; <span style="color: blue">private string </span>type; <span style="color: blue">#region </span>IHttpHandler Members <span style="color: blue">public bool </span>IsReusable { <span style="color: blue">get </span>{ <span style="color: blue">return true</span>; } } <span style="color: blue">public void </span>ProcessRequest(<span style="color: #2b91af">HttpContext </span>context) { <span style="color: #2b91af">WebClient </span>wc = InitializeRequest(context); <span style="color: blue">if </span>(wc == <span style="color: blue">null</span>) { <span style="color: blue">return</span>; } OutputFeed(wc.OpenRead(mixSessionsUri)); } <span style="color: blue">#endregion #region </span>IHttpAsyncHandler Members <span style="color: blue">public </span><span style="color: #2b91af">IAsyncResult </span>BeginProcessRequest(<span style="color: #2b91af">HttpContext </span>context, <span style="color: #2b91af">AsyncCallback </span>cb, <span style="color: blue">object </span>extraData) { <span style="color: #2b91af">WebClient </span>wc = InitializeRequest(context); <span style="color: blue">if </span>(wc == <span style="color: blue">null</span>) { <span style="color: blue">return new </span><span style="color: #2b91af">WebClientOpenReadAsyncResult</span>(); } <span style="color: #2b91af">WebClientOpenReadAsyncResult </span>ar = <span style="color: blue">new </span><span style="color: #2b91af">WebClientOpenReadAsyncResult</span>(cb); wc.OpenReadCompleted += ar.Completed; wc.OpenReadAsync(mixSessionsUri, extraData); <span style="color: blue">return </span>ar; } <span style="color: blue">public void </span>EndProcessRequest(<span style="color: #2b91af">IAsyncResult </span>result) { <span style="color: #2b91af">Stream </span>stream = (result <span style="color: blue">as </span><span style="color: #2b91af">WebClientOpenReadAsyncResult</span>).Stream; <span style="color: blue">if </span>(stream != <span style="color: blue">null</span>) { OutputFeed(stream); } } <span style="color: blue">#endregion private </span><span style="color: #2b91af">WebClient </span>InitializeRequest(<span style="color: #2b91af">HttpContext </span>context) { <span style="color: blue">this</span>.context = context; <span style="color: blue">this</span>.type = context.Request.QueryString[<span style="color: #a31515">"type"</span>]; <span style="color: blue">if </span>(<span style="color: blue">string</span>.IsNullOrEmpty(<span style="color: blue">this</span>.type)) { <span style="color: blue">return null</span>; } <span style="color: #2b91af">WebClient </span>wc = <span style="color: blue">new </span><span style="color: #2b91af">WebClient</span>(); wc.Headers[<span style="color: #2b91af">HttpRequestHeader</span>.UserAgent] = <span style="color: #a31515">"Required User Agent"</span>; <span style="color: blue">return </span>wc; } <span style="color: blue">private void </span>OutputFeed(<span style="color: #2b91af">Stream </span>source) { <span style="color: #2b91af">XmlReader </span>feedReader = <span style="color: #2b91af">XmlReader</span>.Create(source); <span style="color: #2b91af">XDocument </span>feed = <span style="color: #2b91af">XDocument</span>.Load(feedReader); <span style="color: blue">var </span>rss = feed.Element(<span style="color: #a31515">"rss"</span>); <span style="color: blue">var </span>channel = rss.Element(<span style="color: #a31515">"channel"</span>); <span style="color: blue">var </span>title = channel.Element(<span style="color: #a31515">"title"</span>); title.Value = <span style="color: blue">string</span>.Format(<span style="color: #a31515">"{0} ({1})"</span>, title.Value, <span style="color: blue">this</span>.type.ToUpper()); channel.Element(<span style="color: #a31515">"link"</span>).Value = <span style="color: #a31515">"http://cli.gs/Mix09Sessions"</span>; <span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>channel.Elements(<span style="color: #a31515">"item"</span>)) { <span style="color: blue">string </span>link = item.Element(<span style="color: #a31515">"link"</span>).Value; <span style="color: blue">string </span>session = link.Substring(link.LastIndexOf(<span style="color: #a31515">'/'</span>) + 1).ToLower(); <span style="color: blue">string </span>enclosureUrl = <span style="color: blue">string</span>.Format(<span style="color: #a31515">"http://mschannel9.vo.msecnd.net/o9/mix/09/{0}/{1}.wmv"</span>, <span style="color: blue">this</span>.type.ToLower(), session); item.Add( <span style="color: blue">new </span><span style="color: #2b91af">XElement</span>(<span style="color: #a31515">"enclosure"</span>, <span style="color: blue">new </span><span style="color: #2b91af">XAttribute</span>(<span style="color: #a31515">"url"</span>, enclosureUrl))); } <span style="color: blue">this</span>.context.Response.Write(feed.ToString()); <span style="color: blue">this</span>.context.Response.ContentType = <span style="color: #a31515">"application/rss+xml"</span>; <span style="color: #2b91af">HttpCachePolicy </span>cache = <span style="color: blue">this</span>.context.Response.Cache; cache.SetCacheability(<span style="color: #2b91af">HttpCacheability</span>.ServerAndPrivate); cache.SetExpires(<span style="color: #2b91af">DateTime</span>.Now.AddHours(1)); cache.VaryByParams[<span style="color: #a31515">"type"</span>] = <span style="color: blue">true</span>; cache.SetValidUntilExpires(<span style="color: blue">true</span>); }
}