Downloading DevExpress Training Video

Back to Listing

Downloading DevExpress Training Video


18 Apr, 2008


I recently laid my hands on a copy of DevExpress’ Code Rush and Refactor! Pro. They have a section on their site that allows you to watch training videos about these products. I wanted to watch them all but at my own pace and on my main TV in the living room.

Using my new toys I whipped up the following code to download all of the movies from their site. Now I just need to convert them to AVI so I can watch them on the Xbox.

Code included just because I want to:

using System;  
using System.Text.RegularExpressions;  
using System.Diagnostics;  
using System.Collections.Generic;  
using System.Text;  
using System.Xml;  
using System.Net;

namespace SnagDevExMovies  
{
    class Program
    {
        private static WebClient client = new WebClient();
        private static Uri site =
                new Uri(@"http://www.devexpress.com/Products/NET/IDETools/CodeRush/Training.xml");
        private static UriBuilder siteResource = new UriBuilder(site);

        static void Main(string[] args)
        {

           string content = getContent(site);
           MatchCollection URLs = getMatches(content,
                new Regex(@"bWatchExpanded.*LessonNewWindow\('(?<URL>.*)'\)"));
           processMatches(URLs);
        }

        private static void processResource(Uri uri)
        {
            Debug.Assert(uri != null, "uri is null.");
            Debug.WriteLine(String.Format("Processing: {0}", uri));

            string content = getContent(uri);

            MatchCollection URLs = getMatches(content, new Regex(@"(?<URL>\w*\.swf)"));

            if (URLs.Count > 0)
            {
                siteResource.Path = getResourcePath(siteResource.Uri)
                        + URLs[0].Groups["URL"].Value;
                downloadFile(siteResource.Uri);
            }
        }

        private static MatchCollection getMatches(string content, Regex expression)
        {
            Debug.Assert(!String.IsNullOrEmpty(content), "content is null or empty.");
            Debug.Assert(expression != null, "expression is null.");

            MatchCollection matches = expression.Matches(content);
            Debug.WriteLine(String.Format("{0} matches found.", matches.Count));
            return matches;
        }

        private static void processMatches(MatchCollection relativePaths)
        {
            Debug.Assert(relativePaths != null, "relativePaths is null.");

            foreach (Match item in relativePaths)
            {
                siteResource.Path = item.Groups["URL"].Value;
                processResource(siteResource.Uri);
            }
        }

        private static void downloadFile(Uri uri)
        {
            Debug.Assert(uri != null, "uri is null.");

            Debug.WriteLine(String.Format("Downloading: {0}", uri.ToString()));
            try
            {
                client.DownloadFile(uri, getFileName(uri));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

        }

        private static string getResourcePath(Uri uri)
        {
            Debug.Assert(uri != null, "uri is null.");

            string resourcePath = uri.AbsolutePath.Substring(0,
                                uri.AbsolutePath.LastIndexOf("/") + 1);
            return resourcePath;
        }

        private static string getFileName(Uri uri)
        {
            Debug.Assert(uri != null, "uri is null.");

            return uri.Segments[uri.Segments.Length - 1];
        }

        private static string getContent(Uri uri)
        {
            Debug.Assert(uri != null, "uri is null.");

            string content = client.DownloadString(uri);
            return content;
        }

    }
}
Share this story

Bobby Johnson

About Author

I am a passionate engineer with an interest in shipping quality software, building strong collaborative teams and continuous improvement of my skills, team and the product.

comments powered by Disqus
Back to top