Sunday, November 21, 2010

PDC Downloader

Below is a super simple (hardly resilient) PDC downloader. Feel free to compile and tweak it to your hearts content.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApplication1
{
     public class Video
    {
        public string title;
        public string description;
        public string url;
    }
    class Program
    {
        static void Main(string[] args)
        {

          

            var doc = XDocument.Load("http://videoak.microsoftpdc.com/pdc_schedule/Schedule.xml");
            var sessions = doc.Descendants("Sessions").Descendants("Session").Where(session => session.Descendants("DownloadableContent").Descendants("Content").Where(content=> content.Attribute("Title").Value.ToLower().Contains("mp4 high")).Count() > 0);
            var videos = sessions.Select(s =>
                                         new Video()
                                             {
                                                 title = s.Descendants("ShortTitle").Single().Value,
                                                 description = s.Descendants("ShortDescription").Single().Value,
                                                 url =
                                                     s.Descendants("DownloadableContent").Descendants("Content").Where(
                                                         content =>
                                                         content.Attribute("Title").Value.ToLower().Contains("mp4 high"))
                                                     .First().Attribute("Url").Value
                                             });
            var downloadableVideos = videos.ToList<Video>();

            var failedLog = new List<Video>();
            for (var count = 0; count < downloadableVideos.Count; count++)
            {
                var video = downloadableVideos[count];
                try
                {
                    DownloadFile(video);
                }
                catch
                {
                    Log("Failed to download " + video.title);
                    try
                    {
                        var path = video.title + ".mp4";
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                    }
                    catch
                    {
                        Log("Failed to delete " + video.title+ ".mp4");
                    }
                }
            }
            var x = failedLog.Count;
        }

        private static void Log(string msg)
        {
            Console.WriteLine(msg);
        }


        private static void DownloadFile(Video video)
        {
            if (!System.IO.File.Exists(video.title + ".mp4"))
            {
                var webClient = new System.Net.WebClient();
                Log("Downloading " + video.title);
                webClient.DownloadFile(video.url, video.title + ".mp4");
            }
            else
            {
                Log("Skipping " + video.title + " - it has already been downloaded");
            }
        }
    }
}

XNA Conference–Lack of VB

I recently attended a user group meeting with http://augustadevelopers.org/ by Chris Williams http://geekswithblogs.net/cwilliams/Default.aspx covering XNA Games for Windows 7.

The new Windows 7 phone is living up to its hype. The little the I played with it  - it proved to be just as snappy as an iPhone. You won’t see me trading in my Droid just yet but credit is due!

I was surprised at the XNA conference to find that XNA currently doesn’t support VB. It is important to point out that the XNA team is small, and support for VB is on their (someday) feature list. The fact that it was even optional by MS came to me as a surprise.

I recently landed a new job where they prefer VB. This has forced me to take a step back and analyze the difference between C# and VB. I started out with VBA and VB.NET but in my last job I fell in love with C#. I now find myself wondering if I have a good reason to like C# over VB or if it is just a comfort bubble. I am finding that there are far more reasons to go with C# than VB.

One of the most convincing arguments has been the close relationship C# has with other languages. I have found very few developers embrace JavaScript and far fewer (none that I know personally) have been VB purists. JavaScript might as well be chinese to a VB purist, while a C# developer can stumble through it with little or no knowledge of the language rules.

Less important (for me) is the transition to other platforms like C++ and Java. VB has the advantage of VBA and VisualFoxPro but VisualFoxPro is already in end of life and I believe VBA is (or already has) gone to .NET which will mean that you can use either language.

Another point worth mentioning is that a majority of the documentation on the internet, in books, at conferences is all C#. Check out PDC – how many videos are in VB?

At the end of the day – I believe that each language has its strengths – and being flexible to the language is a must. But those that say you should be completely language agnostic ignore the fact that by focusing and embracing on a language – you become a master with the language. The language becomes a tool and as such – an extension of your creative ability. Being (too) language agnostic will create an implementation barrier as you are unable to gain momentum. Your intelligence is shifted from business logic problems and onto syntactic problems.