-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-OpenGraph.ps1
More file actions
116 lines (104 loc) · 3.96 KB
/
Get-OpenGraph.ps1
File metadata and controls
116 lines (104 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function Get-OpenGraph
{
<#
.SYNOPSIS
Gets Open Graph metadata for a given URL.
.DESCRIPTION
Gets Open Graph metadata for a given URL.
[Open Graph](https://ogp.me/) is a protocol that enables any web page to become a rich object in a social graph.
It is used many social networks to display rich content when links are shared.
This function retrieves the Open Graph metadata from a given URL and returns it as a custom object.
.EXAMPLE
Get-OpenGraph -Url https://abc.com/
.EXAMPLE
'https://cnn.com/',
'https://msnbc.com/',
'https://fox.com/' |
Get-OpenGraph
.LINK
https://ogp.me/
#>
[Alias('openGraph','ogp')]
[CmdletBinding(PositionalBinding=$false)]
param(
# The URL that may contain Open Graph metadata
[Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Uri]
$Url,
# Any HTML that may contain open graph metadata.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$Html,
# A dictionary of additional Open Graph metadata to include in the result
[Parameter(ValueFromPipelineByPropertyName)]
[Collections.IDictionary]
$Data,
# If set, forces the function to retrieve the Open Graph metadata even if it is already cached.
[Parameter(ValueFromPipelineByPropertyName)]
[switch]
$Force
)
begin {
# Make a regex to match meta tags
# We will match both open and closed tags.
$metaRegex = [Regex]::new('<meta.+?/?>','IgnoreCase','00:00:00.1')
# If we do not have a cache
if (-not $script:Cache) {
# create one.
$script:Cache = [Ordered]@{}
}
}
process {
# Declare an empty object to hold the Open Graph metadata
$openGraphMetadata = [Ordered]@{PSTypeName='OpenGraph'}
if ($Url -and -not $PSBoundParameters['html']) {
# If the url is an absolute url with a scheme or http or https.
if ($url.Scheme -in 'http', 'https') {
# Get the url (if it is not cached).
if (-not $script:Cache[$url] -or $Force) {
$script:Cache[$url] =try {
Invoke-RestMethod -Uri $Url
} catch { $_ }
}
$html = $script:Cache[$url]
}
# Otherwise, see if the path exists
elseif (Test-Path $url)
{
# and get content from that path.
$html = Get-Content "$url" -Raw
}
}
# If we had any html,
if ($html) {
# find all of the `<meta>` tags.
foreach ($match in $metaRegex.Matches($html)) {
# Try to make them XML
$matchXml = "$match" -as [xml]
# If that fails,
if (-not $matchXml) {
# try once more after explicitly closing the end tag.
$matchXml = $match -replace '>$', '/>' -as [xml]
}
# If the meta tag contained a property and content,
if ($matchXml.meta.property -and $matchXml.meta.content) {
# we will add it to our openGraph metadata.
$openGraphMetadata[
$matchXml.meta.property
] = $matchXml.meta.content
}
}
}
# If any data was provided
if ($Data) {
# copy it into open graph metadata
foreach ($key in $Data.Keys) {
$openGraphMetadata[$key] = $Data[$key]
}
}
# If there was no open graph metadata, return nothing.
if (-not $openGraphMetadata.Count) { return }
# Otherwise, return our OpenGraph metadata
[PSCustomObject]$openGraphMetadata
}
}