Damnit. After getting into the habit of writing a before bed blog post as my “write a daily blog post” excercise last night I accidently deleted it today. That’s not to say I didn’t attempt publishing it last night, but I’ve been writing a Powershell script to write and publish from my Ubuntu Server laptop. Unfortunately, I’d written the REST API call to include the content in the post in as a parameter in the URL POST instead of JSON body so last night’s attempted publication failed. So today, as part of my “do something meaningful with your weekened” programming (pun not intended) I sat down to correct my script to post the text file created as part of the REST POST body. Got all that working but while testing I deleted all the articles in my local “posts” folder and since there’s no recycle bin on Ubuntu server I lost the post forever. So here’s the summary:
I wrote about what I did yesterday which was got up, peed, went back to sleep, got up, smoked, showered, went out with wife to do some antiquing, bought some books and vinyls, stopped at Starbucks for another pee, found a bookstore and loaded up on even more, came home, read, napped, read, watched tv, blogged, went to sleep.
I’d share what books I bought and why, but maybe another time. In fact, I should make it a practice to write about what I’ve been reading if for no other reason than to make my time stairing at books more meaningful and maybe encourage you, dear reader, to ingest more on your own. Anyhow, I should hit the gym before it gets too late as I didn’t do so Friday or Saturday.
For those that are interested in seeing my code, here it is. This is a fairly straight forward Powershell script for writing a blog post and publishing it to a WordPress site. Enjoy.
Note:The code is formatted pretty ugly but this is the fault of the CSS on my new site–will get that worked out at some point as I’m sure I’ll be sharing more code in the future…
Clear-Host $headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]” $headers.Add(“Content-Type”, “application/json”) $headers.Add(“Authorization”, “Basic MY FUCKING SECRET WENT HERE!!!”) $Title = Read-Host “Enter a title” $PostFilename = “./posts/$($Title).txt” $MoveForward = $false if( Test-Path $PostFilename ) { Write-Output “`nThe filename $PostFilename already exists!`n” $Overwrite = Read-host “Overwrite/PublishAsIs? [O=Overwrite/P=PublishAsIs/[Enter]=No]” if( $Overwrite -eq “O” ) { Remove-Item $PostFilename -Force; $MoveForward = $true } if( $Overwrite -eq “P” ) { $MoveForward = $true } } else{ $MoveForward = $true } if( $MoveForward ){ if( $Overwrite -ne “P” ) { nano $PostFilename } if( -not ( Test-Path $PostFilename ) ){ $MoveForward = $false } } if( $MoveForward ){ Clear-host Write-Host “‘$Title’ is ready to post!” -ForegroundColor Green $Content = Get-Content $PostFilename $AvailableStates = @( “draft”, “pending”, “future”, “private”, “publish” ) do{ $Index = 0 Write-Host “`nAvailable States:`n” foreach( $State in $AvailableStates ){ $Index++ Write-Output “[$Index] – $State” } $StatusIndex = Read-Host “`nEnter status for the new post ([Enter]=draft)” if( [System.String]::IsNullOrEmpty( $StatusIndex ) ) { $StatusIndex = 1 } }while( [System.Int16]$StatusIndex -le 0 -or [System.Int16]$StatusIndex -gt $AvailableStates.Count ) $Status = $AvailableStates[($StatusIndex – 1)] $AllCategories = Invoke-RestMethod “http://mywebsite/wp-json/wp/v2/categories?per_page=100” -Method ‘GET’ -Headers $headers $SelectedCategories = @() do{ do{ $Index = 0 Write-Host “`nExisting Categories:`n” -ForegroundColor White foreach( $Cat in $AllCategories ){ $Index++ Write-Host “[$Index] – $($Cat.Name)” -ForegroundColor Gray } if( $SelectedCategories.Count -gt 0 ){ $SelectedDitty = “`nSo far you’ve selected: ” $SelectedDitty += $SelectedCategories | Foreach-Object { ‘”‘ + $_.Name.Replace( “&”, ‘&’) + ‘” ‘ } Write-Output $SelectedDitty } $CategoryIndex = Read-host “`nChoose a category number ([Enter] to continue)” if( [System.String]::IsNullOrEmpty( $CategoryIndex ) ){ $CategoryIndex = 0 } }while( [System.Int16]$CategoryIndex -lt 0 -or [System.Int16]$CategoryIndex -gt $AllCategories.count ) if( $CategoryIndex -eq 0 -and $SelectedCategories.Count -eq 0 ){ $SelectedCategories += $AllCategories | Where-Object { $_.Name -eq “Uncategorized” } } elseif( $CategoryIndex -ne 0 ){ $SelectedCategories += $AllCategories[($CategoryIndex – 1)] } }while ( $CategoryIndex -ne 0 ) $PublishingDitty = “`nPublishing ‘$Title’ w/ status ‘$Status’ in categories: ” $PublishingDitty += $SelectedCategories | Foreach-Object { ‘”‘ + $_.Name.Replace( “&”, ‘&’) + ‘” ‘ } Write-Output $PublishingDitty $PublicationSucceeded = $Null try{ $BodyJson = @{ “content” = $Content } | ConvertTo-Json $Categories = $SelectedCategories | ForEach-Object { $_.Id.ToString() + ‘,’ } $response = Invoke-RestMethod “http://mywebsite/wp-json/wp/v2/posts?title=$title&status=$Status&categories=$Categories” -Method ‘POST’ -Headers $headers -Body $BodyJson $PublicationSucceeded = $response } catch{ Write-Host “`nErrors encountered during publication: $_`n” -ForegroundColor Red } if( $PublicationSucceeded ) { Write-Host “`nPublication succeeded!`n” -ForegroundColor Green } else{ Write-Host “`nPublication failed!`n” -ForegroundColor Red } }