@@ -17,45 +17,58 @@ function Get-OpenGraph
17
17
'https://cnn.com/',
18
18
'https://msnbc.com/',
19
19
'https://fox.com/' |
20
- Get-OpenGraph
20
+ Get-OpenGraph
21
21
#>
22
22
[Alias (' openGraph' , ' ogp' )]
23
- param (
23
+ [CmdletBinding (PositionalBinding = $false )]
24
+ param (
24
25
# The URL that may contain Open Graph metadata
25
- [Parameter (ValueFromPipeline , ValueFromPipelineByPropertyName )]
26
+ [Parameter (ValueFromPipelineByPropertyName )]
26
27
[Uri ]
27
28
$Url ,
28
29
29
30
# A dictionary of additional Open Graph metadata to include in the result
30
31
[Parameter (ValueFromPipelineByPropertyName )]
31
32
[Collections.IDictionary ]
32
- $Data
33
+ $Data ,
34
+
35
+ # If set, forces the function to retrieve the Open Graph metadata even if it is already cached.
36
+ [Parameter (ValueFromPipelineByPropertyName )]
37
+ [switch ]
38
+ $Force
33
39
)
34
40
35
41
begin {
36
42
# Make a regex to match meta tags
37
43
$metaRegex = [Regex ]::new(' <meta.+?/>' , ' IgnoreCase' , ' 00:00:00.1' )
44
+ if (-not $script :OpenGraphCache ) {
45
+ $script :OpenGraphCache = [Ordered ]@ {}
46
+ }
38
47
}
39
48
40
49
process {
41
50
# Declare an empty object to hold the Open Graph metadata
42
51
$openGraphMetadata = [Ordered ]@ {PSTypeName = ' OpenGraph' }
43
52
if ($Url ) {
53
+ if ($script :OpenGraphCache [$url ] -and -not $Force ) {
54
+ return $script :OpenGraphCache [$url ]
55
+ }
44
56
$restResponse = Invoke-RestMethod - Uri $Url
45
57
foreach ($match in $metaRegex.Matches (" $restResponse " )) {
46
58
$matchXml = " $match " -as [xml ]
47
59
if ($matchXml.meta.property -and $matchXml.meta.content ) {
48
60
$openGraphMetadata [$matchXml.meta.property ] = $matchXml.meta.content
49
61
}
50
62
}
63
+ $script :OpenGraphCache [$url ] = $openGraphMetadata
51
64
}
52
65
if ($Data ) {
53
66
foreach ($key in $Data.Keys ) {
54
67
$openGraphMetadata [$key ] = $Data [$key ]
55
68
}
56
69
}
57
70
58
- if (-not $openGraphMetadata.Count ) { return }
71
+ if (-not $openGraphMetadata.Count ) { return }
59
72
60
73
[PSCustomObject ]$openGraphMetadata
61
74
}
0 commit comments