-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-BloggerPost.ps1
More file actions
187 lines (153 loc) · 6.66 KB
/
Copy pathGet-BloggerPost.ps1
File metadata and controls
187 lines (153 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<#
.DESCRIPTION
Retrieves an individual post from a specified Blogger blog and optionally saves the content to a file as HTML or Markdown.
.PARAMETER BlogId
The ID of the blog to retrieve the post from. If not specified, uses the BlogId in the user preferences.
.PARAMETER PostId
The ID of the post to retrieve. This parameter is required.
.PARAMETER Format
The format of the post content to retrieve. Use either Markdown, JSON or HTML.
.PARAMETER FolderDateFormat
The folder name as expressed in a DateTime format string. For example, "YYYY/MM" which will save files
in a folder structure like "2023/10" based on the date of the post.
.PARAMETER OutDirectory
The directory where the HTML file will be saved. If not specified, uses the current directory.
.PARAMETER PassThru
If specified, the function will return the post object instead of just saving it to a file
.EXAMPLE
# obtain a post from the blog defined in the user preferences
$post = Get-BloggerPost -PostId "1234567890123456789"
.EXAMPLE
# obtain a post from a specified blog and save it as HTML in a specific directory
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format HTML -OutDirectory "C:\temp"
.EXAMPLE
# obtain a post from a specified blog and save it as Markdown in a specific directory with a date-based folder structure
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format Markdown -DateFormat "YYYY\\MM" -OutDirectory "C:\blogposts"
.EXAMPLE
# obtain a post from a specified blog and save it as JSON in the current directory
Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format JSON
.EXAMPLE
# obtain a post from a specified blog, write it to disk and return the post object
$post = Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format Markdown -PassThru
#>
Function Get-BloggerPost {
[CmdletBinding()]
param(
[Parameter(ParameterSetName = "Default")]
[Parameter(ParameterSetName = "Persist")]
[string]$BlogId,
[Parameter(ParameterSetName = "Default")]
[Parameter(ParameterSetName = "Persist")]
[string]$PostId,
[Parameter(Mandatory, ParameterSetName = "Persist")]
[ValidateSet("HTML", "Markdown", "JSON")]
[string]$Format,
[Parameter(ParameterSetName ="Persist")]
[string]$FolderDateFormat,
[Parameter(ParameterSetName = "Persist")]
[string]$OutDirectory = (Get-Location).Path,
[Parameter(ParameterSetName = "Persist")]
[switch]$PassThru
)
if ([string]::IsNullOrEmpty($PostId)) {
throw "PostId is required."
}
if (!$PSBoundParameters.ContainsKey("BlogId")) {
$BlogId = $BloggerSession.BlogId
if ([string]::IsNullOrEmpty($BlogId) -or $BlogId -eq 0) {
throw "BlogId not specified and no default BlogId found in settings."
}
}
try {
$uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts/$PostId"
$result = Invoke-GApi -uri $uri
if ($null -eq $result) {
throw "No post found with PostId '$PostId' in blog '$BlogId'."
}
Write-Verbose "Post: $($result | ConvertTo-Json -Depth 10)"
# Construct a subfolder based on the published date
if ($FolderDateFormat -and $result.published) {
$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
if (!(Test-Path -Path $OutDirectory)) {
try {
Write-Verbose "Creating output directory: $OutDirectory"
New-Item -ItemType Directory -Path $OutDirectory -Force | Out-Null
}
catch {
throw "Failed to create output directory '$OutDirectory': $($_.Exception.Message)"
}
}
Write-Verbose "Using output directory: $OutDirectory"
# Extract the HTML content
$htmlContent = $result.content
if ([string]::IsNullOrEmpty($htmlContent)) {
Write-Warning "Post '$PostId' has no content."
$htmlContent = ""
}
# Create the output file path
try {
switch ($Format) {
# Save the HTML content to a file
"HTML" {
$fileName = "$PostId.html"
$filePath = Join-Path -Path $OutDirectory -ChildPath $fileName
$htmlContent | Out-File -FilePath $filePath -Encoding UTF8
Write-Verbose "Post content saved to: $filePath"
}
# Save the Post to a Markdown file
"Markdown" {
$title = $result.title
$frontMatter = [ordered]@{
postId = $result.id
title = $result.title
}
if ($result.PSObject.Properties.Name -contains "labels") {
Write-Verbose "Using post labels: $($result.labels)"
$frontMatter['tags'] = $result.labels
} else {
Write-Verbose "No labels found in post, using empty tags."
$frontMatter['tags'] = @()
}
Write-Verbose "Saving frontmatter: $($frontMatter | ConvertTo-Json -Depth 10)"
$sanitizedTitle = $title -replace '[\\\/:\*\?"<>\|]', '_' -replace '\s+', ' ' -replace '^\s+|\s+$',''
$file = "$sanitizedTitle.md"
$filePath = Join-Path -Path $OutDirectory -ChildPath $file
ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath
Set-MarkdownFrontMatter -File $filePath -Replace $frontMatter
Write-Verbose "Post content saved to: $filePath"
}
"JSON" {
$fileName = "$PostId.json"
$filePath = Join-Path -Path $OutDirectory -ChildPath $fileName
$result | ConvertTo-Json | Out-File -FilePath $filePath -Encoding UTF8
Write-Verbose "Post content saved to: $filePath"
}
}
# Return the post object for further processing if needed
if (!($PSCmdlet.ParameterSetName -eq "Persist") -or ($PassThru.IsPresent -and $PassThru)) {
Write-Verbose "Returning blog post object"
return $result
}
}
catch {
throw "Failed to save post content: $($_.Exception.Message)"
}
}
catch {
# Handle specific HTTP errors
if ($_.Exception -like "*404*" -or $_.Exception -like "*Not Found*") {
throw "Post with PostId '$PostId' not found in blog '$BlogId'. Please verify the PostId and BlogId are correct."
}
elseif ($_.Exception -like "*403*" -or $_.Exception -like "*Forbidden*") {
throw "Access denied to blog '$BlogId' or post '$PostId'. Please verify your permissions."
}
else {
Write-Error $_.ToString() -ErrorAction Stop
}
}
}