+ # Look for Brightcove New Studio embeds
+ bc_url = BrightcoveNewIE._extract_url(self, webpage)
+ if bc_url:
+ return brightcove_url_result(bc_url)
+
+ brightcove_iframe = self._search_regex(
+ r'(<iframe[^>]+data-video-id=["\']\d+[^>]+>)', webpage,
+ 'brightcove iframe', default=None)
+ if brightcove_iframe:
+ attr = extract_attributes(brightcove_iframe)
+ src = attr.get('src')
+ if src:
+ parsed_src = compat_urlparse.urlparse(src)
+ qs = compat_urlparse.parse_qs(parsed_src.query)
+ account_id = qs.get('accountId', ['2376984109001'])[0]
+ brightcove_id = attr.get('data-video-id') or qs.get('videoId', [None])[0]
+ if account_id and brightcove_id:
+ return brightcove_url_result(
+ 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
+ % (account_id, brightcove_id))
+
+ # Query result is often embedded in webpage as JSON. Sometimes explicit requests
+ # to video API results in a failure with geo restriction reason therefore using
+ # embedded query result when present sounds reasonable.
+ config_json = self._search_regex(
+ r'window\.Af\.bootstrap\[[^\]]+\]\s*=\s*({.*?"applet_type"\s*:\s*"td-applet-videoplayer".*?});(?:</script>|$)',
+ webpage, 'videoplayer applet', default=None)
+ if config_json:
+ config = self._parse_json(config_json, display_id, fatal=False)
+ if config:
+ sapi = config.get('models', {}).get('applet_model', {}).get('data', {}).get('sapi')
+ if sapi and 'query' in sapi:
+ info = self._extract_info(display_id, sapi, webpage)
+ self._sort_formats(info['formats'])
+ return info
+
+ items_json = self._search_regex(
+ r'mediaItems: ({.*?})$', webpage, 'items', flags=re.MULTILINE,
+ default=None)
+ if items_json is None:
+ alias = self._search_regex(
+ r'"aliases":{"video":"(.*?)"', webpage, 'alias', default=None)
+ if alias is not None:
+ alias_info = self._download_json(
+ 'https://www.yahoo.com/_td/api/resource/VideoService.videos;video_aliases=["%s"]' % alias,
+ display_id, 'Downloading alias info')
+ video_id = alias_info[0]['id']
+ else:
+ CONTENT_ID_REGEXES = [
+ r'YUI\.namespace\("Media"\)\.CONTENT_ID\s*=\s*"([^"]+)"',
+ r'root\.App\.Cache\.context\.videoCache\.curVideo = \{"([^"]+)"',
+ r'"first_videoid"\s*:\s*"([^"]+)"',
+ r'%s[^}]*"ccm_id"\s*:\s*"([^"]+)"' % re.escape(page_id),
+ r'<article[^>]data-uuid=["\']([^"\']+)',
+ r'<meta[^<>]+yahoo://article/view\?.*\buuid=([^&"\']+)',
+ r'<meta[^<>]+["\']ytwnews://cavideo/(?:[^/]+/)+([\da-fA-F-]+)[&"\']',
+ ]
+ video_id = self._search_regex(
+ CONTENT_ID_REGEXES, webpage, 'content ID')
+ else:
+ items = json.loads(items_json)
+ info = items['mediaItems']['query']['results']['mediaObj'][0]
+ # The 'meta' field is not always in the video webpage, we request it
+ # from another page
+ video_id = info['id']
+ return self._get_info(video_id, display_id, webpage)
+
+ def _extract_info(self, display_id, query, webpage):
+ info = query['query']['results']['mediaObj'][0]
+ meta = info.get('meta')
+ video_id = info.get('id')
+
+ if not meta:
+ msg = info['status'].get('msg')
+ if msg:
+ raise ExtractorError(
+ '%s returned error: %s' % (self.IE_NAME, msg), expected=True)
+ raise ExtractorError('Unable to extract media object meta')
+
+ formats = []
+ for s in info['streams']:
+ tbr = int_or_none(s.get('bitrate'))
+ format_info = {
+ 'width': int_or_none(s.get('width')),
+ 'height': int_or_none(s.get('height')),
+ 'tbr': tbr,
+ }
+
+ host = s['host']
+ path = s['path']
+ if host.startswith('rtmp'):
+ fmt = 'rtmp'
+ format_info.update({
+ 'url': host,
+ 'play_path': path,
+ 'ext': 'flv',
+ })
+ else:
+ if s.get('format') == 'm3u8_playlist':
+ fmt = 'hls'
+ format_info.update({
+ 'protocol': 'm3u8_native',
+ 'ext': 'mp4',
+ })
+ else:
+ fmt = format_info['ext'] = determine_ext(path)
+ format_url = compat_urlparse.urljoin(host, path)
+ format_info['url'] = format_url
+ format_info['format_id'] = fmt + ('-%d' % tbr if tbr else '')
+ formats.append(format_info)
+
+ closed_captions = self._html_search_regex(
+ r'"closedcaptions":(\[[^\]]+\])', webpage, 'closed captions',
+ default='[]')
+
+ cc_json = self._parse_json(closed_captions, video_id, fatal=False)
+ subtitles = {}
+ if cc_json:
+ for closed_caption in cc_json:
+ lang = closed_caption['lang']
+ if lang not in subtitles:
+ subtitles[lang] = []
+ subtitles[lang].append({
+ 'url': closed_caption['url'],
+ 'ext': mimetype2ext(closed_caption['content_type']),
+ })
+
+ return {
+ 'id': video_id,
+ 'display_id': display_id,
+ 'title': unescapeHTML(meta['title']),
+ 'formats': formats,
+ 'description': clean_html(meta['description']),
+ 'thumbnail': meta['thumbnail'] if meta.get('thumbnail') else self._og_search_thumbnail(webpage),
+ 'duration': int_or_none(meta.get('duration')),
+ 'subtitles': subtitles,
+ }
+
+ def _get_info(self, video_id, display_id, webpage):
+ region = self._search_regex(
+ r'\\?"region\\?"\s*:\s*\\?"([^"]+?)\\?"',
+ webpage, 'region', fatal=False, default='US').upper()
+ formats = []
+ info = {}
+ for fmt in ('webm', 'mp4'):
+ query_result = self._download_json(
+ 'https://video.media.yql.yahoo.com/v1/video/sapi/streams/' + video_id,
+ display_id, 'Downloading %s video info' % fmt, query={
+ 'protocol': 'http',
+ 'region': region,
+ 'format': fmt,
+ })
+ info = self._extract_info(display_id, query_result, webpage)
+ formats.extend(info['formats'])
+ formats.extend(self._extract_m3u8_formats(
+ 'http://video.media.yql.yahoo.com/v1/hls/%s?region=%s' % (video_id, region),
+ video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
+ self._sort_formats(formats)
+ info['formats'] = formats
+ return info
+
+
+class YahooSearchIE(SearchInfoExtractor):
+ IE_DESC = 'Yahoo screen search'