2 from __future__ import unicode_literals
6 from .common import InfoExtractor
12 from ..compat import compat_urlparse
15 class UDNEmbedIE(InfoExtractor):
17 _PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
18 _VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL
20 'url': 'http://video.udn.com/embed/news/300040',
24 'title': '生物老師男變女 全校挺"做自己"',
25 'thumbnail': r're:^https?://.*\.jpg$',
29 'skip_download': True,
31 'expected_warnings': ['Failed to parse JSON Expecting value'],
33 'url': 'https://video.udn.com/embed/news/300040',
34 'only_matching': True,
36 # From https://video.udn.com/news/303776
37 'url': 'https://video.udn.com/play/news/303776',
38 'only_matching': True,
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
44 page = self._download_webpage(url, video_id)
46 options_str = self._html_search_regex(
47 r'var\s+options\s*=\s*([^;]+);', page, 'options')
48 trans_options_str = js_to_json(options_str)
49 options = self._parse_json(trans_options_str, 'options', fatal=False) or {}
51 video_urls = options['video']
52 title = options['title']
53 poster = options.get('poster')
55 video_urls = self._parse_json(self._html_search_regex(
56 r'"video"\s*:\s*({.+?})\s*,', trans_options_str, 'video urls'), 'video urls')
57 title = self._html_search_regex(
58 r"title\s*:\s*'(.+?)'\s*,", options_str, 'title')
59 poster = self._html_search_regex(
60 r"poster\s*:\s*'(.+?)'\s*,", options_str, 'poster', default=None)
62 if video_urls.get('youtube'):
63 return self.url_result(video_urls.get('youtube'), 'Youtube')
66 for video_type, api_url in video_urls.items():
70 video_url = self._download_webpage(
71 compat_urlparse.urljoin(url, api_url), video_id,
72 note='retrieve url for %s video' % video_type)
74 ext = determine_ext(video_url)
76 formats.extend(self._extract_m3u8_formats(
77 video_url, video_id, ext='mp4', m3u8_id='hls'))
79 formats.extend(self._extract_f4m_formats(
80 video_url, video_id, f4m_id='hds'))
82 mobj = re.search(r'_(?P<height>\d+)p_(?P<tbr>\d+)\.mp4', video_url)
85 # video_type may be 'mp4', which confuses YoutubeDL
86 'format_id': 'http-' + video_type,
90 'height': int_or_none(mobj.group('height')),
91 'tbr': int_or_none(mobj.group('tbr')),
93 formats.append(a_format)
95 self._sort_formats(formats)