1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-09-28 06:15:27 +00:00

JSON-LD: Support top-level @graph expressing implicit default graph

Per W3C JSON-LD v1.1 §4.9 (non-normative ref):

    When a JSON-LD document's top-level structure is a map that contains
    no other keys than @graph and optionally @context (properties that
    are not mapped to an IRI or a keyword are ignored), @graph is
    considered to express the otherwise implicit default graph.

Support such a structure in InfoExtractor._json_ld parsing:
Wrap the control flow block in a function, which is called recursively
upon such a structure
This commit is contained in:
Zenon Mousmoulas 2021-11-10 22:21:14 +02:00
parent a803582717
commit 77e8f5353c

View file

@ -1296,8 +1296,16 @@ class InfoExtractor(object):
}) })
extract_interaction_statistic(e) extract_interaction_statistic(e)
for e in json_ld: def traverse_json_ld(json_ld, info, at_top_level=True):
if '@context' in e: for e in json_ld:
if at_top_level and '@context' not in e:
continue
if at_top_level and all(k in ('@context', '@graph') for k in e):
graph = e['@graph']
if isinstance(graph, dict):
graph = [graph]
traverse_json_ld(graph, info, at_top_level=False)
break
item_type = e.get('@type') item_type = e.get('@type')
if expected_type is not None and expected_type != item_type: if expected_type is not None and expected_type != item_type:
continue continue
@ -1345,6 +1353,8 @@ class InfoExtractor(object):
continue continue
else: else:
break break
traverse_json_ld(json_ld, info)
return dict((k, v) for k, v in info.items() if v is not None) return dict((k, v) for k, v in info.items() if v is not None)
@staticmethod @staticmethod