Last active
March 17, 2016 05:07
-
-
Save derekables/c71be44b32485fc0c85c to your computer and use it in GitHub Desktop.
r/Powershell Daily Challenge - 3/17/16 (A VERY simple/quick example)
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
<# | |
.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