- if vids_data_string is not None:
- vids_data = self._parse_json(
- vids_data_string, video_id,
- transform_source=js_to_json)
- video_data = [v for v in vids_data.values()
- if 'slug' in v and v['slug'] == video_id]
- if len(video_data) > 0 and 'streamId' in video_data[0]:
- wat_id = video_data[0]['streamId']
- if wat_id is None:
+
+ data = self._parse_json(
+ self._search_regex(
+ r'__APOLLO_STATE__\s*=\s*({.+?})\s*(?:;|</script>)', webpage,
+ 'data', default='{}'), video_id, fatal=False)
+
+ if data:
+ try:
+ wat_id = next(
+ video.get('streamId')
+ for key, video in data.items()
+ if isinstance(video, dict)
+ and video.get('slug') == video_id)
+ if not isinstance(wat_id, compat_str) or not wat_id.isdigit():
+ wat_id = None
+ except StopIteration:
+ pass
+
+ if not wat_id: