|
| 1 | +<# |
| 2 | +.DESCRIPTION |
| 3 | + Retrieves an individual post from a specified Blogger blog and optionally saves the content to a file as HTML or Markdown. |
| 4 | +
|
| 5 | +.PARAMETER BlogId |
| 6 | + The ID of the blog to retrieve the post from. If not specified, uses the BlogId in the user preferences. |
| 7 | +
|
| 8 | +.PARAMETER PostId |
| 9 | + The ID of the post to retrieve. This parameter is required. |
| 10 | +
|
| 11 | +.PARAMETER Format |
| 12 | + The format of the post content to retrieve. Use either Markdown or HTML. |
| 13 | +
|
| 14 | +.PARAMETER FolderDateFormat |
| 15 | + The folder name as expressed in a DateTime format string. For example, "YYYY/MM" which will save files |
| 16 | + in a folder structure like "2023/10" based on the date of the post. |
| 17 | +
|
| 18 | +.PARAMETER OutDirectory |
| 19 | + The directory where the HTML file will be saved. If not specified, uses the current directory. |
| 20 | +
|
| 21 | +.EXAMPLE |
| 22 | + Get-BloggerPost -PostId "1234567890123456789" |
| 23 | +
|
| 24 | +.EXAMPLE |
| 25 | + Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format HTML -OutDirectory "C:\temp" |
| 26 | +
|
| 27 | +.EXAMPLE |
| 28 | + Get-BloggerPost -BlogId "9876543210987654321" -PostId "1234567890123456789" -Format Markdown -DateFormat "YYYY\\MM" -OutDirectory "C:\blogposts" |
| 29 | +#> |
| 30 | +Function Get-BloggerPost { |
| 31 | + [CmdletBinding()] |
| 32 | + param( |
| 33 | + [Parameter(ParameterSetName = "Default")] |
| 34 | + [Parameter(ParameterSetName = "Persist")] |
| 35 | + [string]$BlogId, |
| 36 | + |
| 37 | + [Parameter(Mandatory,ParameterSetName = "Default")] |
| 38 | + [Parameter(Mandatory,ParameterSetName = "Persist")] |
| 39 | + [string]$PostId, |
| 40 | + |
| 41 | + [Parameter(Mandatory, ParameterSetName = "Persist")] |
| 42 | + [ValidateSet("HTML", "Markdown")] |
| 43 | + [string]$Format, |
| 44 | + |
| 45 | + [Parameter(ParameterSetName ="Persist")] |
| 46 | + [string]$FolderDateFormat, |
| 47 | + |
| 48 | + [Parameter(ParameterSetName = "Persist")] |
| 49 | + [string]$OutDirectory = (Get-Location).Path |
| 50 | + ) |
| 51 | + |
| 52 | + if (!$PSBoundParameters.ContainsKey("BlogId")) { |
| 53 | + $BlogId = $BloggerSession.BlogId |
| 54 | + if ([string]::IsNullOrEmpty($BlogId) -or $BlogId -eq 0) { |
| 55 | + throw "BlogId not specified and no default BlogId found in settings." |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if ([string]::IsNullOrEmpty($PostId)) { |
| 60 | + throw "PostId is required." |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + $uri = "https://www.googleapis.com/blogger/v3/blogs/$BlogId/posts/$PostId" |
| 65 | + |
| 66 | + $result = Invoke-GApi -uri $uri |
| 67 | + |
| 68 | + if ($null -eq $result) { |
| 69 | + throw "No post found with PostId '$PostId' in blog '$BlogId'." |
| 70 | + } |
| 71 | + |
| 72 | + # Construct a subfolder based on the published date |
| 73 | + if ($FolderDateFormat -and $result.published) { |
| 74 | + $date = [datetime]::Parse($result.published) |
| 75 | + $formattedDate = $date.ToString($FolderDateFormat) |
| 76 | + $OutDirectory = Join-Path -Path $OutDirectory -ChildPath $formattedDate |
| 77 | + } |
| 78 | + |
| 79 | + # Ensure the output directory exists |
| 80 | + if (!(Test-Path -Path $OutDirectory)) { |
| 81 | + try { |
| 82 | + New-Item -ItemType Directory -Path $OutDirectory -Force | Out-Null |
| 83 | + } |
| 84 | + catch { |
| 85 | + throw "Failed to create output directory '$OutDirectory': $($_.Exception.Message)" |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + # Extract the HTML content |
| 90 | + $htmlContent = $result.content |
| 91 | + |
| 92 | + if ([string]::IsNullOrEmpty($htmlContent)) { |
| 93 | + Write-Warning "Post '$PostId' has no content." |
| 94 | + $htmlContent = "" |
| 95 | + } |
| 96 | + |
| 97 | + # Create the output file path |
| 98 | + try { |
| 99 | + |
| 100 | + switch ($Format) { |
| 101 | + |
| 102 | + # Save the HTML content to a file |
| 103 | + "HTML" { |
| 104 | + |
| 105 | + $fileName = "$PostId.html" |
| 106 | + $filePath = Join-Path -Path $OutDirectory -ChildPath $fileName |
| 107 | + $htmlContent | Out-File -FilePath $filePath -Encoding UTF8 |
| 108 | + Write-Verbose "Post content saved to: $filePath" |
| 109 | + } |
| 110 | + |
| 111 | + # Save the Post to a Markdown file |
| 112 | + "Markdown" { |
| 113 | + |
| 114 | + $title = $result.title |
| 115 | + $frontMatter = [ordered]@{ |
| 116 | + postId = $result.id |
| 117 | + } |
| 118 | + $file = "$title.md" |
| 119 | + $filePath = Join-Path -Path $OutDirectory -ChildPath $file |
| 120 | + ConvertTo-MarkdownFromHtml -Content $result.content -OutFile $filePath > $null |
| 121 | + Set-MarkdownFrontMatter -File $filePath -Replace $frontMatter |
| 122 | + Write-Verbose "Post content saved to: $filePath" |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + # Return the post object for further processing if needed |
| 127 | + return $result |
| 128 | + } |
| 129 | + catch { |
| 130 | + throw "Failed to save post content to file '$filePath': $($_.Exception.Message)" |
| 131 | + } |
| 132 | + } |
| 133 | + catch { |
| 134 | + # Handle specific HTTP errors |
| 135 | + if ($_.Exception -like "*404*" -or $_.Exception -like "*Not Found*") { |
| 136 | + throw "Post with PostId '$PostId' not found in blog '$BlogId'. Please verify the PostId and BlogId are correct." |
| 137 | + } |
| 138 | + elseif ($_.Exception -like "*403*" -or $_.Exception -like "*Forbidden*") { |
| 139 | + throw "Access denied to blog '$BlogId' or post '$PostId'. Please verify your permissions." |
| 140 | + } |
| 141 | + else { |
| 142 | + Write-Error $_.ToString() -ErrorAction Stop |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments