Skip to content

Instantly share code, notes, and snippets.

@jongalloway
Forked from DerAlbertCom/Channel9Downloader.ps1
Last active September 14, 2022 12:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jongalloway/935780 to your computer and use it in GitHub Desktop.
Save jongalloway/935780 to your computer and use it in GitHub Desktop.
PowerShell Scripts for Downloading Channel 9 Videos
<#PSScriptInfo
.VERSION 1.3
.AUTHOR jongalloway@gmail.com
.GUID da220b4e-e889-42dc-85cd-91e0f91a965e
.PROJECTURI https://gist.github.com/jongalloway/935780
.RELEASENOTES
1.2 adds regular expression title match (thanks, @meavk)
1.3 adds check for / at end of RSS URL
#>
<#
.DESCRIPTION
Downloads videos from Channel 9 event RSS feeds.
Updates: https://gist.github.com/jongalloway/935780
Install: https://www.powershellgallery.com/packages/Channel9Downloader/
#>
# --- settings ---
$feedUrl = "http://s.ch9.ms/Events/dotNetConf/2017/RSS/"
$mediaType = "mp4high"
[regex]$matchTitleExp = ''
$overwrite = $false
$destinationDirectory = join-path ([Environment]::GetFolderPath("MyDocuments")) "dotNetConf-2017-Video"
# --- locals ---
$webClient = New-Object System.Net.WebClient
# --- functions ---
function PromptForInput ($prompt, $default) {
$selection = read-host "$prompt`r`n(default: $default)"
if ($selection) {$selection} else {$default}
}
function DownloadEntries {
param ([string]$feedUrl, [regex]$expression)
$feed = [xml]$webClient.DownloadString($feedUrl)
$progress = 0
$pagepercent = 0
$entries = ($feed.rss.channel.item | where {$_.title -match $expression}).Count
if ($entries -eq $null)
{
echo 'Invalid expression'
return;
}
$invalidChars = [System.IO.Path]::GetInvalidFileNameChars()
$feed.rss.channel.item | where { $_.title -match $expression } | foreach {
$url = New-Object System.Uri($_.enclosure.url)
$name = $_.title
$extension = [System.IO.Path]::GetExtension($url.Segments[-1])
$fileName = $name + $extension
$invalidchars | foreach { $filename = $filename.Replace($_, ' ') }
$saveFileName = join-path $destinationDirectory $fileName
$tempFilename = $saveFilename + ".tmp"
$filename
if ((-not $overwrite) -and (Test-Path -path $saveFileName))
{
write-progress -activity "$fileName already downloaded" -status "$pagepercent% ($progress / $entries) complete" -percentcomplete $pagepercent
}
else
{
write-progress -activity "Downloading $fileName" -status "$pagepercent% ($progress / $entries) complete" -percentcomplete $pagepercent
$webClient.DownloadFile($url, $tempFilename)
rename-item $tempFilename $saveFileName
}
$pagepercent = [Math]::floor((++$progress)/$entries*100)
}
}
# --- do the actual work ---
[string]$feedUrl = PromptForInput "Enter feed URL" $feedUrl
[string]$mediaType = PromptForInput "Enter media type`r`n(options:Wmv,WmvHigh,mp4,mp4high,mp3)" $mediaType
if (!$feedUrl.EndsWith('/')) {$feedUrl += '/'}
$feedUrl += $mediaType
[regex]$matchTitleExp = PromptForInput "Enter regex to match title with, (A|B) to specify a list A,B, default: all" $matchTitleExp
[string]$destinationDirectory = PromptForInput "Enter destination directory" $destinationDirectory
# if dest dir doesn't exist, create it
if (!(Test-Path -path $destinationDirectory)) { New-Item $destinationDirectory -type directory }
DownloadEntries $feedUrl $matchTitleExp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment