-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Your session will be automatically recovered when the module is imported, sweet! Get-RedditAccount also works as well
- Loading branch information
Showing
13 changed files
with
295 additions
and
6 deletions.
There are no files selected for viewing
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Function Get-DecryptedValue{ | ||
param($inputObj,$name) | ||
|
||
$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($inputObj) | ||
$result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) | ||
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) | ||
New-Variable -Scope Global -Name $name -Value $result -PassThru -Force | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,92 @@ | ||
Function Connect-RedditAccount { | ||
param($ClientSecret) | ||
[CmdletBinding()] | ||
param( | ||
$ClientSecret, | ||
$ClientID, | ||
$redirectURI, | ||
[Switch]$force) | ||
|
||
$configDir = "$Env:AppData\WindowsPowerShell\Modules\PSReddit\0.1\Config.ps1xml" | ||
$refreshToken = "$Env:AppData\WindowsPowerShell\Modules\PSReddit\0.1\Config.Refresh.ps1xml" | ||
#look for a stored password | ||
|
||
|
||
if (-not (Test-Path $configDir) -or $force){ | ||
if ($force){Write-verbose "`$force detected"} | ||
#create the file to store our Access Token | ||
Write-Verbose "cached Access Code not found, or the user instructed us to refresh" | ||
|
||
if (-not (Test-Path $refreshToken)){New-item -Force -Path $refreshToken -ItemType file } | ||
New-item -Force -Path "$configDir" -ItemType File | ||
|
||
$guid = [guid]::NewGuid() | ||
$URL = "https://www.reddit.com/api/v1/authorize?client_id=$clientID&response_type=code&state=$GUID&redirect_uri=$redirectURI&duration=permanent&scope=identity,history,mysubreddits,read,report,save,submit" | ||
|
||
#Display an oAuth login prompt for the user to user authorize our application, returns uri | ||
Show-OAuthWindow -url $URL | ||
|
||
#attempt to parse $uri to retrieve our AuthCode | ||
$regex = '(?<=code=)(.*)' | ||
|
||
try {$Reddit_authCode = ($uri | Select-string -pattern $regex).Matches[0].Value} | ||
catch {Write-Warning "did not receive an authCode, check ClientID and RedirectURi" | ||
return} | ||
|
||
$global:Reddit_authCode = $Reddit_authCode | ||
Write-Verbose "Received an authCode, $Reddit_authCode" | ||
|
||
write-debug "Pause here to test value of `$uri" | ||
Write-Verbose "Exchanging authCode for Access Token" | ||
|
||
try { | ||
#reddit uses basic auth, which means in PowerShell that we can provide our creds using a credential object | ||
$secpasswd = ConvertTo-SecureString $ClientSecret -AsPlainText -Force | ||
$credential = New-Object System.Management.Automation.PSCredential ($ClientID, $secpasswd) | ||
|
||
#retrieve Access Token | ||
$result = Invoke-RestMethod https://ssl.reddit.com/api/v1/access_token -Method Post -Body @{client_id=$clientId; state=$guid ; redirect_uri=$redirectURI; grant_type="authorization_code"; code=$Reddit_authCode} -ContentType "application/x-www-form-urlencoded" -ErrorAction STOP -Credential $credential | ||
} | ||
catch { | ||
Write-Warning "Something didn't work" | ||
Write-debug "Test the -body params for the Rest command" | ||
} | ||
|
||
Write-Debug 'go through the results of $result, looking for our token' | ||
if ($result.access_token){ | ||
Write-Output "Updated Authorization Token" | ||
$global:PSReddit_accessToken = $result.access_token} | ||
|
||
Write-Verbose "Storing token in $configDir" | ||
#store the token | ||
$password = ConvertTo-SecureString $PSReddit_accessToken -AsPlainText -Force | ||
$password | ConvertFrom-SecureString | Export-Clixml $configDir -Force | ||
|
||
Write-verbose "Storing refresh token in $refreshToken" | ||
$password = ConvertTo-SecureString $result.refresh_token -AsPlainText -Force | ||
$password | ConvertFrom-SecureString | Export-Clixml $refreshToken -Force | ||
|
||
} | ||
else{ | ||
#if the user did not specify -Force, or if the file path for a stored token already exists | ||
Write-Verbose "We're looking for a stored token in $configDir" | ||
try { | ||
$password = Import-Clixml -Path $configDir -ErrorAction STOP | ConvertTo-SecureString | ||
$refreshToken = Import-Clixml -Path $refreshToken -ErrorAction STOP | ConvertTo-SecureString | ||
} | ||
catch { | ||
Write-Warning "Corrupt Password file found, rerun with -Force to fix this" | ||
BREAK | ||
} | ||
|
||
Get-DecryptedValue -inputObj $password -name PSReddit_accessToken | ||
Get-DecryptedValue -inputObj $refreshToken -name PSReddit_refreshToken | ||
<# | ||
$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($password) | ||
$result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) | ||
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) | ||
$global:PSReddit_accessToken = $result #> | ||
'Found cached Cred' | ||
continue | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<# | ||
.SYNOPSIS | ||
Gets information about the currently logged in user | ||
.Description | ||
After you've connected using Connect-RedditAccount, you can use this cmdlet to get information about the currently logged in user | ||
.PARAMETER redditSession | ||
An optional session to use (like that returned from Connect-RedditSession) | ||
.Example | ||
Get-RedditAccount | ||
name : 1RedOne | ||
hide_from_robots : False | ||
gold_creddits : 0 | ||
link_karma : 2674 | ||
comment_karma : 19080 | ||
over_18 : True | ||
is_gold : False | ||
is_mod : False | ||
gold_expiration : | ||
has_verified_email : True | ||
inbox_count : 2 | ||
Created Date : 1/20/2010 6:44:21 PM | ||
.LINK | ||
https://github.com/1RedOne/PSReddit | ||
#> | ||
function Get-RedditAccount | ||
{ | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter( | ||
Position = 1, | ||
Mandatory = $false, | ||
ValueFromPipelineByPropertyName = $true | ||
)] | ||
[Alias("Link")] | ||
$accessToken=$Global:PSReddit_accessToken | ||
) | ||
|
||
|
||
$defaultDisplaySet = 'ID','name','Created Date','comment_karma','link_karma','gold_credits' | ||
|
||
#Create the default property display set | ||
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet(‘DefaultDisplayPropertySet’,[string[]]$defaultDisplaySet) | ||
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet) | ||
|
||
$uri = 'https://oAuth.reddit.com/api/v1/me' | ||
try {$response = (Invoke-RestMethod $uri -Headers @{"Authorization" = "bearer $accessToken"}) } | ||
catch{write-warning "Authentication failed, we should do something here"} | ||
|
||
$origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0 | ||
$created = $origin.AddSeconds($response.created) | ||
|
||
$response | select -ExcludeProperty created* -Property *,@{Name="Created Date";exp={$created}} | ||
|
||
$response.PSObject.TypeNames.Insert(0,'PSReddit.User') | ||
$response | Add-Member MemberSet PSStandardMembers $PSStandardMembers | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<# | ||
.SYNOPSIS | ||
Gets comments of a Reddit link | ||
.DESCRIPTION | ||
Uses the Reddit API to get comments made on a given link | ||
.PARAMETER id | ||
Internal id of the Reddit link | ||
#> | ||
function Get-RedditComment | ||
{ | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter( | ||
Position = 1, | ||
Mandatory = $true, | ||
ValueFromPipelineByPropertyName = $true | ||
)] | ||
[Alias("Link")] | ||
[string] | ||
$id | ||
) | ||
|
||
Process | ||
{ | ||
$uri = 'http://www.reddit.com/comments/{0}.json' -f $id | ||
|
||
$listings = (Invoke-RestMethod $uri) | Where kind -eq 'Listing' | ||
|
||
# Comments have a type 't1' in Reddit API | ||
$comments = $listings | %{ $_.data.children } | Where kind -eq 't1' | Select -Expand data | ||
|
||
$comments | %{ $_.PSObject.TypeNames.Insert(0,'PowerReddit.Comment'); $_ } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<# | ||
.SYNOPSIS | ||
Gets a list of Reddit links | ||
.DESCRIPTION | ||
Uses the Reddit API to get Reddit links from given subreddit(s) | ||
.PARAMETER Name | ||
Name of the Subreddit to fetch from. Can be an array. | ||
#> | ||
function Get-RedditLink | ||
{ | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter(Position=1, Mandatory=$false, ValueFromPipelineByPropertyName=$true)] | ||
[Alias("r","Subreddit")] | ||
[string[]] | ||
$Name = 'frontpage' | ||
) | ||
|
||
# Construct the Uri. Multiple subreddits can be joined with plusses | ||
$uri = 'http://www.reddit.com/r/{0}.json' -f [string]::Join('+', $Name) | ||
|
||
# Run the RestMethod in the current user context | ||
$response = (Invoke-RestMethod $uri -WebSession $script:currentRedditSession) | ||
|
||
# This is the listing. Contains before/after for pagination, and links | ||
$listing = $response | Where kind -eq 'Listing' | Select -Expand data | ||
|
||
# Links have type "t3" in Reddit API | ||
$links = $listing.children | Where kind -eq 't3' | select -expand data | ||
|
||
# Return the links with a custom type of [PowerReddit.Link]. We do this so | ||
# that they can be extended by psxml files, etc. | ||
$links | %{ $_.PSObject.TypeNames.Insert(0,'PowerReddit.Link'); $_ } | ||
|
||
} |
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