]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/udn.py
[nrk] Relax _VALID_URL (closes #10928)
[youtube-dl] / youtube_dl / extractor / udn.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     determine_ext,
10     int_or_none,
11     js_to_json,
12 )
13 from ..compat import compat_urlparse
14
15
16 class UDNEmbedIE(InfoExtractor):
17     IE_DESC = '聯合影音'
18     _PROTOCOL_RELATIVE_VALID_URL = r'//video\.udn\.com/(?:embed|play)/news/(?P<id>\d+)'
19     _VALID_URL = r'https?:' + _PROTOCOL_RELATIVE_VALID_URL
20     _TESTS = [{
21         'url': 'http://video.udn.com/embed/news/300040',
22         'info_dict': {
23             'id': '300040',
24             'ext': 'mp4',
25             'title': '生物老師男變女 全校挺"做自己"',
26             'thumbnail': 're:^https?://.*\.jpg$',
27         },
28         'params': {
29             # m3u8 download
30             'skip_download': True,
31         },
32     }, {
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',
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(
47             r'var\s+options\s*=\s*([^;]+);', page, 'video urls dictionary')))
48
49         video_urls = options['video']
50
51         if video_urls.get('youtube'):
52             return self.url_result(video_urls.get('youtube'), 'Youtube')
53
54         formats = []
55         for video_type, api_url in video_urls.items():
56             if not api_url:
57                 continue
58
59             video_url = self._download_webpage(
60                 compat_urlparse.urljoin(url, api_url), video_id,
61                 note='retrieve url for %s video' % video_type)
62
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)
83
84         self._sort_formats(formats)
85
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]
90
91         return {
92             'id': video_id,
93             'formats': formats,
94             'title': options['title'],
95             'thumbnails': thumbnails,
96         }