Skip to content

Commit 01f0f2b

Browse files
feat: Get-JSONLD -As and updates ( Fixes #13, Fixes #16, Fixes #17, Fixes #18, Fixes #19 )
1 parent 82e7cf5 commit 01f0f2b

1 file changed

Lines changed: 78 additions & 11 deletions

File tree

Commands/Get-JsonLD.ps1

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function Get-JsonLD {
88
This is a format used by many websites to provide structured data about their content.
99
.EXAMPLE
1010
# Want to get information about a movie? Linked Data to the rescue!
11-
Get-JsonLD -Url https://www.imdb.com/title/tt0211915/
11+
Get-JsonLD -Url https://letterboxd.com/film/amelie/
1212
.EXAMPLE
1313
# Want information about an article? Lots of news sites use this format.
1414
Get-JsonLD https://www.thebulwark.com/p/mahmoud-khalil-immigration-detention-first-amendment-free-speech-rights
@@ -21,9 +21,31 @@ function Get-JsonLD {
2121
param(
2222
# The URL that may contain JSON-LD data
2323
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
24+
[Alias('href')]
2425
[Uri]
2526
$Url,
2627

28+
<#
29+
30+
If set, will the output as:
31+
32+
|as|is|
33+
|-|-|
34+
|html|the response as text|
35+
|json|the match as json|
36+
|*jsonld`|ld`|linkedData*|the match as linked data|'
37+
|script|the script tag|
38+
|xml|the script tag, as xml|
39+
40+
#>
41+
42+
[ValidateSet('html', 'json', 'jsonld', 'ld', 'linkedData', 'script', 'xml')]
43+
[string]
44+
$as = 'jsonld',
45+
46+
[switch]
47+
$RawHtml,
48+
2749
# If set, will force the request to be made even if the URL has already been cached.
2850
[switch]
2951
$Force
@@ -46,39 +68,84 @@ application/ld\+json # The type that indicates linked d
4668
'@, 'IgnoreCase,IgnorePatternWhitespace','00:00:00.1')
4769

4870
# Initialize the cache for JSON-LD requests
49-
if (-not $script:JsonLDRequestCache) {
50-
$script:JsonLDRequestCache = [Ordered]@{}
71+
if (-not $script:Cache) {
72+
$script:Cache = [Ordered]@{}
5173
}
5274
}
5375

5476
process {
5577
$restResponse =
56-
if ($Force -or -not $script:JsonLDRequestCache[$url]) {
57-
$script:JsonLDRequestCache[$url] = Invoke-RestMethod -Uri $Url
58-
$script:JsonLDRequestCache[$url]
78+
if ($Force -or -not $script:Cache[$url]) {
79+
$script:Cache[$url] = Invoke-RestMethod -Uri $Url
80+
$script:Cache[$url]
5981
} else {
60-
$script:JsonLDRequestCache[$url]
82+
$script:Cache[$url]
6183
}
84+
85+
if ($as -eq 'html') {
86+
return $restResponse
87+
}
88+
89+
90+
# Find all linked data tags within the response
6291
foreach ($match in $linkedDataRegex.Matches("$restResponse")) {
92+
# If we want the result as xml
93+
if ($As -eq 'xml') {
94+
# try to cast it
95+
$matchXml ="$match" -as [xml]
96+
if ($matchXml) {
97+
# and output it if found.
98+
$matchXml
99+
continue
100+
} else {
101+
# otherwise, fall back to the `<script>` tag
102+
$As = 'script'
103+
}
104+
}
105+
106+
# If we want the tag, that should be the whole match
107+
if ($As -eq 'script') {
108+
"$match"
109+
continue
110+
}
111+
112+
# If we want it as json, we have a match group.
113+
if ($As -eq 'json') {
114+
$match.Groups['JsonContent'].Value
115+
continue
116+
}
117+
# Otherwise, we want it as linked data, so convert from the json
63118
foreach ($jsonObject in
64119
$match.Groups['JsonContent'].Value |
65120
ConvertFrom-Json
66121
) {
122+
# If there was a `@type` property
67123
if ($jsonObject.'@type') {
124+
# all we need to do is decorate the object
125+
# If we combine the `@context` and `@type` property, we should have a schema url
68126
$schemaType = $jsonObject.'@context',$jsonObject.'@type' -ne '' -join '/'
127+
# and we can make that the typename
69128
$jsonObject.pstypenames.insert(0, $schemaType)
129+
# and show the object.
70130
$jsonObject
71-
} elseif ($jsonObject.'@graph') {
131+
}
132+
# If there was a `@graph` property
133+
elseif ($jsonObject.'@graph') {
134+
# we can display all items in the graph
72135
foreach ($graphObject in $jsonObject.'@graph') {
136+
# each of them will tell us it's `@type`
73137
if ($graphObject.'@type') {
138+
# and we can decorate each object appropriately
74139
$graphObject.pstypenames.insert(0, $graphObject.'@type')
75140
}
76141
$graphObject
77142
}
78-
} else {
79-
$jsonObject
80143
}
81-
144+
# If there is neither a `@type` or a `@graph`
145+
else {
146+
# just output the object.
147+
$jsonObject
148+
}
82149
}
83150
}
84151
}

0 commit comments

Comments
 (0)