Skip to content

Instantly share code, notes, and snippets.

@DerAlbertCom
Created November 21, 2012 20:08
Show Gist options
  • Save DerAlbertCom/4127330 to your computer and use it in GitHub Desktop.
Save DerAlbertCom/4127330 to your computer and use it in GitHub Desktop.
A comfortable Channel 9 video download script
# channel9 downloader, downloads complete channel9 videos from there rss feeds
#
# Features
# * Checks for allready downloaded files
# * use a tempfile to ensure full downloads
# * gives the download file a name based on the title of the rss entry in the feed
# * creates a proper directory structure
# * set the creation und modified date to the date in the rss entry
#
# by Albert Weinert http://der-albert.com
#
# usage: ch9downloader.ps1 feedUrl
# example: ch9downloader.ps1 http://channel9.msdn.com/Events/WindowsAzureConf/2012/RSS/mp4high
#
# i'm not a powershell poweruser, if you have suggestion for better scripting, let me know
# your base directory for the downloads, change it to your needs
# the script creates subdirectories based on the rss fedd url
# with the example the download goes to
# \\DISKSTATION\video\anderes\channel9\Events\WindowsAzureConf\2012
$destinationDirectory = "\\DISKSTATION\video\anderes\channel9\"
$feedUrl = $Args[0]
$url = New-Object System.Uri($feedUrl)
$overwrite = $false
for ($i=1; $i -le ($url.Segments.Length-3); $i++)
{
$destinationDirectory = join-path $destinationDirectory $url.Segments[$i]
}
function DownloadEntries {
param ([string]$feedUrl)
$webClient = New-Object System.Net.WebClient
$webClient.proxy.credentials=[system.net.credentialcache]::defaultnetworkcredentials
$feed = [xml]$webClient.DownloadString($feedUrl)
$progress = 0
$pagepercent = 0
$entries = $feed.rss.channel.item.Length
$invalidChars = [System.IO.Path]::GetInvalidFileNameChars()
$feed.rss.channel.item | foreach {
$url = $Null
$name = $_.title
$url = New-Object System.Uri($_.enclosure.url)
$extension = [System.IO.Path]::GetExtension($url.Segments[-1])
$fileName = $name + $extension
$invalidchars | foreach { $filename = $filename.Replace($_, ' ') }
$saveFileName = join-path $destinationDirectory $fileName
$tempFilename = $saveFilename + ".tmp"
$progress++
if ((-not $overwrite) -and (Test-Path -path $saveFileName))
{
write-progress -activity "$fileName already downloaded" -status "$pagepercent% of current feed complete" -percentcomplete $pagepercent
}
else
{
write-progress -activity "Downloading $url" -status "$pagepercent% of current feed complete ($progress of $entries)" -percentcomplete $pagepercent
try {
$wc = (New-Object System.Net.WebClient)
$wc.proxy.credentials=[system.net.credentialcache]::defaultnetworkcredentials
$wc.DownloadFile($url, $tempFilename)
if (Test-Path -path $tempFilename)
{
rename-item $tempFilename $saveFileName
}
} catch [Exception] {
write-host $_.Exception.GetType().FullName
write-host $_.Exception.Message
write-host $_.Exception.StackTrace
throw
}
}
$date = [System.DateTime]::Parse($_.pubDate)
$file = Get-Item $saveFilename
$file.LastWriteTime = $date
$file.CreationTime = $date
$pagepercent = [Math]::floor(($progress)/$entries*100)
}
}
if (!(Test-Path -path $destinationDirectory)) { New-Item $destinationDirectory -type directory }
DownloadEntries $feedUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment