1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-06-27 17:08:32 +00:00

[test:InfoExtractor] improve _search_nuxt_json test

Authored by: bashonly
This commit is contained in:
bashonly 2025-06-11 12:45:37 -05:00
parent 4c2e4840d3
commit 8578e78719
No known key found for this signature in database
GPG Key ID: 783F096F253D15B0

View File

@ -1948,41 +1948,53 @@ def test_search_nextjs_data(self):
self.assertEqual(self.ie._search_nextjs_data('', None, default='{}'), {}) self.assertEqual(self.ie._search_nextjs_data('', None, default='{}'), {})
def test_search_nuxt_json(self): def test_search_nuxt_json(self):
HTML = ''' HTML_TMPL = '<script data-ssr="true" id="__NUXT_DATA__" type="application/json">[{}]</script>'
<script data-ssr="true" id="__NUXT_DATA__" type="application/json"> VALID_DATA = '''
[ ["ShallowReactive",1],
["ShallowReactive",1], {"data":2,"state":21,"once":25,"_errors":28,"_server_errors":30},
{"data":2,"state":21,"once":25,"_errors":28}, ["ShallowReactive",3],
["ShallowReactive",3], {"$abcdef123456":4},
{"$abcdef123456":4}, {"podcast":5,"activeEpisodeData":7},
{"podcast":5,"activeEpisodeData":7}, {"podcast":6,"seasons":14},
{"podcast":6,"seasons":14}, {"title":10,"id":11},
{"title":10,"id":11}, ["Reactive",8],
["Reactive",8], {"episode":9,"creators":18,"empty_list":20},
{"episode":9,"creators":18,"empty_list":20}, {"title":12,"id":13,"refs":34,"empty_refs":35},
{"title":12,"id":13}, "Series Title",
"Series Title", "podcast-id-01",
"podcast-id-01", "Episode Title",
"Episode Title", "episode-id-99",
"episode-id-99", [15,16,17],
[15,16,17], 1,
1, 2,
2, 3,
3, [19],
[19], "Podcast Creator",
"Podcast Creator", [],
[], {"$ssite-config":22},
{"$ssite-config":22}, {"env":23,"name":24,"map":26,"numbers":14},
{"env":23,"name":24,"map":26,"numbers":14}, "production",
"production", "podcast-website",
"podcast-website", ["Set"],
["Set"], ["Reactive",27],
["Reactive",27], ["Map"],
["Map"], ["ShallowReactive",29],
["ShallowReactive",29], {},
{} ["NuxtError",31],
] {"status":32,"message":33},
</script>''' 503,
"Service Unavailable",
[36,37],
[38,39],
["Ref",40],
["ShallowRef",41],
["EmptyRef",42],
["EmptyShallowRef",43],
"ref",
"shallow_ref",
"{\\"ref\\":1}",
"{\\"shallow_ref\\":2}"
'''
PAYLOAD = { PAYLOAD = {
'data': { 'data': {
'$abcdef123456': { '$abcdef123456': {
@ -1997,6 +2009,8 @@ def test_search_nuxt_json(self):
'episode': { 'episode': {
'title': 'Episode Title', 'title': 'Episode Title',
'id': 'episode-id-99', 'id': 'episode-id-99',
'refs': ['ref', 'shallow_ref'],
'empty_refs': [{'ref': 1}, {'shallow_ref': 2}],
}, },
'creators': ['Podcast Creator'], 'creators': ['Podcast Creator'],
'empty_list': [], 'empty_list': [],
@ -2013,20 +2027,43 @@ def test_search_nuxt_json(self):
}, },
'once': [], 'once': [],
'_errors': {}, '_errors': {},
'_server_errors': {
'status': 503,
'message': 'Service Unavailable',
},
} }
BAD_HTML = ''' INVALID_LIST = [
<script data-ssr="true" id="__NUXT_DATA__" type="application/json"> '''
[ {"data":1},
["ShallowReactive",1], {"invalid_raw_list":2},
{"data":2}, [15,16,17]
{"improper_raw_list":3}, ''',
[15,16,17] '''
] {"data":1},
</script>''' ["EmptyRef",2],
"not valid JSON"
''',
'''
{"data":1},
["EmptyShallowRef",2],
"not valid JSON"
''',
'''
{"data":1},
["unsupported",2],
{}
''',
]
DEFAULT = {'default': 'works'}
self.assertEqual(self.ie._search_nuxt_json(HTML, 'id'), PAYLOAD) self.assertEqual(self.ie._search_nuxt_json(HTML_TMPL.format(VALID_DATA), None), PAYLOAD)
self.assertEqual(self.ie._search_nuxt_json('', None, fatal=False), {}) self.assertEqual(self.ie._search_nuxt_json('', None, fatal=False), {})
self.assertEqual(self.ie._search_nuxt_json(BAD_HTML, None, default={}), {}) self.assertEqual(self.ie._search_nuxt_json('', None, default=DEFAULT), DEFAULT)
self.assertEqual(self.ie._search_nuxt_json(HTML_TMPL.format(INVALID_LIST[0]), None, fatal=False), {})
for invalid_data in INVALID_LIST[1:]:
self.assertEqual(
self.ie._search_nuxt_json(HTML_TMPL.format(invalid_data), None, default=DEFAULT),
DEFAULT)
if __name__ == '__main__': if __name__ == '__main__':