Skip to content

Commit 1a87d9a

Browse files
author
James Brundage
committed
feat: Get-OEmbed with progress bars ( Fixes #2 )
1 parent b28037d commit 1a87d9a

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

Commands/Get-oEmbed.ps1

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ function Get-OEmbed {
1010
Most social networks support oEmbed, so this little function lets you embed almost any social network post
1111
#>
1212
[Alias('oEmbed')]
13-
[CmdletBinding(PositionalBinding=$false,SupportsShouldProcess)]
13+
[CmdletBinding(PositionalBinding=$false,SupportsShouldProcess,DefaultParameterSetName='?url')]
1414
param(
1515
# The URL
16-
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName,ParameterSetName='?url')]
16+
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName='?url')]
1717
[Uri]
1818
$Url,
1919

@@ -37,10 +37,13 @@ function Get-OEmbed {
3737
[switch]
3838
$Force,
3939

40+
# The name of an oEmbed provider. Wildcards are supported.
4041
[Parameter(Mandatory,ParameterSetName='ProviderByName')]
42+
[SupportsWildcards()]
4143
[string]
4244
$ProviderName,
4345

46+
# If set, will list the oEmbed providers.
4447
[Parameter(Mandatory,ParameterSetName='ProviderList')]
4548
[switch]
4649
$ProviderList
@@ -59,6 +62,12 @@ function Get-OEmbed {
5962
if (-not $script:oEmbedUrlCache) {
6063
$script:oEmbedUrlCache = [Ordered]@{}
6164
}
65+
66+
if (-not $script:oEmbedDomainCache) {
67+
$script:oEmbedDomainCache = [Ordered]@{}
68+
}
69+
70+
$oEmbedQueue = [Collections.Queue]::new()
6271
}
6372

6473
process {
@@ -71,18 +80,30 @@ function Get-OEmbed {
7180
return $script:cachedOmbedProviders |
7281
# and return the name
7382
Where-Object Provider_Name -like $ProviderName
74-
}
75-
$topLevelDomain = $Url.DnsSafeHost.Split('.')[-2..-1] -join '.'
83+
}
7684
$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+
}
8296
}
97+
} else {
98+
$script:oEmbedDomainCache[$url.DnsSafeHost]
8399
}
100+
84101

85-
if (-not $matchingEndpoint) { return }
102+
if (-not $matchingEndpoint) {
103+
Write-Error "No oEmbed Provider found for URL '$url'"
104+
return
105+
}
106+
86107
$oEmbedUrl =
87108
"$($matchingEndpoint)?$(
88109
@(
@@ -98,13 +119,39 @@ function Get-OEmbed {
98119
}
99120
) -join '&'
100121
)"
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
106155
}
107-
$script:oEmbedUrlCache[$oEmbedUrl]
108-
109156
}
110157
}

0 commit comments

Comments
 (0)