Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,30 @@ When publishing, all images are converted to standard markdown format with Googl
- Find-MarkdownImages: scans the markdown file to locate images in both standard and Obsidian formats
- Update-MarkdownImages: updates the content in the markdown file with updated urls, converting all formats to standard markdown

## Future

- Download existing posts to your machine in markdown
## Examples

## Download existing posts to your machine in markdown

```powershell
### Initialize the auth settings for your google account
Initialize-Blogger -ClientId <id> -ClientSecret <secret>

### Fetch the available blogs and set the first blog as the default
$blogs = Get-BloggerBlogs
$blogId = $blogs[0].id
Set-BloggerConfig -Name BlogId $blogId

### Fetch posts
$posts = Get-BloggerPosts -All

### Download Posts
$total = $posts.count
$count = 0
foreach($post in $posts) {
$complete = $count++/$total * 100
Write-Progress -Activity "Downloading..." -PercentComplete $complete -Status "$complete% ($count of $total)"
$post = Get-BloggerPost -PostId $post.id -Format Markdown -FolderDateFormat "yyyy\\MM" -OutDirectory ".\Posts"
Write-Host "Downloaded: $($post.title) - $($post.published)"
}
Write-Progress -Activity "Downloading..." -Complete
```
5 changes: 3 additions & 2 deletions src/public/Get-BloggerPost.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ Function Get-BloggerPost {

# Construct a subfolder based on the published date
if ($FolderDateFormat -and $result.published) {
$date = [datetime]::Parse($result.published)
$formattedDate = $date.ToString($FolderDateFormat)
$formattedDate = $result.published.ToString($FolderDateFormat)
Write-Verbose "Using published date '$formattedDate' for folder structure."
$OutDirectory = Join-Path -Path $OutDirectory -ChildPath $formattedDate
Write-Verbose "Output directory set to: $OutDirectory"
}

# Ensure the output directory exists
Expand Down
75 changes: 51 additions & 24 deletions src/public/Get-BloggerPosts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,60 @@

.PARAMETER Status
The status of the posts to retrieve. Valid values are "draft", "live", and "scheduled"

.PARAMETER Since
Filter the results to only return posts that are older than the supplied date.

.PARAMETER All
Flag to indicate if all paginated results should be returned.
#>
Function Get-BloggerPosts
{
[CmdletBinding()]
param(
[string]$BlogId,

[ValidateSet("draft","live","scheduled")]
[string]$Status = "live"
)

if (!$PSBoundParameters.ContainsKey("BlogId"))
{
$BlogId = $BloggerSession.BlogId
if (0 -eq $BlogId) {
throw "BlogId not specified."
}
Function Get-BloggerPosts {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$BlogId,

[Parameter(Mandatory = $false)]
[ValidateSet("draft", "live", "scheduled")]
[string]$Status = "live",

[Parameter(Mandatory = $false)]
[datetime]$Since,

[Parameter(Mandatory = $false)]
[switch]$All
)

if (!$PSBoundParameters.ContainsKey("BlogId")) {
$BlogId = $BloggerSession.BlogId
if (0 -eq $BlogId) {
throw "BlogId not specified."
}
}
$includeDateFilter = $PSBoundParameters.ContainsKey("Since")

try {
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts?status=$status"

$result = Invoke-GApi -uri $uri
try {
$done = $false
$pageToken = $null
while (!$done) {
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts?status=$status&fetchBodies=false"

if ($pageToken) {
$uri += "&pageToken=$pageToken"
}
if ($includeDateFilter) {
$uri += "&since=" + $Since.ToString("yyyy-MM-ddTHH:mm:ssZ")
}
$result = Invoke-GApi -uri $uri

$result.items
}
catch {
Write-Error $_.ToString() -ErrorAction Stop
$result.items

# loop if pageToken is present and -All switch is set
$pageToken = $result.nextPageToken
$done = ($All.IsPresent -and $All -and [string]::IsNullOrEmpty($pageToken)) -or !$All.IsPresent
}
}
catch {
Write-Error $_.ToString() -ErrorAction Stop
}
}
90 changes: 49 additions & 41 deletions src/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
- Setup the google access-token + refresh token, save it to disk

```
Init-Blogger -clientId -clientSecret -redirectUri -code
Initialize-Blogger -clientId -clientSecret -redirectUri -code
```

And if we could host a weblistener, we could launch the browser and wait for the auth-flow

```
Connect-Blogger
```

We want configuration settings to store default values.

- Get configuration settings. This returns an object with settings to use.
Expand All @@ -25,7 +21,7 @@ We want configuration settings to store default values.
- Set Configuration settings. You can pass individual settings. Set to "" to erase

```
Set-BloggerConfig
Set-BloggerConfig -Name <parameter> -Value
```


Expand All @@ -37,54 +33,75 @@ We want configuration settings to store default values.
Get-BloggerBlogs
```

- Get a list of blogger posts and save to disk
- Get a list of blogger posts

```
Get-BloggerPosts -blogid -directory
Get-BloggerPosts -BlogId <blogid> -All
```

- Publish to Blogger. We also need to consider 'publishing' or scheduling a publish

```
Publish-BloggerPost -blogid -content -title -draft
Publish-BloggerPost -blogid -postid -title
Publish-BloggerPost -blogid <blogid> -content <html> -title "First Post" -draft
Publish-BloggerPost -blogid <blogid> -postid <postid> -content <html> -title "First Post"
```

## Google Drive

We need the ability to upload images to google drive. The upload process would read the images from the markdown and if the backing URL isn't Google Drive, it uploads to google and then updates the markdown. You should be able to publish the images independently of the blog.

- Test if an image exists in GDrive
- Get all items from Google Drive

```
Get-GoogleDriveItems -ResultType All
```

- Find if an image exists in Google Drive

```
$folder = Get-GoogleDriveItems -ResultType Folders -Title "PSBlogger"
Get-GoogleDriveItem -ResultType Files -Title image.jpg -Folder $folder.id
```

- Upload an image to Google Drive

```
Test-GDriveImage
Add-GoogleDriveFile -FilePath "c:\image.jpg" -TargetFolderName "PSBlogger"
```

- Upload an image to GDrive
- Make an image publicly accessible to the internet

```
Send-GDriveImage -file
$folder = Get-GoogleDriveItems -ResultType Folders -Title "PSBlogger"
$file = Get-GoogleDriveItem -ResultType Files -Title image.jpg -Folder $folder.id
$permission = New-GoogleDriveFilePermission -role "reader" -type "anyone"
Set-GoogleDriveFilePermission -FileId $file.id -PermissionData $permission
```

## Markdown

- Related to finding images in the markdown and uploading, we want:
- Related to finding images in the markdown, we want:

```
Get-MarkdownLinks -file
```
```
$imageMappings = Find-MarkdownImages -File .\file.md
```

and
- After uploading these images, we update the mappings and then update the file:

```
Update-MarkdownLink -file -path -url
```
```
Update-MarkdownImages -File .\file.md -ImageMappings $imageMappings
```

- We'll also need to get the meta-data from the markdown

```
Show-MarkdownFrontMatter
Get-MarkdownFrontMatter -File .\file.md
```

- And the ability to update the meta-data

```
Set-MarkdownFrontMatter -file .\file.md -Update [ordered]@{ postid = "123" }

## Pandoc

Expand All @@ -96,39 +113,30 @@ The conversion of markdown to HTML will use pandoc with some custom extensions t
ConvertTo-HtmlFromMarkdown -file something.md
```

...which is equivalent to:

```
pandoc test.md -f markdown -t html -o test.html
```

- Upload the images in the markdown to Google Drive. This involves:

- Find all the images in the markdown that need to be uploaded
- Upload the files in the markdown to google drive
- Update the markdown with the values

```
Publish-GDriveImages -file something.md
```

- Update the local files for a post if they've been updated. This involves:

- find all the referenced images in the markdown
- get the modified date from the local file
- get the modified date from google drive
- upload newer files to google drive
- Find all the images in the markdown that need to be uploaded `Find-MarkdownImages`
- Upload the files in the markdown to google drive `Add-GoogleDriveFile`
- Update the markdown with the values `Update-MarkdownIamges`

```
Update-GDriveImages -file something.md
Publish-MarkdownDriveImages -file something.md
```

- The "money shot" is performing the work of converting the markdown and publishing to blogger. Assuming this involves:

- Publish-GDriveImages
- Update-GDriveImages
- Publish-MarkdownDriveImages
- ConvertTo-HtmlFromMarkDown
- Get-MarkdownFrontMatter
- Publish-BloggerPost
- Set-MarkdownFrontMatter

```
Publish-MarkdownToBlog -file post.md
Publish-MarkdownBloggerPost -file post.md
```
4 changes: 2 additions & 2 deletions src/tests/Get-BloggerPost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Describe "Get-BloggerPost" {
return @{
id = $postId
title = "Test Post"
published = "2023-10-01T12:00:00Z"
published = "10/01/2023 12:00:00"
content = "<h1>Hello World</h1><p>This is a post.</p>"
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ Describe "Get-BloggerPost" {
return @{
id = $postId
title = "Test Post"
published = "2023-10-01T12:00:00Z"
published = [datetime]"10/01/2023 12:00:00"
content = "<h1>Hello World</h1><p>This is a post.</p>"
}
}
Expand Down
Loading