-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadRss.ps1
104 lines (80 loc) · 2.62 KB
/
DownloadRss.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
$url = "https://rss.art19.com/how-i-built-this"
# Set the directory where the downloaded episodes will be saved
$directory = "D:\Temp\Podcasts"
$archiveFile = "D:\Temp\Podcasts\downloaded.csv"
# Create the directory if it doesn't already exist
function Get-UrlHash {
param(
[string]$Url
)
$urlBase = $Url -replace '\?.*$', ''
$urlHashBin = [System.Security.Cryptography.MD5]::Create().ComputeHash([System.Text.Encoding]::UTF8.GetBytes($urlBase))
$urlHash = [System.BitConverter]::ToString($urlHashBin).Replace('-', '').ToLower()
return $urlHash
}
function Check-ArchiveFile {
param(
[string]$ArchiveFile,
[string]$Url
)
### Format:
# UrlHash,Title
if (!(Test-Path $ArchiveFile)) {
return $false
}
$archive = Get-Content $ArchiveFile | ConvertFrom-Csv
return $archive.UrlHash -contains $(Get-UrlHash($Url))
}
function Record-ArchiveFile {
param(
[string]$ArchiveFile,
[string]$Url,
[string]$Title
)
if (!(Test-Path $ArchiveFile)) {
New-Item -ItemType File -Path $ArchiveFile | Out-Null
# Add CSV headers
Add-Content $ArchiveFile "UrlHash,Title"
}
$urlHash = Get-UrlHash($Url)
Add-Content $ArchiveFile "${urlHash},${Title}"
}
function Download-RssFeed {
param(
[string]$Url,
[string]$Directory,
[string]$ArchiveFile
)
if (!(Test-Path $directory)) {
New-Item -ItemType Directory -Path $directory | Out-Null
}
$client = New-Object System.Net.WebClient
# Download the RSS feed and parse it as XML
[xml]$rss = $client.DownloadString($url)
# Loop through each item in the RSS feed
foreach ($item in $rss.rss.channel.item) {
$mp3Url = $item.enclosure.url
$title = $item.title[0]
# Check if the episode is already downloaded
$isArchive = Check-ArchiveFile -ArchiveFile $archiveFile -Url $mp3Url
if ($isArchive) {
Write-Debug "$title is already downloaded, skipping."
Continue
}
$filename = "$($item.episode). $title.mp3.part" -replace '[\\/:*?"<>|]', '' # Remove the special characters
Write-Debug "Downloading $mp3Url..."
Write-Debug "Title: $title"
$filepath = "$directory\$filename"
$client.DownloadFile($mp3Url, $filepath)
# Check if download was successful
if ($client.ResponseHeaders['Content-Length'] -eq (Get-Item $filepath).Length) {
# Rename the file to remove the .part extension
Rename-Item -Path $filepath -NewName ($filepath -replace '\.part$', '') -Force | Out-Null
Write-Debug "COMPLETED: $title"
Record-ArchiveFile -ArchiveFile $archiveFile -Url $mp3Url -Title $title
}
else {
Write-Error "Download failed for $title"
}
}
}