-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-JsonLD.ps1
More file actions
85 lines (79 loc) · 3.44 KB
/
Copy pathGet-JsonLD.ps1
File metadata and controls
85 lines (79 loc) · 3.44 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
function Get-JsonLD {
<#
.SYNOPSIS
Gets JSON-LD data from a given URL.
.DESCRIPTION
Gets JSON Linked Data from a given URL.
This is a format used by many websites to provide structured data about their content.
.EXAMPLE
# Want to get information about a movie? Linked Data to the rescue!
Get-JsonLD -Url https://www.imdb.com/title/tt0211915/
.EXAMPLE
# Want information about an article? Lots of news sites use this format.
Get-JsonLD https://www.thebulwark.com/p/mahmoud-khalil-immigration-detention-first-amendment-free-speech-rights
.EXAMPLE
# Want to get information about a schema?
jsonld https://schema.org/Movie
# Get-JSONLD will output the contents of a `@Graph` object if no `@type` is found.
#>
[Alias('jsonLD','json-ld')]
param(
# The URL that may contain JSON-LD data
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Uri]
$Url,
# If set, will force the request to be made even if the URL has already been cached.
[switch]
$Force
)
begin {
# Create a pattern to match the JSON-LD script tag
$linkedDataRegex = [Regex]::new(@'
(?<HTML_LinkedData>
<script # Match <script tag
\s{1,} # Then whitespace
type= # Then the type= attribute (this regex will only match if it is first)
[\"\'] # Double or Single Quotes
application/ld\+json # The type that indicates linked data
[\"\'] # Double or Single Quotes
[^>]{0,} # Match anything until the end of the start tag
\> # Match the end of the start tag
(?<JsonContent>(?:.|\s){0,}?(?=\z|</script>)) # Anything until the end tag is JSONContent
)
'@, 'IgnoreCase,IgnorePatternWhitespace','00:00:00.1')
# Initialize the cache for JSON-LD requests
if (-not $script:JsonLDRequestCache) {
$script:JsonLDRequestCache = [Ordered]@{}
}
}
process {
$restResponse =
if ($Force -or -not $script:JsonLDRequestCache[$url]) {
$script:JsonLDRequestCache[$url] = Invoke-RestMethod -Uri $Url
$script:JsonLDRequestCache[$url]
} else {
$script:JsonLDRequestCache[$url]
}
foreach ($match in $linkedDataRegex.Matches("$restResponse")) {
foreach ($jsonObject in
$match.Groups['JsonContent'].Value |
ConvertFrom-Json
) {
if ($jsonObject.'@type') {
$schemaType = $jsonObject.'@context',$jsonObject.'@type' -ne '' -join '/'
$jsonObject.pstypenames.insert(0, $schemaType)
$jsonObject
} elseif ($jsonObject.'@graph') {
foreach ($graphObject in $jsonObject.'@graph') {
if ($graphObject.'@type') {
$graphObject.pstypenames.insert(0, $graphObject.'@type')
}
$graphObject
}
} else {
$jsonObject
}
}
}
}
}