Download from an RSS Feed with Powershell

I was looking to download the videos from MIX11 – I think it is just great being able to pick’n’choose and watch in your own time (while folding the laundry or whatever developers do in their spare time).

I’ve previously written my own (parallelized) RSS downloader in C#, but then I came across a really simple, really cool piece of Powershell script from Scott Hanselman. Unfortunately the script wasn’t able to restart downloading where it had left off in case the script is stopped (like my computer automatically shutting down at 01 am), so I added a few extra lines to make that happen:

Code Snippet
  1. $feed=[xml](New-Object System.Net.WebClient).DownloadString("http://channel9.msdn.com/Events/MIX/MIX11/RSS/wmvhigh")
  2. foreach($i in $feed.rss.channel.item)
  3. {    
  4.     $url = New-Object System.Uri($i.enclosure.url)
  5.     $file = $url.Segments[1]
  6.   
  7.     if (! (Test-Path $file))
  8.     {
  9.         $url.ToString()    
  10.         (New-Object System.Net.WebClient).DownloadFile($url, $file)
  11.     }
  12. }
  13. Write-Host "Script finished. Press any key to continue …"
  14. $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Word of warning: I’ve previously had problems downloading from RSS because not all feeds are created equal – especially the Yahoo Media metadata extensions (mrss) were a royal pain to parse. Thus the script may not just work out of the box with any feed.

I’ve always had a sort of on / off relationship with Powershell – very often I find it easier to just crank up VS and create a quick C# console-app, or even to go back to the 80’s and create a .BAT file. This sample, however, has made me think again that perhaps I should try to become a little more proficient in PS…

The formatting and coloring is thanks to PowerGUI which supplies an Intellisense-editor (for strange reasons that feature is not part of VS). On Codeplex there’s also a VSIX package for plugging PowerGUI into VS, but I haven’t tried that yet.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s