|
| 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 | + #> |
| 12 | + [Alias('oEmbed')] |
| 13 | + [CmdletBinding(PositionalBinding=$false,SupportsShouldProcess)] |
| 14 | + param( |
| 15 | + # The URL |
| 16 | + [Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName,ParameterSetName='?url')] |
| 17 | + [Uri] |
| 18 | + $Url, |
| 19 | + |
| 20 | + # The maximum width of the returned image |
| 21 | + [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='?url')] |
| 22 | + [int] |
| 23 | + $MaxWidth, |
| 24 | + |
| 25 | + # The maximum height of the returned image |
| 26 | + [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='?url')] |
| 27 | + [int] |
| 28 | + $MaxHeight, |
| 29 | + |
| 30 | + # The format of the returned image |
| 31 | + [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='?url')] |
| 32 | + [string] |
| 33 | + $Format, |
| 34 | + |
| 35 | + # Whether to force a refresh of the cached oEmbed data |
| 36 | + [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='?url')] |
| 37 | + [switch] |
| 38 | + $Force, |
| 39 | + |
| 40 | + [Parameter(Mandatory,ParameterSetName='ProviderByName')] |
| 41 | + [string] |
| 42 | + $ProviderName |
| 43 | + ) |
| 44 | + |
| 45 | + begin { |
| 46 | + # If we haven't yet cached the list of oEmbed providers, do so now. |
| 47 | + if (-not $script:cachedOmbedProviders) { |
| 48 | + $script:cachedOmbedProviders = Invoke-RestMethod "https://oembed.com/providers.json" |
| 49 | + } |
| 50 | + # If we haven't yet cached the list of oEmbed endpoints, do so now. |
| 51 | + if (-not $script:openEmbeddings) { |
| 52 | + $script:openEmbeddings = $script:cachedOmbedProviders.Endpoints.Url -as [uri[]] |
| 53 | + } |
| 54 | + # Create a cache to store the oEmbed data in, if we haven't already done so. |
| 55 | + if (-not $script:oEmbedUrlCache) { |
| 56 | + $script:oEmbedUrlCache = [Ordered]@{} |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + process { |
| 61 | + # If we're asking for a Provider by Name |
| 62 | + if ($PSCmdlet.ParameterSetName -eq 'ProviderByName') { |
| 63 | + # filter the list of providers |
| 64 | + return $script:cachedOmbedProviders | |
| 65 | + # and return the name |
| 66 | + Where-Object Provider_Name -like $ProviderName |
| 67 | + } |
| 68 | + $topLevelDomain = $Url.DnsSafeHost.Split('.')[-2..-1] -join '.' |
| 69 | + $matchingEndpoint = |
| 70 | + foreach ($endpoint in $script:openEmbeddings) { |
| 71 | + if ($endpoint.DnsSafeHost -eq $topLevelDomain -or |
| 72 | + $endpoint.DnsSafeHost -like "*.$topLevelDomain") { |
| 73 | + $endpoint |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (-not $matchingEndpoint) { return } |
| 79 | + $oEmbedUrl = |
| 80 | + "$($matchingEndpoint)?$( |
| 81 | + @( |
| 82 | + "url=$([Web.HttpUtility]::UrlEncode($Url) -replace '\+','%20')" |
| 83 | + if ($MaxWidth) { |
| 84 | + "maxwidth=$MaxWidth" |
| 85 | + } |
| 86 | + if ($MaxHeight) { |
| 87 | + "maxheight=$MaxHeight" |
| 88 | + } |
| 89 | + if ($Format) { |
| 90 | + "format=$Format" |
| 91 | + } |
| 92 | + ) -join '&' |
| 93 | + )" |
| 94 | + if (-not $script:oEmbedUrlCache[$oEmbedUrl] -or $Force) { |
| 95 | + $script:oEmbedUrlCache[$oEmbedUrl] = Invoke-RestMethod -Uri $oEmbedUrl | |
| 96 | + Add-Member NoteProperty 'Url' $Url -Force -PassThru | |
| 97 | + Add-Member NoteProperty 'oEmbedUrl' $oEmbedUrl -Force -PassThru |
| 98 | + $script:oEmbedUrlCache[$oEmbedUrl].pstypenames.insert(0,'OpenEmbedding') |
| 99 | + } |
| 100 | + $script:oEmbedUrlCache[$oEmbedUrl] |
| 101 | + |
| 102 | + } |
| 103 | +} |
0 commit comments