@@ -18,15 +18,22 @@ function Get-OpenGraph
1818 'https://msnbc.com/',
1919 'https://fox.com/' |
2020 Get-OpenGraph
21+ . LINK
22+ https://ogp.me/
2123 #>
2224 [Alias (' openGraph' , ' ogp' )]
2325 [CmdletBinding (PositionalBinding = $false )]
2426 param (
2527 # The URL that may contain Open Graph metadata
26- [Parameter (ValueFromPipelineByPropertyName )]
28+ [Parameter (Position = 0 , ValueFromPipeline , ValueFromPipelineByPropertyName )]
2729 [Uri ]
2830 $Url ,
2931
32+ # Any HTML that may contain open graph metadata.
33+ [Parameter (ValueFromPipelineByPropertyName )]
34+ [string ]
35+ $Html ,
36+
3037 # A dictionary of additional Open Graph metadata to include in the result
3138 [Parameter (ValueFromPipelineByPropertyName )]
3239 [Collections.IDictionary ]
@@ -40,36 +47,70 @@ function Get-OpenGraph
4047
4148 begin {
4249 # Make a regex to match meta tags
43- $metaRegex = [Regex ]::new(' <meta.+?/>' , ' IgnoreCase' , ' 00:00:00.1' )
44- if (-not $script :OpenGraphCache ) {
45- $script :OpenGraphCache = [Ordered ]@ {}
46- }
50+ # We will match both open and closed tags.
51+ $metaRegex = [Regex ]::new(' <meta.+?/?>' , ' IgnoreCase' , ' 00:00:00.1' )
52+ # If we do not have a cache
53+ if (-not $script :Cache ) {
54+ # create one.
55+ $script :Cache = [Ordered ]@ {}
56+ }
4757 }
4858
4959 process {
5060 # Declare an empty object to hold the Open Graph metadata
5161 $openGraphMetadata = [Ordered ]@ {PSTypeName = ' OpenGraph' }
52- if ($Url ) {
53- if ($script :OpenGraphCache [$url ] -and -not $Force ) {
54- return $script :OpenGraphCache [$url ]
62+ if ($Url -and -not $PSBoundParameters [' html' ]) {
63+ # If the url is an absolute url with a scheme or http or https.
64+ if ($url.Scheme -in ' http' , ' https' ) {
65+ # Get the url (if it is not cached).
66+ if (-not $script :Cache [$url ] -or $Force ) {
67+ $script :Cache [$url ] = try {
68+ Invoke-RestMethod - Uri $Url
69+ } catch { $_ }
70+ }
71+ $html = $script :Cache [$url ]
72+ }
73+ # Otherwise, see if the path exists
74+ elseif (Test-Path $url )
75+ {
76+ # and get content from that path.
77+ $html = Get-Content " $url " - Raw
5578 }
56- $restResponse = Invoke-RestMethod - Uri $Url
57- foreach ($match in $metaRegex.Matches (" $restResponse " )) {
79+ }
80+
81+ # If we had any html,
82+ if ($html ) {
83+ # find all of the `<meta>` tags.
84+ foreach ($match in $metaRegex.Matches ($html )) {
85+ # Try to make them XML
5886 $matchXml = " $match " -as [xml ]
87+ # If that fails,
88+ if (-not $matchXml ) {
89+ # try once more after explicitly closing the end tag.
90+ $matchXml = $match -replace ' >$' , ' />' -as [xml ]
91+ }
92+ # If the meta tag contained a property and content,
5993 if ($matchXml.meta.property -and $matchXml.meta.content ) {
60- $openGraphMetadata [$matchXml.meta.property ] = $matchXml.meta.content
94+ # we will add it to our openGraph metadata.
95+ $openGraphMetadata [
96+ $matchXml.meta.property
97+ ] = $matchXml.meta.content
6198 }
6299 }
63- $script :OpenGraphCache [$url ] = $openGraphMetadata
64100 }
101+
102+ # If any data was provided
65103 if ($Data ) {
104+ # copy it into open graph metadata
66105 foreach ($key in $Data.Keys ) {
67106 $openGraphMetadata [$key ] = $Data [$key ]
68107 }
69108 }
70109
110+ # If there was no open graph metadata, return nothing.
71111 if (-not $openGraphMetadata.Count ) { return }
72112
113+ # Otherwise, return our OpenGraph metadata
73114 [PSCustomObject ]$openGraphMetadata
74115 }
75116}
0 commit comments