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