Last active
July 17, 2018 19:37
-
-
Save TylerLeonhardt/a1f2e0c9f0219051a06fa5c409307846 to your computer and use it in GitHub Desktop.
Importing Scripts from Tweets! #280characters
This file contains hidden or 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
# Importing Scripts directly from Tweets! #280characters | |
# | |
# NOTE: This should not be used for any production environment. Or any environment for that matter. Use https://PowerShellGallery.com | |
# | |
# | |
#################################### | |
# Example # | |
#################################### | |
# | |
# | |
# PS\> Import-Module .\TweetScript.psm1 | |
# PS\> Set-TweetScriptKeys -ConsumerKey <your key> -ConsumerSecret <your key> # from https://apps.twitter.com/ | |
# PS\> $script = Import-TweetScript -TweetUrl https://twitter.com/TylerLeonhardt/status/929040434386452480 | |
# PS\> . $script | |
# # Output goes here! | |
# | |
<# | |
.SYNOPSIS | |
Creates access token for Twitter API calls. To get keys go to: https://apps.twitter.com/ | |
.DESCRIPTION | |
Creates access token for Twitter API calls. To get keys go to: https://apps.twitter.com/ | |
.PARAMETER ConsumerKey | |
The ConsumerKey. You get this from https://apps.twitter.com/ | |
.PARAMETER ConsumerSecret | |
The ConsumerSecret. You get this from https://apps.twitter.com/ | |
.EXAMPLE | |
Set-TweetScriptKeys -ConsumerKey <your key> -ConsumerSecret <your key> | |
#> | |
function Set-TweetScriptKeys { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$ConsumerKey, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$ConsumerSecret | |
) | |
$Bytes = [System.Text.Encoding]::Default.GetBytes("$($ConsumerKey):$($ConsumerSecret)") | |
$EncodedText =[Convert]::ToBase64String($Bytes) | |
$EncodedText | |
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add("Authorization", "Basic $EncodedText") | |
$global:AccessToken = (Invoke-RestMethod https://api.twitter.com/oauth2/token?grant_type=client_credentials -Method POST -Headers $headers).access_token | |
} | |
<# | |
.SYNOPSIS | |
Import a script from Twitter using the Tweet's url. | |
.DESCRIPTION | |
Import a script from Twitter using the Tweet's url. | |
.PARAMETER TweetUrl | |
The tweet's url. You can get this from twitter.com | |
.PARAMETER Execute | |
(Optional) Will execute the script | |
.EXAMPLE | |
Set-TweetScriptKeys -ConsumerKey <your key> -ConsumerSecret <your key> | |
$script = Import-TweetScript -TweetUrl https://twitter.com/TylerLeonhardt/status/929040434386452480 | |
. $script | |
$script = Import-TweetScript -TweetUrl https://twitter.com/TylerLeonhardt/status/929040434386452480 -Execute | |
#> | |
function Import-TweetScript { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$TweetUrl, | |
[Parameter()] | |
[switch] | |
$Execute | |
) | |
if ($AccessToken -eq $null -or $AccessToken -eq "") { | |
Write-Host "Missing Access token. Please use Set-TweetScriptKeys. To get keys go to: https://apps.twitter.com/" | |
return | |
} | |
$tweetId = ($TweetUrl -Split '/status/')[1].Trim("/").Trim(" ") | |
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add("Authorization", "Bearer $AccessToken") | |
$tweet = (Invoke-RestMethod "https://api.twitter.com/1.1/statuses/show.json?id=$tweetId" -Headers $headers) | |
$scriptBlock = [Scriptblock]::Create($tweet.text) | |
if($Execute) { | |
Write-Host (. $scriptBlock) | |
} | |
return [Scriptblock]::Create($tweet.text) | |
} | |
Export-ModuleMember -Function Import-TweetScript,Set-TweetScriptKeys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment