Created
March 17, 2016 19:58
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
#requires -version 3 | |
function Read-Reddit | |
{ | |
[CmdletBinding()] | |
param ( | |
[Parameter(Position = 0)] | |
[ValidateNotNullOrEmpty()] | |
$Subreddit = 'PowerShell', | |
[parameter(Position = 1)] | |
[ValidateScript({ $_ -gt 0 })] | |
[int]$Top = 10, | |
[ValidateScript({ $_ -ge 0 })] | |
[int]$Skip = 0, | |
# I tried parsing the html and decided that was stupid | |
# I then tried parsing the xml from RSS, better but still not ideal | |
# I was very happy when I found you can pull the info in json | |
# I hope im not breaking the reddit json API rules as this does not authn with oauth | |
[Parameter(DontShow = $true)] | |
$BaseUrl = "https://www.reddit.com/r/$Subreddit/top/.json?sort=top&t=all&limit=$Top" | |
) | |
begin | |
{ | |
$subredditContent = Invoke-WebRequest $BaseUrl -UseBasicParsing | |
$posts = $($subredditContent.Content | ConvertFrom-Json).data.children | Select-Object -ExpandProperty data | |
# I looked up the easiest way to convert from unix time and converted it to powershell | |
$epoch = ([datetime]"1/1/1970 +0").ToUniversalTime() | |
function To-NonStupidDateTime | |
{ | |
param ( $StupidTime ) | |
if ($StupidTime -ne $false) | |
{ | |
return $epoch.AddSeconds($StupidTime) | |
} | |
else | |
{ | |
return $false | |
} | |
} | |
} | |
process | |
{ | |
# Add whatever properties you like to your custom object per https://github.com/reddit/reddit/wiki/JSON | |
$posts | ForEach-Object { | |
[PSCustomObject]@{ | |
Score = $_.score | |
Author = $_.author | |
Created = To-NonStupidDateTime -StupidTime $_.created | |
Edited = To-NonStupidDateTime -StupidTime $_.edited | |
Comments = $_.num_comments | |
Title = $_.title | |
SelfText = $_.selftext | |
} | |
} | Select-Object -Skip $Skip | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment