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");
            }
        }
    }
}

No comments:

Post a Comment