@@ -18,15 +18,22 @@ function Get-OpenGraph
1818 'https://msnbc.com/',
1919 'https://fox.com/' |
2020 Get-OpenGraph
21- . LINK
22- https://ogp.me/
21+ . EXAMPLE
22+ OpenGraph https://posh.pckt.blog/static-sites-are-simple-6u51kgj
2323 #>
24- [Alias (' openGraph' , ' ogp' )]
24+ [Alias (' openGraph' , ' ogp' , ' Test-OpenGraph ' , ' Test-OGP ' )]
2525 [CmdletBinding (PositionalBinding = $false )]
26- param (
26+ param (
27+ # A list of any arguments.
28+ # This allows the command to take natural input.
29+ [Parameter (ValueFromRemainingArguments )]
30+ [Alias (' Argument' , ' Arguments' , ' Args' )]
31+ [PSObject []]
32+ $ArgumentList ,
33+
2734 # The URL that may contain Open Graph metadata
28- [Parameter (Position = 0 , ValueFromPipeline , ValueFromPipelineByPropertyName )]
29- [Uri ]
35+ [Parameter (ValueFromPipelineByPropertyName )]
36+ [string ]
3037 $Url ,
3138
3239 # Any HTML that may contain open graph metadata.
@@ -42,75 +49,93 @@ function Get-OpenGraph
4249 # If set, forces the function to retrieve the Open Graph metadata even if it is already cached.
4350 [Parameter (ValueFromPipelineByPropertyName )]
4451 [switch ]
45- $Force
52+ $Force ,
53+
54+ # Any number of input objects
55+ [Parameter (ValueFromPipeline )]
56+ [Alias (' Input' )]
57+ [PSObject []]
58+ $InputObject
4659 )
4760
4861 begin {
4962 # Make a regex to match meta tags
50- # We will match both open and closed tags.
5163 $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- }
64+ if (-not $script :OpenGraphCache ) {
65+ $script :OpenGraphCache = [Ordered ]@ {}
66+ }
5767 }
5868
5969 process {
60- # Declare an empty object to hold the Open Graph metadata
61- $openGraphMetadata = [Ordered ]@ {PSTypeName = ' OpenGraph' }
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
78- }
70+ # Turn any of our strongly bound parameters into arguments
71+ if ($Url ) {
72+ $argumentList = @ ($argumentList ) + $url
7973 }
8074
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
86- $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 ]
75+ # If any data was provided
76+ if ($Data ) {
77+ $argumentList = @ ($argumentList ) + $Data
78+ }
79+ if ($InputObject ) {
80+ $ArgumentList = @ ($ArgumentList ) + $InputObject
81+ }
82+
83+ :nextArgument foreach ($argument in $argumentList ) {
84+ # Declare an empty object to hold the Open Graph metadata
85+ if (-not $argument ) { continue }
86+ $openGraphMetadata = [Ordered ]@ {PSTypeName = ' OpenGraph' }
87+ $url , $data = $null , $null
88+
89+ if ($argument -as [uri ]) {
90+ $url = $argument -as [uri ]
91+ } elseif ($argument -is [Collections.IDictionary ]) {
92+ $data = $argument
93+ } else {
94+ Write-Warning " Only [uri] and [Collections.IDictionary] supported: $argument "
95+ continue nextArgument
96+ }
97+
98+ if ($Url ) {
99+ if ($script :OpenGraphCache [$url ] -and -not $Force ) {
100+ foreach ($key in $script :OpenGraphCache [$url ].Keys) {
101+ $openGraphMetadata [$key ] =
102+ $script :OpenGraphCache [$url ][$key ]
103+ }
104+ ([PSCustomObject ]$script :OpenGraphCache [$url ])
105+ continue nextArgument
91106 }
92- # If the meta tag contained a property and content,
93- if ($matchXml.meta.property -and $matchXml.meta.content ) {
94- # we will add it to our openGraph metadata.
95- $openGraphMetadata [
96- $matchXml.meta.property
97- ] = $matchXml.meta.content
107+
108+ $restResponse = Invoke-RestMethod - Uri $Url
109+
110+ foreach ($match in $metaRegex.Matches (" $restResponse " )) {
111+ $matchXml = (
112+ # close unclosed `<meta>` tags.
113+ " $match " -replace ' /?>$' , ' />'
114+ ) -as [xml ]
115+
116+ if ($matchXml.meta.property -and $matchXml.meta.content ) {
117+ $openGraphMetadata [$matchXml.meta.property ] = $matchXml.meta.content
118+ }
98119 }
120+
121+ $script :OpenGraphCache [$url ] = $openGraphMetadata
99122 }
100- }
101123
102- # If any data was provided
103- if ($Data ) {
104- # copy it into open graph metadata
105- foreach ($key in $Data.Keys ) {
106- $openGraphMetadata [$key ] = $Data [$key ]
124+ if ($Data ) {
125+ foreach ($key in $Data.Keys ) {
126+ $openGraphMetadata [$key ] = $Data [$key ]
127+ }
128+ }
129+
130+ # If there was no metadata
131+ if (-not $openGraphMetadata.Count ) {
132+ # output false (so `Test-` verb scenarios are met)
133+ $false
134+ # and continue to the next argument.
135+ continue nextArgument
107136 }
108- }
109-
110- # If there was no open graph metadata, return nothing.
111- if (-not $openGraphMetadata.Count ) { return }
112137
113- # Otherwise, return our OpenGraph metadata
114- [ PSCustomObject ] $openGraphMetadata
138+ [ PSCustomObject ] $openGraphMetadata
139+ }
115140 }
116141}
0 commit comments