function for lambda

This commit is contained in:
Kyle Gonsalves 2024-04-23 16:15:22 -07:00
parent d5b48c06e6
commit 1d851a6751

View file

@ -1293,6 +1293,9 @@ def parse_model(model):
}) })
} }
def is_type(*types):
return lambda _, v: v['type'] in types
initial_data = self._search_regex( initial_data = self._search_regex(
r'window\.__INITIAL_DATA__\s*=\s*("{.+?}")\s*;', webpage, r'window\.__INITIAL_DATA__\s*=\s*("{.+?}")\s*;', webpage,
'quoted preload state', default=None) 'quoted preload state', default=None)
@ -1305,10 +1308,10 @@ def parse_model(model):
initial_data = self._parse_json(initial_data, playlist_id, fatal=False) initial_data = self._parse_json(initial_data, playlist_id, fatal=False)
if initial_data: if initial_data:
for video_data in traverse_obj(initial_data, ( for video_data in traverse_obj(initial_data, (
'stores', 'article', 'articleBodyContent', lambda _, v: v['type'] == 'video')): 'stores', 'article', 'articleBodyContent', is_type('video'))):
model = traverse_obj(video_data, ( model = traverse_obj(video_data, (
'model', 'blocks', lambda _, v: v['type'] == 'aresMedia', 'model', 'blocks', is_type('aresMedia'),
'model', 'blocks', lambda _, v: v['type'] == 'aresMediaMetadata', 'model', 'blocks', is_type('aresMediaMetadata'),
'model', {dict}, any)) 'model', {dict}, any))
entry = parse_model(model) entry = parse_model(model)
if entry: if entry:
@ -1374,15 +1377,15 @@ def extract_all(pattern):
next_data = traverse_obj(self._search_nextjs_data(webpage, playlist_id, default={}), next_data = traverse_obj(self._search_nextjs_data(webpage, playlist_id, default={}),
('props', 'pageProps', 'page')) ('props', 'pageProps', 'page'))
model = traverse_obj(next_data, ( model = traverse_obj(next_data, (
..., 'contents', lambda _, v: v['type'] == 'video', ..., 'contents', is_type('video'),
'model', 'blocks', lambda _, v: v['type'] == 'media', 'model', 'blocks', is_type('media'),
'model', 'blocks', lambda _, v: v['type'] == 'mediaMetadata', 'model', 'blocks', is_type('mediaMetadata'),
'model', {dict}, any)) 'model', {dict}, any))
if model: if model:
if entry := parse_model(model): if entry := parse_model(model):
if not entry.get('timestamp'): if not entry.get('timestamp'):
entry['timestamp'] = traverse_obj(next_data, ( entry['timestamp'] = traverse_obj(next_data, (
..., 'contents', lambda _, v: v['type'] == 'timestamp', ..., 'contents', is_type('timestamp'),
'model', 'timestamp', {k_int_or_none}, any)) 'model', 'timestamp', {k_int_or_none}, any))
entries.append(entry) entries.append(entry)
return self.playlist_result( return self.playlist_result(