Skip to content

Instantly share code, notes, and snippets.

@githubbery
Last active October 10, 2015 08:56
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 githubbery/28c8397ea87d33c5e7d8 to your computer and use it in GitHub Desktop.
Save githubbery/28c8397ea87d33c5e7d8 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Octbober 2015 Powershell.org Scripting Gamnes Puzzle entry
.DESCRIPTION
This advanced function retrieves news feeds from the BBC. The vanilla command defaults to the BBC's "Top Stories" but may be directed to any feed among the popular feeds listed on this page: http://www.bbc.co.uk/news/10628494
.EXAMPLE
This example displays the default feed by publishdate in list format
Get-RSSFeed | sort Publishdate -Descending | fl Headline,Description,Publishdate,Link
-------------------------- EXAMPLE 2 --------------------------
This example uses the -Uri parameter to retrieve the US & Canada feed
Get-RSSFeed -Uri http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml | sort Publishdate -Descending | fl Headline,Description,Publishdate,Link
-------------------------- EXAMPLE 3 --------------------------
This example uses the -ViewNewestStory parameter to launch in Internet Explorer the newest story from the Latin America feed
Get-RSSFeed -Uri http://feeds.bbci.co.uk/news/world/latin_america/rss.xml -ViewNewestStory
-------------------------- EXAMPLE 4 --------------------------
This example exports the default feed to a .csv file
Get-RSSFeed | export-csv c:\BBC_TopStories.csv -notypeinformation
-------------------------- EXAMPLE 5 --------------------------
This example displays the default feed Headlines and Publishdate in tabular format
Get-RSSFeed | ft Headline,Publishdate
.LINK
http://powershell.org/wp/2015/10/03/october-2015-scripting-games-puzzle/
#>
function Get-RSSFeed
{
param
(
$Uri,
[switch]$ViewNewestStory
)
# url param
if ($Uri)
{
# rss feed collection with defined url
$feed = (Invoke-WebRequest -Uri $Uri).content
}
# no url param
else
{
# rss feed collection default (top stories)
$feed = (Invoke-WebRequest -Uri http://feeds.bbci.co.uk/news/rss.xml).content
}
# feed headlines
$regexTitle = '(?ms)<title>(.+?)</title>'
$feedTitle = ([regex]::Matches($feed,$regexTitle)| foreach {$_.groups[1].value -split "</title>, " }) | select -Skip 2
# feed descriptions
$regexDescription = '(?ms)<description>(.+?)</description>'
$feedDescription = ([regex]::Matches($feed,$regexDescription)| foreach {$_.groups[1].value -split "</title>, " }) | select -Skip 1
# feed urls
$regexLink = '(?ms)<link>(.+?)</link>'
$feedLink = ([regex]::Matches($feed,$regexLink)| foreach {$_.groups[1].value -split "</title>, " }) | select -Skip 2
# feed publish date/time
$regexPubdate = '(?ms)<pubDate>(.+?)</pubDate>'
$feedPubdate = ([regex]::Matches($feed,$regexPubdate)| foreach {$_.groups[1].value -split "</title>, " })
# table builder
[int]$selector = 0
foreach ($title in $feedTitle)
{
$writeDescription = $feedDescription[$selector]
$writePubdate = [datetime]($feedPubdate[$selector]).substring(5)
$writeLink = $feedLink[$selector]
$title | Add-Member -Name HeadLine -MemberType NoteProperty -Value ($title)
$title | Add-Member -Name Description -MemberType NoteProperty -Value ($writeDescription)
$title | Add-Member -Name Publishdate -MemberType NoteProperty -Value ($writePubdate)
$title | Add-Member -Name Link -MemberType NoteProperty -Value ($writeLink)
$selector = $selector+1
}
# rss table
$rssTable = $feedTitle | select Headline,Description,Publishdate,Link
# view newest story param
if ($viewNewestStory)
{
$newestStory = ($rssTable | Sort Publishdate -Descending | select -First 1).Link
& "C:\Program Files\Internet Explorer\iexplore.exe" $newestStory
break
}
# default output
$rssTable
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment