Skip to content

Commit 706f24b

Browse files
author
James Brundage
committed
feat: Get-JsonLD ( Fixes #2 )
1 parent 1542fd8 commit 706f24b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Commands/Get-JsonLD.ps1

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function Get-JsonLD {
2+
<#
3+
.SYNOPSIS
4+
Gets JSON-LD data from a given URL.
5+
.DESCRIPTION
6+
Gets JSON Linked Data from a given URL.
7+
8+
This is a format used by many websites to provide structured data about their content.
9+
.EXAMPLE
10+
Get-JsonLD -Url https://www.imdb.com/title/tt0211915/
11+
#>
12+
param(
13+
# The URL that may contain JSON-LD data
14+
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
15+
[Uri]
16+
$Url
17+
)
18+
19+
begin {
20+
$linkedDataRegex = [Regex]::new(@'
21+
(?<HTML_LinkedData>
22+
<script # Match <script tag
23+
\s{1,} # Then whitespace
24+
type= # Then the type= attribute (this regex will only match if it is first)
25+
[\"\'] # Double or Single Quotes
26+
application/ld\+json # The type that indicates linked data
27+
[\"\'] # Double or Single Quotes
28+
[^>]{0,} # Match anything until the end of the start tag
29+
\> # Match the end of the start tag
30+
(?<JsonContent>(?:.|\s){0,}?(?=\z|</script>)) # Anything until the end tag is JSONContent
31+
)
32+
'@, 'IgnoreCase,IgnorePatternWhitespace','00:00:00.1')
33+
}
34+
35+
process {
36+
$restResponse = Invoke-RestMethod -Uri $Url
37+
foreach ($match in $linkedDataRegex.Matches("$restResponse")) {
38+
foreach ($jsonObject in
39+
$match.Groups['JsonContent'].Value |
40+
ConvertFrom-Json
41+
) {
42+
if ($jsonObject.'@context' -and $jsonObject.'@type') {
43+
$schemaType = $jsonObject.'@context',$jsonObject.'@type' -join '/'
44+
$jsonObject.pstypenames.insert(0, $schemaType)
45+
}
46+
$jsonObject
47+
}
48+
}
49+
50+
}
51+
}

0 commit comments

Comments
 (0)