Last active
May 20, 2024 02:38
-
-
Save arebee/a7a77044c77443effaeddbe3730af4ad to your computer and use it in GitHub Desktop.
Powershell script to create an RSS feed from a local set of MP3s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on the format described at http://podcast411.com/howto_1.html | |
function createRssElement{ | |
param( | |
[string]$elementName, | |
[string]$value, | |
$parent | |
) | |
$thisNode = $rss.CreateElement($elementName) | |
$thisNode.InnerText = $value | |
$null = $parent.AppendChild($thisNode) | |
return $thisNode | |
} | |
$mp3s = gci $pwd -filter *.mp3 # -recurse | |
[xml]$rss = '' | |
$root = $rss.CreateElement('rss') | |
$null = $root.SetAttribute('version','2.0') | |
$null = $rss.AppendChild($root) | |
$rssChannel = $rss.CreateElement('channel') | |
$null = $root.AppendChild($rssChannel) | |
# Channel metadata | |
$null = createRssElement -elementName 'title' -value 'Your title' -parent $rssChannel | |
$null = createRssElement -elementName 'description' -value 'Your description' -parent $rssChannel | |
$null = createRssElement -elementName 'link' -value 'http://example.com' -parent $rssChannel | |
$null = createRssElement -elementName 'language' -value 'en-US' -parent $rssChannel | |
$null = createRssElement -elementName 'copyright' -value 'entity' -parent $rssChannel | |
$null = createRssElement -elementName 'lastBuildDate' -value $([datetime]::Now.ToString('s')) -parent $rssChannel | |
$null = createRssElement -elementName 'pubDate' -value $([datetime]::Now.ToString('s')) -parent $rssChannel | |
# Items in rss feed | |
foreach ($item in $mp3s) | |
{ | |
$thisItem = createRssElement -elementName 'item' -value '' -parent $rssChannel | |
$null = createRssElement -elementName 'title' -value $item.Name -parent $thisItem | |
$null = createRssElement -elementName 'link' -value 'http://example.com' -parent $thisItem | |
$null = createRssElement -elementName 'description' -value 'Your description' -parent $thisItem | |
$null = createRssElement -elementName 'guid' -value $item.FullName -parent $thisItem | |
$enclosure = createRssElement -elementName 'enclosure' -value '' -parent $thisItem | |
$null = createRssElement -elementName 'category' -value "Podcasts" -parent $thisItem | |
$null = createRssElement -elementName 'pubDate' -value $item.LastWriteTimeUtc.ToString('u') -parent $thisItem | |
# The URL is by default the file path. | |
# You may want something like: | |
# $null = $enclosure.SetAttribute('url',"http://example.com/pathToMp3s/$($item.Name)") | |
$null = $enclosure.SetAttribute('url',"file://$($item.FullName)") | |
$null = $enclosure.SetAttribute('length',"$($item.Length)") | |
$null = $enclosure.SetAttribute('type','audio/mpeg') | |
} | |
$_filename = join-path $pwd "test.rss" | |
$xmlWriterSettings = new-object System.Xml.XmlWriterSettings | |
$xmlWriterSettings.CloseOutput = $true | |
# 4 space indent | |
$xmlWriterSettings.IndentChars = ' ' | |
$xmlWriterSettings.Indent = $true | |
# $xmlWriterSettings.ConformanceLevel = 2 | |
write-verbose $('xml formatting - writing to ' + $_filename) | |
$xmlWriter = [System.Xml.XmlWriter]::Create($_filename, $xmlWriterSettings) | |
$rss.Save($xmlWriter) | |
$xmlWriter.Close() | |
Write-Verbose ("Tabbify finish " + ("*" * 60)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey this is awesome work!
Have you used this for anything worthwhile?
I actually am going to adapt this for a site that doesn't have an RSS feed but does have easy access to a JSON file I have parsed through.
Just one question about your script... why do you have all the $null = in front of the commands?