@@ -10,10 +10,10 @@ function Get-OEmbed {
10
10
Most social networks support oEmbed, so this little function lets you embed almost any social network post
11
11
#>
12
12
[Alias (' oEmbed' )]
13
- [CmdletBinding (PositionalBinding = $false , SupportsShouldProcess )]
13
+ [CmdletBinding (PositionalBinding = $false , SupportsShouldProcess , DefaultParameterSetName = ' ?url ' )]
14
14
param (
15
15
# The URL
16
- [Parameter (Mandatory , Position = 0 , ValueFromPipelineByPropertyName , ParameterSetName = ' ?url' )]
16
+ [Parameter (Mandatory , Position = 0 , ValueFromPipeline , ValueFromPipelineByPropertyName , ParameterSetName = ' ?url' )]
17
17
[Uri ]
18
18
$Url ,
19
19
@@ -37,10 +37,13 @@ function Get-OEmbed {
37
37
[switch ]
38
38
$Force ,
39
39
40
+ # The name of an oEmbed provider. Wildcards are supported.
40
41
[Parameter (Mandatory , ParameterSetName = ' ProviderByName' )]
42
+ [SupportsWildcards ()]
41
43
[string ]
42
44
$ProviderName ,
43
45
46
+ # If set, will list the oEmbed providers.
44
47
[Parameter (Mandatory , ParameterSetName = ' ProviderList' )]
45
48
[switch ]
46
49
$ProviderList
@@ -59,6 +62,12 @@ function Get-OEmbed {
59
62
if (-not $script :oEmbedUrlCache ) {
60
63
$script :oEmbedUrlCache = [Ordered ]@ {}
61
64
}
65
+
66
+ if (-not $script :oEmbedDomainCache ) {
67
+ $script :oEmbedDomainCache = [Ordered ]@ {}
68
+ }
69
+
70
+ $oEmbedQueue = [Collections.Queue ]::new()
62
71
}
63
72
64
73
process {
@@ -71,18 +80,30 @@ function Get-OEmbed {
71
80
return $script :cachedOmbedProviders |
72
81
# and return the name
73
82
Where-Object Provider_Name -like $ProviderName
74
- }
75
- $topLevelDomain = $Url.DnsSafeHost.Split (' .' )[-2 .. -1 ] -join ' .'
83
+ }
76
84
$matchingEndpoint =
77
- foreach ($endpoint in $script :openEmbeddings ) {
78
- if ($endpoint.DnsSafeHost -eq $topLevelDomain -or
79
- $endpoint.DnsSafeHost -like " *.$topLevelDomain " ) {
80
- $endpoint
81
- break
85
+ if (-not $script :oEmbedDomainCache [$url.DnsSafeHost ]) {
86
+ :oEmbedProvider foreach ($oEmbedProvider in $script :cachedOmbedProviders ) {
87
+ foreach ($oEmbedEndpoint in $oEmbedProvider.Endpoints ) {
88
+ foreach ($oEmbedScheme in $oEmbedEndpoint.Schemes ) {
89
+ if ($url -like $oEmbedScheme ) {
90
+ $script :oEmbedDomainCache [$url.DnsSafeHost ] = $oEmbedEndpoint.url
91
+ $script :oEmbedDomainCache [$url.DnsSafeHost ]
92
+ break oEmbedProvider
93
+ }
94
+ }
95
+ }
82
96
}
97
+ } else {
98
+ $script :oEmbedDomainCache [$url.DnsSafeHost ]
83
99
}
100
+
84
101
85
- if (-not $matchingEndpoint ) { return }
102
+ if (-not $matchingEndpoint ) {
103
+ Write-Error " No oEmbed Provider found for URL '$url '"
104
+ return
105
+ }
106
+
86
107
$oEmbedUrl =
87
108
" $ ( $matchingEndpoint ) ?$ (
88
109
@ (
@@ -98,13 +119,39 @@ function Get-OEmbed {
98
119
}
99
120
) -join ' &'
100
121
) "
101
- if (-not $script :oEmbedUrlCache [$oEmbedUrl ] -or $Force ) {
102
- $script :oEmbedUrlCache [$oEmbedUrl ] = Invoke-RestMethod - Uri $oEmbedUrl |
103
- Add-Member NoteProperty ' Url' $Url - Force - PassThru |
104
- Add-Member NoteProperty ' oEmbedUrl' $oEmbedUrl - Force - PassThru
105
- $script :oEmbedUrlCache [$oEmbedUrl ].pstypenames.insert(0 , ' OpenEmbedding' )
122
+
123
+ $oEmbedQueue.Enqueue ([Ordered ]@ {
124
+ Url = $Url
125
+ oEmbedUrl = $oEmbedUrl
126
+ })
127
+ }
128
+
129
+ end {
130
+ $counter = 0
131
+ $total = $oEmbedQueue.Count
132
+ $progressId = $MyInvocation.HistoryId
133
+ foreach ($queueItem in $oEmbedQueue ) {
134
+ $url = $queueItem.Url
135
+ $oEmbedUrl = $queueItem.oEmbedUrl
136
+ if ($oEmbedQueue.Count -gt 1 ) {
137
+ $counter ++
138
+ Write-Progress " oEmbed" " Retrieving oEmbed data for $url " - PercentComplete (
139
+ $counter * 100 / $total
140
+ ) - Id $progressId
141
+ }
142
+ if (-not $script :oEmbedUrlCache [$oEmbedUrl ] -or $Force ) {
143
+ $script :oEmbedUrlCache [$oEmbedUrl ] = Invoke-RestMethod - Uri $oEmbedUrl |
144
+ Add-Member NoteProperty ' Url' $Url - Force - PassThru |
145
+ Add-Member NoteProperty ' oEmbedUrl' $oEmbedUrl - Force - PassThru
146
+ $script :oEmbedUrlCache [$oEmbedUrl ].pstypenames.insert(0 , ' OpenEmbedding' )
147
+ }
148
+
149
+
150
+ $script :oEmbedUrlCache [$oEmbedUrl ]
151
+ }
152
+
153
+ if ($oEmbedQueue.Count -gt 1 ) {
154
+ Write-Progress " oEmbed" " Retrieving oEmbed data" - Completed - Id $progressId
106
155
}
107
- $script :oEmbedUrlCache [$oEmbedUrl ]
108
-
109
156
}
110
157
}
0 commit comments