Skip to content

Instantly share code, notes, and snippets.

@tigattack
Last active December 30, 2020 00:00
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 tigattack/e02e7039a07404f9df48e28a4558467c to your computer and use it in GitHub Desktop.
Save tigattack/e02e7039a07404f9df48e28a4558467c to your computer and use it in GitHub Desktop.
"Days since last incident" automated meme generator
#######################
## GitHub Configuration
#######################
$githubUser = 'tigattack'
$githubSecret = ''
$gistID = 'e02e7039a07404f9df48e28a4558467c'
$githubURI = "https://api.github.com/user"
$githubGistURI = "https://api.github.com/gists"
##############
## GitHub Auth
##############
## Bastardised from https://blog.darrenjrobinson.com/searching-and-retrieving-your-github-gists-using-powershell/
# GitHub Client ID/username
$clientID = $githubUser
# GitHub API Client Secret
$clientSecret = $githubSecret
# Basic Auth
$Bytes = [System.Text.Encoding]::utf8.GetBytes("$($clientID):$($clientSecret)")
$encodedAuth = [Convert]::ToBase64String($Bytes)
$Headers = @{Authorization = "Basic $($encodedAuth)"; Accept = 'application/vnd.github.v3+json'}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$auth = Invoke-RestMethod -Method Get -Uri $githubURI -Headers $Headers -SessionVariable GITHUB
###############
## Get Gist txt
###############
$gist = Invoke-RestMethod -Method Get -Uri $githubGistURI/$gistID -WebSession $GITHUB
$daysSinceContent = ($gist.files.'DaysSince.txt'.content)
########################
## Imgflip configuration
########################
$username = ''
$password = ''
$templateID = ''
$text0 = 'days since last incident'
$text1 = $daysSinceContent
##########################
## Imgflip meme generation
##########################
If ($daysSinceContent -eq '0') {
$Params = @{
URI = 'https://api.imgflip.com/caption_image'
Method = 'POST'
Body = @{
'username' = $username
'password' = $password
'template_id' = $templateID
'boxes[0][text]' = $text0.ToUpper()
'boxes[1][text]' = 'Zero!'
'boxes[1][color]' = '#FF0000'
}
}
}
Else {
$Params = @{
URI = 'https://api.imgflip.com/caption_image'
Method = 'POST'
Body = @{
'username' = $username
'password' = $password
'template_id' = $templateID
'text0' = $text0
'text1' = $text1
}
}
}
$memeURL = (Invoke-RestMethod @Params).Data.URL
#######################
## HTML file generation
#######################
$html =
@"
<!DOCTYPE html>
<html>
<head>
<title>Days since last incident</title>
<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
width: 72%;
height: auto;
}
</style>
</head>
<body>
<img src=`"$memeURL`" alt=`"Sam disaster girl meme`">
</body>
</html>
"@
##############
## Update Gist
##############
$text1 = [System.Decimal]::Parse($daysSinceContent)
$text1++
$gistPatch = [PSCustomObject]@{
description = $gist.description
files = @{
$gist.files.'index.html'.filename = @{
content = $html
filename = $gist.files.'index.html'.filename
}
$gist.files.'DaysSince.txt'.filename = @{
content = $text1.ToString()
filename = $gist.files.'DaysSince.txt'.filename
}
}
}
$patchResult = Invoke-RestMethod -Method Patch -Uri $githubGistURI/$gistID -Body (ConvertTo-Json $gistPatch) -WebSession $GITHUB
<!DOCTYPE html>
<html>
<head>
<title>Days since last incident</title>
<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
width: 72%;
height: auto;
}
</style>
</head>
<body>
<img src="https://i.imgflip.com/4rz9bw.jpg" alt="Sam disaster girl meme">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment