It’s late at night.
I should go to sleep.
But I love this podcast called Empire and feel this itch to download its episodes and store locally for re-listening to later. In the past I’ve used castget to download podcasts, but it’s not very flexible – it cannot name the files the way I want. All it can do is pick up the title, and podcasts like Empire don’t have a season or episode number in the title so all the files are in a alphabetical order rather the the correct order.
I poke around the RSS feed of the podcast and realize that it has all this info.
1 2 3 4 |
<pubDate>Tue, 04 Apr 2023 01:00:00 -0000</pubDate> <itunes:episodeType>full</itunes:episodeType> <itunes:season>2</itunes:season> <itunes:episode>20</itunes:episode> |
So it’s presumeably just a case of me parsing the feed and downloading the episodes. Surely this can be done easily via PowerShell.
Sure enough, it can be. And happy with the progress I made here I set about downloading episodes of a few other podcasts too I had my eyes on. The result is a bunch of scripts in this repo.
Here’s the one for the Empire podcast. It may change a bit from what’s in the repo as it’s unlikely I will keep updating it here.
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 |
$feedUrl = "https://feeds.megaphone.fm/empirepodcast" $outDir = $Global:PodcastPath + [IO.Path]::DirectorySeparatorChar + "Empire" # Number of digits in the season and episodes $seasonPadding = 2 $episodePadding = 3 # Should I include season and episode in the filename? What about pubdate? $skipSeasonEpisode = $false $includePubDate = $false # === Typically there should be nothing to change past this point === # === Except any changes to the title identifier in the loop below === if (!(Test-Path -Path "$outDir" -PathType Container)) { New-Item -Path "$outDir" -ItemType Directory } try { $webRequest = Invoke-WebRequest -Uri $feedUrl -ErrorAction Stop if ($webRequest.StatusCode -ne 200) { Write-Error "Something went wrong. StatusCode $($webRequest.StatusCode) - $($webRequest.StatusDescription)" } } catch { Write-Error "Something went wrong: $($_.Exception.Message)" } $xmlContent = [xml]$webRequest.Content foreach ($item in $xmlContent.rss.Channel.item) { # Get the real title from the feed $realTitle = $item.title # Replace any spaces at the beginning and end of the title # Remove certain characters $episodeTitle = $realTitle -replace '[/:]','' -replace '^\s+','' -replace '\s+$','' # If Season or Episode exists then capture them with some padding # Only do this if I haven't set the toggle above to skip if (!$skipSeasonEpisode) { if ($item.season) { $seasonNumber = "S{0:d$seasonPadding}" -f [int]$item.season } else { $seasonNumber = "" } if ($item.episode) { $episodeNumber = "E{0:d$episodePadding}" -f [int]$item.episode } else { $episodeNumber = "" } # Add a space if either the season or episode exists if ($seasonNumber.Length -eq 0 -and $episodeNumber.Length -eq 0) { $spacer = "" } else { $spacer = " " } } else { # Initialize these variables coz sometimes I copy paste the scripts $seasonNumber = "" $episodeNumber = "" $spacer = "" } if ($includePubDate) { $pubDate = Get-Date $item.pubDate -Format "yyyyMMddHHmm " } else { $pubDate = "" } # Generate the title $title = "${pubDate}${seasonNumber}${episodeNumber}${spacer}$episodeTitle" # The download Url $downloadUrl = $item.enclosure.url # Figure out the output file name $outputFile = "${outDir}/${title}.mp3" # Check if the output file exists; download if it doesn't if (Test-Path -Path $outputFile) { Write-Host "Skipping $outputFile as it already exists" } else { Write-Host "Downloading $downloadUrl as $outputFile" Invoke-WebRequest -Uri "$downloadUrl" -OutFile $outputFile } } |
Podcast Recommendation
I do highly recommend the Empire podcast, though, if you love a bit of history. It’s such a money pit though as they keep interviewing writers and recommending books in each episode, such that I end up buying a ton of books. 😄 Not that I have time to read these, so currently I have a huge pile of unread books and a massive guit-trip about spending money and not having space to store these! I should be spending time reading them instead of creating scripts like these… 🙃
Book Recommendation
Speaking of which, I am currently reading Peter Frankopan’s excellent “The First Crusade“. I came across it in the first episode of Season 2 and so far I am enjoying it. Only wish I had more time to read it properly. I finally switched to its audiobook version today (am about half-way through the book and progress has been slow, so time for new measures). I love the narrator so I am glad with the switch. This is a book best read though, atleast for a major part, as there’s so many names and places and it’s difficult to keep track of these when listening. I constantly rewind as I have missed something. With a physical book it’s way easier to keep going back a few pages or checking out the index.
I had no idea who Alexios Komnenos was until I read this book. Now I do. ☺️