Download All the TekPub Videos from Your Account’s RSS Feed (using Windows PowerShell)

I just subscribed to TekPub after watching the previews for a bunch of videos and realizing how awesome they are. I subscribed to the Annual plan (for $179) so that I could download the videos to my local device and watch them at my leisure.

After signing up and signing in, you’re presented with a variety of RSS feeds to choose from, including ones tailored for iTunes and Zune. I don’t have iTunes installed on my development PC and after upgrading to Windows 8, I don’t have the Zune app installed anymore either. I didn’t want to right-click each video link to download the videos individually, so I wrote up the following PowerShell script to run during the day while I worked. I was heavily inspired by Scott Hanselman’s post on downloading with PowerShell.

$base = "B:\Video\TekPub"
$chars = [System.IO.Path]::GetInvalidFileNameChars()

$xml = ([xml](New-Object System.Net.Webclient).DownloadString("put your url containing your token here"))

$xml.rss.channel.item | foreach
{
    $url = $_.enclosure.url
    $url_parts = $url.Split('/')

    $folder = $url_parts[4]

    $filename = $_.title;
    foreach($char in $chars)
    {
        $filename = $file.Replace($char, '')
    }

    $filepath = $base + "\" + $folder + "\" + $filename

    $client = (New-Object System.Net.WebClient)
    $client.Headers["User-Agent"] = "TekPub Video Downloader - Nick Ohrn (nohrn@ohrnventures.com)"

    if(!(test-path $file))
    {
        try
        {
            $client.DownloadFile($url, $filepath)
        }
        catch
        {
            Write-Host $error[0]
        }
    }
}

There’s a couple of important things to notice here. First, I explicitly declared the base location that I wanted the videos to be saved to. Second, I’m replacing all characters that would lead to an invalid Windows filename with an underscore to create a valid filename from the production title.

Finally, you’ll notice the custom User-Agent header that I added. I noticed that if you don’t supply the User-Agent, you’ll get a 406 Rejected error thrown back at you. In addition, it is just courteous to let people know what is requesting their stuff.

There are a few interesting things you could probably do to improve this script:

  • Find common prefixes for video titles and then put those videos into subdirectories named by the series
  • Number videos based on their position in the feed (like 001 - Full Throttle - Azure - Azure Deployment or something)

Anyways, if you’re looking to download a bunch of videos from TekPub (or any RSS feed, really) feel free to grab the above script.

One thought on “Download All the TekPub Videos from Your Account’s RSS Feed (using Windows PowerShell)

Leave a Reply

Your email address will not be published. Required fields are marked *