Skip to content

Instantly share code, notes, and snippets.

@Windos
Created October 16, 2015 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Windos/b18f5379b88d26a640df to your computer and use it in GitHub Desktop.
Save Windos/b18f5379b88d26a640df to your computer and use it in GitHub Desktop.
October 2015 Scripting Game Submissions, Windos
function Get-RSSFeed
{
<#
.SYNOPSIS
Gets data from a RSS Feed.
.DESCRIPTION
The Get-RSSFeed function retrieves the title, link and excerpt from a specified rss feed.
Multiple URIs can be provided as parameters or piped to the function.
By default only post titles are displayed and optional switches can be provided to change this behaviour.
.INPUTS
System.String
You can pipe input to this function
.OUTPUTS
PSCustomObject
.PARAMETER Uri
Specifies the Uniform Resource Identifier (URI) of the Internet resource to which the web request is sent.
This parameter supports HTTP, HTTPS, FTP, and FILE values.
This parameter is required. The parameter name (-Uri) is optional.
.NOTES
If you try this function on a RSS Feed and do not get back expected results, please submit a bug report
via GitHub including the Uri used and the output you recieved.
Run `Get-Help Get-RSSFeed -Online` to open a web browser to the relevant GitHub repo.
.EXAMPLE
Get-RSSFeed -Uri http://blog1.github.io/feed.xml
Gets the titles of recent blog posts from Blog1
.EXAMPLE
http://blog1.github.io/feed.xml | Get-RSSFeed
Gets the titles of recent blog posts from Blog1, via a piped Uri
.EXAMPLE
Get-RSSFeed -Uri http://blog1.github.io/feed.xml, http://feeds.feedburner.com/blog2
Gets the titles of recent blog posts from Blog1 and Blog2
.EXAMPLE
Get-RSSFeed -Uri http://blog1.github.io/feed.xml -IncludeLink
Gets the titles and links of recent blog posts from Blog1
.EXAMPLE
Get-RSSFeed -Uri http://blog1.github.io/feed.xml, http://feeds.feedburner.com/blog2 -IncludeLink -IncludeExcerpt
Gets the titles, links and excerpts of recent blog posts from Blog1 and Blog2
.LINK
https://github.com/Windos/ScriptingGames
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param
(
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[String[]] $Uri,
[Switch] $IncludeLink,
[Switch] $IncludeExcerpt
)
process
{
foreach ($Link in $Uri)
{
$Feed = Invoke-RestMethod -Uri $Link
foreach ($Post in $Feed)
{
# Some Atom feeds weren't nice, the following if statements made my test cases work.
$Title = $Post.Title
if ($Title.ToString() -eq 'title')
{
$Title = $Title.InnerText
}
$Link = $Post.Link
if ($Link.ToString() -eq 'link')
{
$Link = $Link.href
}
if ($Post.Description)
{
$Excerpt = $Post.description.InnerText.Split('.')[0]
}
elseif ($Post.Content)
{
$Excerpt = ($Post.Content.InnerText -replace '<[^>]*>', '').TrimStart().Split('.')[0]
}
else
{
$Excerpt = ''
}
if ($Excerpt.Length -gt 75)
{
$Excerpt = $Excerpt.Substring(0, 75) + '...'
}
$Result = New-Object -TypeName PSCustomObject -Property @{
'Title' = $Title
'Link' = $Link
'Excerpt' = $Excerpt
}
$defaultDisplaySet = @('Title')
if ($IncludeLink) { $defaultDisplaySet += 'Link' }
if ($IncludeExcerpt) { $defaultDisplaySet += 'Excerpt' }
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’,[string[]]$defaultDisplaySet)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
$Result
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment