nl.nlsw.Feed.psm1
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# __ _ ____ _ _ _ _ ____ ____ ____ ____ ____ ___ _ _ ____ ____ ____ # | \| |=== |/\| |___ | |--- |=== ==== [__] |--- | |/\| |--| |--< |=== # # @file nl.nlsw.Feed.psm1 # @copyright Ernst van der Pols, Licensed under the EUPL-1.2-or-later # @date 2020-09-08 # @author Ernst van der Pols #requires -version 5 using namespace System.Xml <# .SYNOPSIS Read an RSS/Atom Feed .DESCRIPTION Read an RSS or Atom web feed from the internet. The resulting XML document is returned. .PARAMETER URI The URI of the feed. .INPUTS string .OUTPUTS System.Xml.XmlDocument .LINK https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest #> function Read-Feed { [CmdletBinding()] [OutputType([System.Xml.XmlDocument])] param( [Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true)] [string]$URI = "https://podcast.npo.nl/feed/bach-van-de-dag.xml" ) begin { } process { try { write-verbose " GET $URI" $request = [System.Net.HttpWebRequest]::Create($URI) $request.Method = "GET" #$request.ContentType = "application/json" $response = $request.GetResponse() write-verbose " response $($response.StatusCode)" if ($response.StatusCode -eq [System.Net.HttpStatusCode]::OK) { $stream = $response.GetResponseStream() $document = [System.Xml.XmlDocument]::new() $document.Load($stream); $document } $response.Close() } catch { throw } } end { } } <# .SYNOPSIS Saves the attachments of an RSS/Atom Feed to a local folder .DESCRIPTION Read an RSS or Atom web feed from the internet. .PARAMETER Path The local folder to save the attachments in. .INPUTS System.Xml.XmlDocument .OUTPUTS System.IO.FileInfo .LINK https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest #> function Save-FeedAttachment { [CmdletBinding()] [OutputType([System.IO.FileInfo])] param( [Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true)] [System.Xml.XmlDocument]$InputObject, [Parameter(Mandatory=$false, Position=1)] [string]$Path ) begin { if ($Path) { if (!(Test-Path $Path)) { New-Item -Path $Path -ItemType "directory" } } } process { $InputObject | where-object { $_ -is [System.Xml.XmlDocument] } | foreach-object { $doc = $InputObject # rss/channel/item/link # select the items of the (single) channel $items = $doc.DocumentElement.SelectNodes("channel/item"); foreach ($item in $items) { $title = $item.SelectSingleNode("title").InnerText; write-verbose " item $title" $link = $item.SelectSingleNode("link").InnerText; write-verbose " link $link" $request = [System.Net.HttpWebRequest]::Create($link) $request.Method = "GET" $response = $request.GetResponse() if ($response.StatusCode -eq [System.Net.HttpStatusCode]::OK) { if ($response.Headers["Content-Disposition"]) { $fileName = $response.Headers["Content-Disposition"].Replace("attachment; filename=", "").Replace('"', ""); } else { $fileName = split-path $link -leaf } $stream = $response.GetResponseStream() #$reader = [System.IO.StreamReader]::new($stream) $outFileName = join-path $Path $fileName write-verbose " writing $outFileName" $outstream = [System.IO.File]::OpenWrite($outFileName); $stream.CopyTo($outstream); $outstream.Close(); get-item $outFileName } $response.Close() } } } end { } } Export-ModuleMember -Function * |