[ie/twitter:spaces] Support video spaces (#10789)
[yt-dlp3.git] / yt_dlp / extractor / udn.py
blob9970e4ff5040afb2a6e66c1e7bc9076902eefb09
1 import re
2 import urllib.parse
4 from .common import InfoExtractor
5 from ..utils import (
6 determine_ext,
7 int_or_none,
8 js_to_json,
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 _EMBED_REGEX = [rf'<iframe[^>]+src="(?:https?:)?(?P<url>{_PROTOCOL_RELATIVE_VALID_URL})"']
17 _TESTS = [{
18 'url': 'http://video.udn.com/embed/news/300040',
19 'info_dict': {
20 'id': '300040',
21 'ext': 'mp4',
22 'title': '生物老師男變女 全校挺"做自己"',
23 'thumbnail': r're:^https?://.*\.jpg$',
25 'params': {
26 # m3u8 download
27 'skip_download': True,
29 'expected_warnings': ['Failed to parse JSON Expecting value'],
30 }, {
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',
36 'only_matching': True,
39 def _real_extract(self, url):
40 video_id = self._match_id(url)
42 page = self._download_webpage(url, video_id)
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)
60 if video_urls.get('youtube'):
61 return self.url_result(video_urls.get('youtube'), 'Youtube')
63 formats = []
64 for video_type, api_url in video_urls.items():
65 if not api_url:
66 continue
68 video_url = self._download_webpage(
69 urllib.parse.urljoin(url, api_url), video_id,
70 note=f'retrieve url for {video_type} video')
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:
80 mobj = re.search(r'_(?P<height>\d+)p_(?P<tbr>\d+)\.mp4', video_url)
81 a_format = {
82 'url': video_url,
83 # video_type may be 'mp4', which confuses YoutubeDL
84 'format_id': 'http-' + video_type,
86 if mobj:
87 a_format.update({
88 'height': int_or_none(mobj.group('height')),
89 'tbr': int_or_none(mobj.group('tbr')),
91 formats.append(a_format)
93 return {
94 'id': video_id,
95 'formats': formats,
96 'title': title,
97 'thumbnail': poster,