|
| 1 | +function Get-OEmbed { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Gets oEmbed data for a given URL. |
| 5 | + .DESCRIPTION |
| 6 | + Gets oEmbed data for a given URL. |
| 7 | + |
| 8 | + [oEmbed](https://oembed.com/) is a format for allowing an embedded representation of a URL on third party sites. |
| 9 | +
|
| 10 | + Most social networks support oEmbed, so this little function lets you embed almost any social network post |
| 11 | + .EXAMPLE |
| 12 | + oEmbed -Url https://www.youtube.com/watch?v=UIR9Z_JdVhs |
| 13 | + #> |
| 14 | + [Alias('oEmbed')] |
| 15 | + [CmdletBinding(PositionalBinding=$false,SupportsShouldProcess,DefaultParameterSetName='Query')] |
| 16 | + param( |
| 17 | + # The URL |
| 18 | + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName='Query')] |
| 19 | + [Uri] |
| 20 | + $Url, |
| 21 | + |
| 22 | + # The maximum width of the returned image |
| 23 | + [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Query')] |
| 24 | + [int] |
| 25 | + $MaxWidth, |
| 26 | + |
| 27 | + # The maximum height of the returned image |
| 28 | + [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Query')] |
| 29 | + [int] |
| 30 | + $MaxHeight, |
| 31 | + |
| 32 | + # The format of the returned image |
| 33 | + [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Query')] |
| 34 | + [string] |
| 35 | + $Format, |
| 36 | + |
| 37 | + # Whether to force a refresh of the cached oEmbed data |
| 38 | + [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Query')] |
| 39 | + [switch] |
| 40 | + $Force, |
| 41 | + |
| 42 | + # The name of an oEmbed provider. Wildcards are supported. |
| 43 | + [Parameter(Mandatory,ParameterSetName='ProviderByName')] |
| 44 | + [SupportsWildcards()] |
| 45 | + [string] |
| 46 | + $ProviderName, |
| 47 | + |
| 48 | + # If set, will list the oEmbed providers. |
| 49 | + [Parameter(Mandatory,ParameterSetName='ProviderList')] |
| 50 | + [switch] |
| 51 | + $ProviderList |
| 52 | + ) |
| 53 | + |
| 54 | + begin { |
| 55 | + # If we haven't yet cached the list of oEmbed providers, do so now. |
| 56 | + if (-not $script:cachedOmbedProviders) { |
| 57 | + $script:cachedOmbedProviders = Invoke-RestMethod "https://oembed.com/providers.json" |
| 58 | + } |
| 59 | + # If we haven't yet cached the list of oEmbed endpoints, do so now. |
| 60 | + if (-not $script:openEmbeddings) { |
| 61 | + $script:openEmbeddings = $script:cachedOmbedProviders.Endpoints.Url -as [uri[]] |
| 62 | + } |
| 63 | + # Create a cache to store the oEmbed data in, if we haven't already done so. |
| 64 | + if (-not $script:oEmbedUrlCache) { |
| 65 | + $script:oEmbedUrlCache = [Ordered]@{} |
| 66 | + } |
| 67 | + |
| 68 | + if (-not $script:oEmbedDomainCache) { |
| 69 | + $script:oEmbedDomainCache = [Ordered]@{} |
| 70 | + } |
| 71 | + |
| 72 | + $oEmbedQueue = [Collections.Queue]::new() |
| 73 | + } |
| 74 | + |
| 75 | + process { |
| 76 | + if ($PSCmdlet.ParameterSetName -eq 'ProviderList') { |
| 77 | + return $script:cachedOmbedProviders |
| 78 | + } |
| 79 | + # If we're asking for a Provider by Name |
| 80 | + if ($PSCmdlet.ParameterSetName -eq 'ProviderByName') { |
| 81 | + # filter the list of providers |
| 82 | + return $script:cachedOmbedProviders | |
| 83 | + # and return the name |
| 84 | + Where-Object Provider_Name -like $ProviderName |
| 85 | + } |
| 86 | + $matchingEndpoint = |
| 87 | + if (-not $script:oEmbedDomainCache[$url.DnsSafeHost]) { |
| 88 | + :oEmbedProvider foreach ($oEmbedProvider in $script:cachedOmbedProviders) { |
| 89 | + foreach ($oEmbedEndpoint in $oEmbedProvider.Endpoints) { |
| 90 | + foreach ($oEmbedScheme in $oEmbedEndpoint.Schemes) { |
| 91 | + if ($url -like $oEmbedScheme) { |
| 92 | + $script:oEmbedDomainCache[$url.DnsSafeHost] = $oEmbedEndpoint.url |
| 93 | + $script:oEmbedDomainCache[$url.DnsSafeHost] |
| 94 | + break oEmbedProvider |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } else { |
| 100 | + $script:oEmbedDomainCache[$url.DnsSafeHost] |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + if (-not $matchingEndpoint) { |
| 105 | + Write-Error "No oEmbed Provider found for URL '$url'" |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + $oEmbedUrl = |
| 110 | + "$($matchingEndpoint)?$( |
| 111 | + @( |
| 112 | + "url=$([Web.HttpUtility]::UrlEncode($Url) -replace '\+','%20')" |
| 113 | + if ($MaxWidth) { |
| 114 | + "maxwidth=$MaxWidth" |
| 115 | + } |
| 116 | + if ($MaxHeight) { |
| 117 | + "maxheight=$MaxHeight" |
| 118 | + } |
| 119 | + if ($Format) { |
| 120 | + "format=$Format" |
| 121 | + } |
| 122 | + ) -join '&' |
| 123 | + )" |
| 124 | + |
| 125 | + $oEmbedQueue.Enqueue([Ordered]@{ |
| 126 | + Url = $Url |
| 127 | + oEmbedUrl = $oEmbedUrl |
| 128 | + }) |
| 129 | + } |
| 130 | + |
| 131 | + end { |
| 132 | + $counter = 0 |
| 133 | + $total = $oEmbedQueue.Count |
| 134 | + $progressId = $MyInvocation.HistoryId |
| 135 | + foreach ($queueItem in $oEmbedQueue) { |
| 136 | + $url = $queueItem.Url |
| 137 | + $oEmbedUrl = $queueItem.oEmbedUrl |
| 138 | + if ($oEmbedQueue.Count -gt 1) { |
| 139 | + $counter++ |
| 140 | + Write-Progress "oEmbed" "Retrieving oEmbed data for $url" -PercentComplete ( |
| 141 | + $counter * 100 / $total |
| 142 | + ) -Id $progressId |
| 143 | + } |
| 144 | + if (-not $script:oEmbedUrlCache[$oEmbedUrl] -or $Force) { |
| 145 | + if ($WhatIfPreference) { return $oEmbedUrl } |
| 146 | + if (-not $PSCmdlet.ShouldProcess($oEmbedUrl)) { |
| 147 | + Write-Verbose "Skipping $oEmbedUrl" |
| 148 | + continue |
| 149 | + } else { |
| 150 | + Write-Verbose "Retrieving $oEmbedUrl" |
| 151 | + } |
| 152 | + |
| 153 | + $script:oEmbedUrlCache[$oEmbedUrl] = Invoke-RestMethod -Uri $oEmbedUrl | |
| 154 | + Add-Member NoteProperty 'Url' $Url -Force -PassThru | |
| 155 | + Add-Member NoteProperty 'oEmbedUrl' $oEmbedUrl -Force -PassThru |
| 156 | + $script:oEmbedUrlCache[$oEmbedUrl].pstypenames.insert(0,'OpenEmbedding') |
| 157 | + } |
| 158 | + |
| 159 | + $script:oEmbedUrlCache[$oEmbedUrl] |
| 160 | + } |
| 161 | + |
| 162 | + if ($oEmbedQueue.Count -gt 1) { |
| 163 | + Write-Progress "oEmbed" "Retrieving oEmbed data" -Completed -Id $progressId |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments