]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/udn.py
4fa74b9e84c87f7f9cf603f197a19c8d0e8b314f
[yt-dlp.git] / yt_dlp / extractor / udn.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 int_or_none,
7 js_to_json,
8 )
9 from ..compat import compat_urlparse
10
11
12 class UDNEmbedIE(InfoExtractor):
13 IE_DESC = '聯合影音'
14 _PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
15 _VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL
16 _TESTS = [{
17 'url': 'http://video.udn.com/embed/news/300040',
18 'info_dict': {
19 'id': '300040',
20 'ext': 'mp4',
21 'title': '生物老師男變女 全校挺"做自己"',
22 'thumbnail': r're:^https?://.*\.jpg$',
23 },
24 'params': {
25 # m3u8 download
26 'skip_download': True,
27 },
28 'expected_warnings': ['Failed to parse JSON Expecting value'],
29 }, {
30 'url': 'https://video.udn.com/embed/news/300040',
31 'only_matching': True,
32 }, {
33 # From https://video.udn.com/news/303776
34 'url': 'https://video.udn.com/play/news/303776',
35 'only_matching': True,
36 }]
37
38 def _real_extract(self, url):
39 video_id = self._match_id(url)
40
41 page = self._download_webpage(url, video_id)
42
43 options_str = self._html_search_regex(
44 r'var\s+options\s*=\s*([^;]+);', page, 'options')
45 trans_options_str = js_to_json(options_str)
46 options = self._parse_json(trans_options_str, 'options', fatal=False) or {}
47 if options:
48 video_urls = options['video']
49 title = options['title']
50 poster = options.get('poster')
51 else:
52 video_urls = self._parse_json(self._html_search_regex(
53 r'"video"\s*:\s*({.+?})\s*,', trans_options_str, 'video urls'), 'video urls')
54 title = self._html_search_regex(
55 r"title\s*:\s*'(.+?)'\s*,", options_str, 'title')
56 poster = self._html_search_regex(
57 r"poster\s*:\s*'(.+?)'\s*,", options_str, 'poster', default=None)
58
59 if video_urls.get('youtube'):
60 return self.url_result(video_urls.get('youtube'), 'Youtube')
61
62 formats = []
63 for video_type, api_url in video_urls.items():
64 if not api_url:
65 continue
66
67 video_url = self._download_webpage(
68 compat_urlparse.urljoin(url, api_url), video_id,
69 note='retrieve url for %s video' % video_type)
70
71 ext = determine_ext(video_url)
72 if ext == 'm3u8':
73 formats.extend(self._extract_m3u8_formats(
74 video_url, video_id, ext='mp4', m3u8_id='hls'))
75 elif ext == 'f4m':
76 formats.extend(self._extract_f4m_formats(
77 video_url, video_id, f4m_id='hds'))
78 else:
79 mobj = re.search(r'_(?P<height>\d+)p_(?P<tbr>\d+)\.mp4', video_url)
80 a_format = {
81 'url': video_url,
82 # video_type may be 'mp4', which confuses YoutubeDL
83 'format_id': 'http-' + video_type,
84 }
85 if mobj:
86 a_format.update({
87 'height': int_or_none(mobj.group('height')),
88 'tbr': int_or_none(mobj.group('tbr')),
89 })
90 formats.append(a_format)
91
92 self._sort_formats(formats)
93
94 return {
95 'id': video_id,
96 'formats': formats,
97 'title': title,
98 'thumbnail': poster,
99 }