[viki] Extract m3u8 videos (#4855)
[youtube-dl] / youtube_dl / extractor / viki.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from ..compat import (
6     compat_urlparse,
7     compat_urllib_request,
8 )
9 from ..utils import (
10     ExtractorError,
11     unescapeHTML,
12     unified_strdate,
13     US_RATINGS,
14     clean_html,
15     determine_ext,
16     mimetype2ext,
17 )
18 from .common import InfoExtractor
19
20
21 class VikiIE(InfoExtractor):
22     IE_NAME = 'viki'
23
24     # iPad2
25     _USER_AGENT = 'Mozilla/5.0(iPad; U; CPU OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F191 Safari/6533.18.5'
26
27     _VALID_URL = r'^https?://(?:www\.)?viki\.com/videos/(?P<id>[0-9]+v)'
28     _TESTS = [{
29         'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
30         'info_dict': {
31             'id': '1023585v',
32             'ext': 'mp4',
33             'title': 'Heirs Episode 14',
34             'uploader': 'SBS',
35             'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
36             'upload_date': '20131121',
37             'age_limit': 13,
38         },
39         'skip': 'Blocked in the US',
40     }, {
41         'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
42         'md5': 'ca6493e6f0a6ec07da9aa8d6304b4b2c',
43         'info_dict': {
44             'id': '1067139v',
45             'ext': 'mp4',
46             'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
47             'upload_date': '20150430',
48             'title': '\'The Avengers: Age of Ultron\' Press Conference',
49         }
50     }, {
51         'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
52         'info_dict': {
53             'id': '1048879v',
54             'ext': 'mp4',
55             'upload_date': '20140820',
56             'description': 'md5:54ff56d51bdfc7a30441ec967394e91c',
57             'title': 'Ankhon Dekhi',
58         },
59         'params': {
60             # requires ffmpeg
61             'skip_download': True,
62         }
63     }]
64
65     def _real_extract(self, url):
66         video_id = self._match_id(url)
67
68         webpage = self._download_webpage(url, video_id)
69         title = self._og_search_title(webpage)
70         description = self._og_search_description(webpage)
71         thumbnail = self._og_search_thumbnail(webpage)
72
73         uploader_m = re.search(
74             r'<strong>Broadcast Network: </strong>\s*([^<]*)<', webpage)
75         if uploader_m is None:
76             uploader = None
77         else:
78             uploader = uploader_m.group(1).strip()
79
80         rating_str = self._html_search_regex(
81             r'<strong>Rating: </strong>\s*([^<]*)<', webpage,
82             'rating information', default='').strip()
83         age_limit = US_RATINGS.get(rating_str)
84
85         req = compat_urllib_request.Request(
86             'http://www.viki.com/player5_fragment/%s?action=show&controller=videos' % video_id)
87         req.add_header('User-Agent', self._USER_AGENT)
88         info_webpage = self._download_webpage(
89             req, video_id, note='Downloading info page')
90         err_msg = self._html_search_regex(r'<div[^>]+class="video-error[^>]+>(.+)</div>', info_webpage, 'error message', default=None)
91         if err_msg:
92             err_msg = clean_html(err_msg)
93             if 'not available in your region' in err_msg:
94                 raise ExtractorError(
95                     'Video %s is blocked from your location.' % video_id,
96                     expected=True)
97             else:
98                 raise ExtractorError('Viki said: ' + err_msg)
99         mobj = re.search(
100             r'<source[^>]+type="(?P<mime_type>[^"]+)"[^>]+src="(?P<url>[^"]+)"', info_webpage)
101         if not mobj:
102             raise ExtractorError('Unable to find video URL')
103         video_url = unescapeHTML(mobj.group('url'))
104         video_ext = mimetype2ext(mobj.group('mime_type'))
105
106         if determine_ext(video_url) == 'm3u8':
107             formats = self._extract_m3u8_formats(
108                 video_url, video_id, ext=video_ext)
109         else:
110             formats = [{
111                 'url': video_url,
112                 'ext': video_ext,
113             }]
114
115         upload_date_str = self._html_search_regex(
116             r'"created_at":"([^"]+)"', info_webpage, 'upload date')
117         upload_date = (
118             unified_strdate(upload_date_str)
119             if upload_date_str is not None
120             else None
121         )
122
123         # subtitles
124         video_subtitles = self.extract_subtitles(video_id, info_webpage)
125
126         return {
127             'id': video_id,
128             'title': title,
129             'formats': formats,
130             'description': description,
131             'thumbnail': thumbnail,
132             'age_limit': age_limit,
133             'uploader': uploader,
134             'subtitles': video_subtitles,
135             'upload_date': upload_date,
136         }
137
138     def _get_subtitles(self, video_id, info_webpage):
139         res = {}
140         for sturl_html in re.findall(r'<track src="([^"]+)"', info_webpage):
141             sturl = unescapeHTML(sturl_html)
142             m = re.search(r'/(?P<lang>[a-z]+)\.vtt', sturl)
143             if not m:
144                 continue
145             res[m.group('lang')] = [{
146                 'url': compat_urlparse.urljoin('http://www.viki.com', sturl),
147                 'ext': 'vtt',
148             }]
149         return res