forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Save-PodcastEpisodes.ps1
68 lines (64 loc) · 2.6 KB
/
Save-PodcastEpisodes.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<#
.SYNOPSIS
Saves enclosures from a podcast feed.
.LINK
Save-WebRequest.ps1
.EXAMPLE
Save-PodcastEpisodes.ps1 https://www.youlooknicetoday.com/rss -UseTitle
Saves podcast episodes to the current directory.
#>
#Requires -Version 3
[CmdletBinding()] Param(
# The URL of the podcast feed.
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][Alias('Url')][uri] $Uri,
# Episodes before this date will be ignored.
[datetime] $After,
# Episodes after this date will be ignored.
[datetime] $Before,
# Includes only the given number of initial episodes, by publish date.
[int] $First,
# Includes only the given number of most recent episodes, by publish date.
[int] $Last,
# Use episode titles for filenames.
[switch] $UseTitle,
# Saves the episodes into a folder with the podcast name.
[switch] $CreateFolder
)
Begin
{
[regex] $invalidchars = "(?:$(([io.path]::GetInvalidFileNameChars() |foreach {'\x{0:X2}' -f [int]$_}) -join '|'))+"
}
Process
{
$channel = ([xml]((Invoke-WebRequest $Uri).Content)).rss.channel
$channel |Format-List |Out-String |Write-Verbose
$activity = "Downloading $($channel.title)"
[object[]] $episodes = Invoke-RestMethod $Uri
foreach($episode in $episodes) {$episode |Add-Member published ([datetime]$episode.pubDate)}
if($PSBoundParameters.ContainsKey('After')) {[object[]] $episodes = $episodes |where published -gt $After}
if($PSBoundParameters.ContainsKey('Before')) {[object[]] $episodes = $episodes |where published -lt $Before}
if($PSBoundParameters.ContainsKey('First')) {[object[]] $episodes = $episodes |sort published |select -First $First}
if($PSBoundParameters.ContainsKey('Last')) {[object[]] $episodes = $episodes |sort published |select -Last $Last}
if($CreateFolder) {New-Item ($channel.title -replace $invalidchars,'_') -ItemType Directory |Push-Location}
$i,$max = 0,($episodes.Count/100)
foreach($episode in $episodes)
{
$episode |Format-List |Out-String |Write-Verbose
$title = $episode.title |select -First 1
Write-Progress $activity $title -curr $episode.enclosure.url -percent ($i++/$max)
if($UseTitle)
{
$filename = if($episode.PSObject.Properties.Match('episode')) {$episode.episode + ' '} else {''}
$filename += $title -replace $invalidchars,'_'
$filename += [io.path]::GetExtension($episode.enclosure.url)
Invoke-WebRequest $episode.enclosure.url -OutFile $filename
(Get-Item $filename).CreationTime = $episode.published
}
else
{
Save-WebRequest.ps1 $episode.enclosure.url -CreationTime $episode.published
}
}
if($CreateFolder) {Pop-Location}
Write-Progress $activity -Completed
}