Skip to content

Commit 5c83436

Browse files
authored
Get-BloggerPosts now includes -All parameter (#17)
* Get-BloggerPosts now includes -All parameter * updated readme * updated docs * fix for broken tests * Get-BloggerPosts now includes -Since date filter
1 parent 2bf12f7 commit 5c83436

6 files changed

Lines changed: 257 additions & 72 deletions

File tree

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,30 @@ When publishing, all images are converted to standard markdown format with Googl
120120
- Find-MarkdownImages: scans the markdown file to locate images in both standard and Obsidian formats
121121
- Update-MarkdownImages: updates the content in the markdown file with updated urls, converting all formats to standard markdown
122122

123-
## Future
124-
125-
- Download existing posts to your machine in markdown
123+
## Examples
124+
125+
## Download existing posts to your machine in markdown
126+
127+
```powershell
128+
### Initialize the auth settings for your google account
129+
Initialize-Blogger -ClientId <id> -ClientSecret <secret>
130+
131+
### Fetch the available blogs and set the first blog as the default
132+
$blogs = Get-BloggerBlogs
133+
$blogId = $blogs[0].id
134+
Set-BloggerConfig -Name BlogId $blogId
135+
136+
### Fetch posts
137+
$posts = Get-BloggerPosts -All
138+
139+
### Download Posts
140+
$total = $posts.count
141+
$count = 0
142+
foreach($post in $posts) {
143+
$complete = $count++/$total * 100
144+
Write-Progress -Activity "Downloading..." -PercentComplete $complete -Status "$complete% ($count of $total)"
145+
$post = Get-BloggerPost -PostId $post.id -Format Markdown -FolderDateFormat "yyyy\\MM" -OutDirectory ".\Posts"
146+
Write-Host "Downloaded: $($post.title) - $($post.published)"
147+
}
148+
Write-Progress -Activity "Downloading..." -Complete
149+
```

src/public/Get-BloggerPost.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ Function Get-BloggerPost {
7171

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

7980
# Ensure the output directory exists

src/public/Get-BloggerPosts.ps1

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,60 @@
77
88
.PARAMETER Status
99
The status of the posts to retrieve. Valid values are "draft", "live", and "scheduled"
10+
11+
.PARAMETER Since
12+
Filter the results to only return posts that are older than the supplied date.
13+
14+
.PARAMETER All
15+
Flag to indicate if all paginated results should be returned.
1016
#>
11-
Function Get-BloggerPosts
12-
{
13-
[CmdletBinding()]
14-
param(
15-
[string]$BlogId,
16-
17-
[ValidateSet("draft","live","scheduled")]
18-
[string]$Status = "live"
19-
)
20-
21-
if (!$PSBoundParameters.ContainsKey("BlogId"))
22-
{
23-
$BlogId = $BloggerSession.BlogId
24-
if (0 -eq $BlogId) {
25-
throw "BlogId not specified."
26-
}
17+
Function Get-BloggerPosts {
18+
[CmdletBinding()]
19+
param(
20+
[Parameter(Mandatory = $false)]
21+
[string]$BlogId,
22+
23+
[Parameter(Mandatory = $false)]
24+
[ValidateSet("draft", "live", "scheduled")]
25+
[string]$Status = "live",
26+
27+
[Parameter(Mandatory = $false)]
28+
[datetime]$Since,
29+
30+
[Parameter(Mandatory = $false)]
31+
[switch]$All
32+
)
33+
34+
if (!$PSBoundParameters.ContainsKey("BlogId")) {
35+
$BlogId = $BloggerSession.BlogId
36+
if (0 -eq $BlogId) {
37+
throw "BlogId not specified."
2738
}
39+
}
40+
$includeDateFilter = $PSBoundParameters.ContainsKey("Since")
2841

29-
try {
30-
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts?status=$status"
31-
32-
$result = Invoke-GApi -uri $uri
42+
try {
43+
$done = $false
44+
$pageToken = $null
45+
while (!$done) {
46+
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts?status=$status&fetchBodies=false"
47+
48+
if ($pageToken) {
49+
$uri += "&pageToken=$pageToken"
50+
}
51+
if ($includeDateFilter) {
52+
$uri += "&since=" + $Since.ToString("yyyy-MM-ddTHH:mm:ssZ")
53+
}
54+
$result = Invoke-GApi -uri $uri
3355

34-
$result.items
35-
}
36-
catch {
37-
Write-Error $_.ToString() -ErrorAction Stop
56+
$result.items
57+
58+
# loop if pageToken is present and -All switch is set
59+
$pageToken = $result.nextPageToken
60+
$done = ($All.IsPresent -and $All -and [string]::IsNullOrEmpty($pageToken)) -or !$All.IsPresent
3861
}
62+
}
63+
catch {
64+
Write-Error $_.ToString() -ErrorAction Stop
65+
}
3966
}

src/readme.md

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
- Setup the google access-token + refresh token, save it to disk
66

77
```
8-
Init-Blogger -clientId -clientSecret -redirectUri -code
8+
Initialize-Blogger -clientId -clientSecret -redirectUri -code
99
```
1010

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

13-
```
14-
Connect-Blogger
15-
```
16-
1713
We want configuration settings to store default values.
1814

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

2723
```
28-
Set-BloggerConfig
24+
Set-BloggerConfig -Name <parameter> -Value
2925
```
3026

3127

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

40-
- Get a list of blogger posts and save to disk
36+
- Get a list of blogger posts
4137

4238
```
43-
Get-BloggerPosts -blogid -directory
39+
Get-BloggerPosts -BlogId <blogid> -All
4440
```
4541

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

4844
```
49-
Publish-BloggerPost -blogid -content -title -draft
50-
Publish-BloggerPost -blogid -postid -title
45+
Publish-BloggerPost -blogid <blogid> -content <html> -title "First Post" -draft
46+
Publish-BloggerPost -blogid <blogid> -postid <postid> -content <html> -title "First Post"
5147
```
5248

5349
## Google Drive
5450

5551
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.
5652

57-
- Test if an image exists in GDrive
53+
- Get all items from Google Drive
54+
55+
```
56+
Get-GoogleDriveItems -ResultType All
57+
```
58+
59+
- Find if an image exists in Google Drive
60+
61+
```
62+
$folder = Get-GoogleDriveItems -ResultType Folders -Title "PSBlogger"
63+
Get-GoogleDriveItem -ResultType Files -Title image.jpg -Folder $folder.id
64+
```
65+
66+
- Upload an image to Google Drive
5867

5968
```
60-
Test-GDriveImage
69+
Add-GoogleDriveFile -FilePath "c:\image.jpg" -TargetFolderName "PSBlogger"
6170
```
6271

63-
- Upload an image to GDrive
72+
- Make an image publicly accessible to the internet
6473

6574
```
66-
Send-GDriveImage -file
75+
$folder = Get-GoogleDriveItems -ResultType Folders -Title "PSBlogger"
76+
$file = Get-GoogleDriveItem -ResultType Files -Title image.jpg -Folder $folder.id
77+
$permission = New-GoogleDriveFilePermission -role "reader" -type "anyone"
78+
Set-GoogleDriveFilePermission -FileId $file.id -PermissionData $permission
6779
```
6880

6981
## Markdown
7082

71-
- Related to finding images in the markdown and uploading, we want:
83+
- Related to finding images in the markdown, we want:
7284

73-
```
74-
Get-MarkdownLinks -file
75-
```
85+
```
86+
$imageMappings = Find-MarkdownImages -File .\file.md
87+
```
7688

77-
and
89+
- After uploading these images, we update the mappings and then update the file:
7890

79-
```
80-
Update-MarkdownLink -file -path -url
81-
```
91+
```
92+
Update-MarkdownImages -File .\file.md -ImageMappings $imageMappings
93+
```
8294

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

8597
```
86-
Show-MarkdownFrontMatter
98+
Get-MarkdownFrontMatter -File .\file.md
99+
```
100+
101+
- And the ability to update the meta-data
102+
87103
```
104+
Set-MarkdownFrontMatter -file .\file.md -Update [ordered]@{ postid = "123" }
88105
89106
## Pandoc
90107
@@ -96,39 +113,30 @@ The conversion of markdown to HTML will use pandoc with some custom extensions t
96113
ConvertTo-HtmlFromMarkdown -file something.md
97114
```
98115
116+
...which is equivalent to:
117+
99118
```
100119
pandoc test.md -f markdown -t html -o test.html
101120
```
102121
103122
- Upload the images in the markdown to Google Drive. This involves:
104123
105-
- Find all the images in the markdown that need to be uploaded
106-
- Upload the files in the markdown to google drive
107-
- Update the markdown with the values
108-
109-
```
110-
Publish-GDriveImages -file something.md
111-
```
112-
113-
- Update the local files for a post if they've been updated. This involves:
114-
115-
- find all the referenced images in the markdown
116-
- get the modified date from the local file
117-
- get the modified date from google drive
118-
- upload newer files to google drive
124+
- Find all the images in the markdown that need to be uploaded `Find-MarkdownImages`
125+
- Upload the files in the markdown to google drive `Add-GoogleDriveFile`
126+
- Update the markdown with the values `Update-MarkdownIamges`
119127
120128
```
121-
Update-GDriveImages -file something.md
129+
Publish-MarkdownDriveImages -file something.md
122130
```
123131
124132
- The "money shot" is performing the work of converting the markdown and publishing to blogger. Assuming this involves:
125133
126-
- Publish-GDriveImages
127-
- Update-GDriveImages
134+
- Publish-MarkdownDriveImages
128135
- ConvertTo-HtmlFromMarkDown
129136
- Get-MarkdownFrontMatter
130137
- Publish-BloggerPost
138+
- Set-MarkdownFrontMatter
131139
132140
```
133-
Publish-MarkdownToBlog -file post.md
141+
Publish-MarkdownBloggerPost -file post.md
134142
```

src/tests/Get-BloggerPost.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Describe "Get-BloggerPost" {
182182
return @{
183183
id = $postId
184184
title = "Test Post"
185-
published = "2023-10-01T12:00:00Z"
185+
published = "10/01/2023 12:00:00"
186186
content = "<h1>Hello World</h1><p>This is a post.</p>"
187187
}
188188
}
@@ -225,7 +225,7 @@ Describe "Get-BloggerPost" {
225225
return @{
226226
id = $postId
227227
title = "Test Post"
228-
published = "2023-10-01T12:00:00Z"
228+
published = [datetime]"10/01/2023 12:00:00"
229229
content = "<h1>Hello World</h1><p>This is a post.</p>"
230230
}
231231
}

0 commit comments

Comments
 (0)