@@ -17,45 +17,58 @@ function Get-OpenGraph
1717 'https://cnn.com/',
1818 'https://msnbc.com/',
1919 'https://fox.com/' |
20- Get-OpenGraph
20+ Get-OpenGraph
2121 #>
2222 [Alias (' openGraph' , ' ogp' )]
23- param (
23+ [CmdletBinding (PositionalBinding = $false )]
24+ param (
2425 # The URL that may contain Open Graph metadata
25- [Parameter (ValueFromPipeline , ValueFromPipelineByPropertyName )]
26+ [Parameter (ValueFromPipelineByPropertyName )]
2627 [Uri ]
2728 $Url ,
2829
2930 # A dictionary of additional Open Graph metadata to include in the result
3031 [Parameter (ValueFromPipelineByPropertyName )]
3132 [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
3339 )
3440
3541 begin {
3642 # Make a regex to match meta tags
3743 $metaRegex = [Regex ]::new(' <meta.+?/>' , ' IgnoreCase' , ' 00:00:00.1' )
44+ if (-not $script :OpenGraphCache ) {
45+ $script :OpenGraphCache = [Ordered ]@ {}
46+ }
3847 }
3948
4049 process {
4150 # Declare an empty object to hold the Open Graph metadata
4251 $openGraphMetadata = [Ordered ]@ {PSTypeName = ' OpenGraph' }
4352 if ($Url ) {
53+ if ($script :OpenGraphCache [$url ] -and -not $Force ) {
54+ return $script :OpenGraphCache [$url ]
55+ }
4456 $restResponse = Invoke-RestMethod - Uri $Url
4557 foreach ($match in $metaRegex.Matches (" $restResponse " )) {
4658 $matchXml = " $match " -as [xml ]
4759 if ($matchXml.meta.property -and $matchXml.meta.content ) {
4860 $openGraphMetadata [$matchXml.meta.property ] = $matchXml.meta.content
4961 }
5062 }
63+ $script :OpenGraphCache [$url ] = $openGraphMetadata
5164 }
5265 if ($Data ) {
5366 foreach ($key in $Data.Keys ) {
5467 $openGraphMetadata [$key ] = $Data [$key ]
5568 }
5669 }
5770
58- if (-not $openGraphMetadata.Count ) { return }
71+ if (-not $openGraphMetadata.Count ) { return }
5972
6073 [PSCustomObject ]$openGraphMetadata
6174 }
0 commit comments