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