- BEFORE = '{swf.addParam(param[0], param[1]);});'
- AFTER = '.forEach(function(variable) {swf.addVariable(variable[0], variable[1]);});'
- PATTERN = re.escape(BEFORE) + '(?:\n|\\\\n)(.*?)' + re.escape(AFTER)
-
- for m in re.findall(PATTERN, webpage):
- swf_params = m.replace('\\\\', '\\').replace('\\"', '"')
- data = dict(json.loads(swf_params))
- params_raw = compat_urllib_parse_unquote(data['params'])
- video_data_candidate = json.loads(params_raw)['video_data']
- for _, f in video_data_candidate.items():
- if not f:
- continue
- if isinstance(f, dict):
- f = [f]
- if not isinstance(f, list):
- continue
- if f[0].get('video_id') == video_id:
- video_data = video_data_candidate
- break
- if video_data:
- break
-
- def video_data_list2dict(video_data):
- ret = {}
- for item in video_data:
- format_id = item['stream_type']
- ret.setdefault(format_id, []).append(item)
- return ret
+ def extract_video_data(instances):
+ for item in instances:
+ if item[1][0] == 'VideoConfig':
+ video_item = item[2][0]
+ if video_item.get('video_id') == video_id:
+ return video_item['videoData']
+
+ server_js_data = self._parse_json(self._search_regex(
+ r'handleServerJS\(({.+})(?:\);|,")', webpage,
+ 'server js data', default='{}'), video_id, fatal=False)
+
+ if server_js_data:
+ video_data = extract_video_data(server_js_data.get('instances', []))