Skip to content

Instantly share code, notes, and snippets.

@derekables
Last active March 17, 2016 05:07
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 derekables/c71be44b32485fc0c85c to your computer and use it in GitHub Desktop.
Save derekables/c71be44b32485fc0c85c to your computer and use it in GitHub Desktop.
r/Powershell Daily Challenge - 3/17/16 (A VERY simple/quick example)
<#
.Synopsis
Get the recently posted news on the front page of Reddit! Alternatively, target a subreddit
.DESCRIPTION
Get the recently posted news on the front page of Reddit! Alternatively, target a subreddit
.EXAMPLE
Get-RedditFeed -NumberOfResults 10
.EXAMPLE
Get-RedditFeed -SubReddit r/Powershell -NumberOfResults 5
#>
function Get-RedditFeed
{
[CmdletBinding()]
Param
(
# Subreddit, in the format r/Subreddit
[Parameter(Position=0)]
$Subreddit,
# The number of results you would like to display
[int]$NumberOfResults = 20
)
Begin {
}
Process {
if ($Subreddit -ne $null) {
#Note the formatting here. This is why it was a 'Quick' example. You have to know not to include /r/
Invoke-WebRequest -Uri https://www.reddit.com/r/$Subreddit/.rss -OutFile $env:TEMP\redditrss.xml
}
else {
Invoke-WebRequest -Uri https://www.reddit.com/.rss -OutFile $env:TEMP\redditrss.xml
}
[xml]$XMLFile = Get-Content $env:TEMP\Redditrss.xml
$Entries = $XMLFile.feed.entry
$i= [int]0
Foreach ($entry in $Entries) {
if ($i -lt $NumberOfResults) {
$i++
[PSCustomObject]@{
'Updated' = [DateTime]$entry.updated
'Submitted By' = $Entry.FirstChild.name
'Title' = $entry.Title
}
}
else {break}
}
}
End{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment