]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/udn.py
[extractor/generic] Remove HEAD request
[yt-dlp.git] / yt_dlp / extractor / udn.py
CommitLineData
0ff3749b
YCH
1import re
2
418c5cc3 3from .common import InfoExtractor
d0eb724e 4from ..utils import (
0ff3749b
YCH
5 determine_ext,
6 int_or_none,
d0eb724e 7 js_to_json,
d0eb724e 8)
0a160363 9from ..compat import compat_urlparse
418c5cc3
YCH
10
11
12class UDNEmbedIE(InfoExtractor):
9b15be97 13 IE_DESC = '聯合影音'
c39fd7b1
YCH
14 _PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
15 _VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL
418c5cc3
YCH
16 _TESTS = [{
17 'url': 'http://video.udn.com/embed/news/300040',
418c5cc3
YCH
18 'info_dict': {
19 'id': '300040',
20 'ext': 'mp4',
21 'title': '生物老師男變女 全校挺"做自己"',
ec85ded8 22 'thumbnail': r're:^https?://.*\.jpg$',
0ff3749b
YCH
23 },
24 'params': {
25 # m3u8 download
26 'skip_download': True,
27 },
81ce479f 28 'expected_warnings': ['Failed to parse JSON Expecting value'],
418c5cc3 29 }, {
d0eb724e
YCH
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',
418c5cc3
YCH
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
81ce479f
RA
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)
418c5cc3
YCH
58
59 if video_urls.get('youtube'):
60 return self.url_result(video_urls.get('youtube'), 'Youtube')
61
0ff3749b
YCH
62 formats = []
63 for video_type, api_url in video_urls.items():
64 if not api_url:
65 continue
418c5cc3 66
0ff3749b 67 video_url = self._download_webpage(
0a160363 68 compat_urlparse.urljoin(url, api_url), video_id,
0ff3749b 69 note='retrieve url for %s video' % video_type)
d0eb724e 70
0ff3749b
YCH
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:
81ce479f 79 mobj = re.search(r'_(?P<height>\d+)p_(?P<tbr>\d+)\.mp4', video_url)
0ff3749b
YCH
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)
418c5cc3
YCH
91
92 self._sort_formats(formats)
93
418c5cc3
YCH
94 return {
95 'id': video_id,
96 'formats': formats,
81ce479f
RA
97 'title': title,
98 'thumbnail': poster,
418c5cc3 99 }