[viki] Enhance error message handling (#3774)
[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 )
16 from .common import InfoExtractor
17
18
19 class VikiIE(InfoExtractor):
20     IE_NAME = 'viki'
21
22     # iPad2
23     _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'
24
25     _VALID_URL = r'^https?://(?:www\.)?viki\.com/videos/(?P<id>[0-9]+v)'
26     _TESTS = [{
27         'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
28         'info_dict': {
29             'id': '1023585v',
30             'ext': 'mp4',
31             'title': 'Heirs Episode 14',
32             'uploader': 'SBS',
33             'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
34             'upload_date': '20131121',
35             'age_limit': 13,
36         },
37         'skip': 'Blocked in the US',
38     }, {
39         'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
40         'md5': 'ca6493e6f0a6ec07da9aa8d6304b4b2c',
41         'info_dict': {
42             'id': '1067139v',
43             'ext': 'mp4',
44             'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
45             'upload_date': '20150430',
46             'title': '\'The Avengers: Age of Ultron\' Press Conference',
47         }
48     }]
49
50     def _real_extract(self, url):
51         video_id = self._match_id(url)
52
53         webpage = self._download_webpage(url, video_id)
54         title = self._og_search_title(webpage)
55         description = self._og_search_description(webpage)
56         thumbnail = self._og_search_thumbnail(webpage)
57
58         uploader_m = re.search(
59             r'<strong>Broadcast Network: </strong>\s*([^<]*)<', webpage)
60         if uploader_m is None:
61             uploader = None
62         else:
63             uploader = uploader_m.group(1).strip()
64
65         rating_str = self._html_search_regex(
66             r'<strong>Rating: </strong>\s*([^<]*)<', webpage,
67             'rating information', default='').strip()
68         age_limit = US_RATINGS.get(rating_str)
69
70         req = compat_urllib_request.Request(
71             'http://www.viki.com/player5_fragment/%s?action=show&controller=videos' % video_id)
72         req.add_header('User-Agent', self._USER_AGENT)
73         info_webpage = self._download_webpage(
74             req, video_id, note='Downloading info page')
75         err_msg = self._html_search_regex(r'<div[^>]+class="video-error[^>]+>(.+)</div>', info_webpage, 'error message', default=None)
76         if err_msg:
77             err_msg = clean_html(err_msg)
78             if 'not available in your region' in err_msg:
79                 raise ExtractorError(
80                     'Video %s is blocked from your location.' % video_id,
81                     expected=True)
82             else:
83                 raise ExtractorError('Viki said: ' + err_msg)
84         video_url = self._html_search_regex(
85             r'<source[^>]+src="([^"]+)"', info_webpage, 'video URL')
86
87         upload_date_str = self._html_search_regex(
88             r'"created_at":"([^"]+)"', info_webpage, 'upload date')
89         upload_date = (
90             unified_strdate(upload_date_str)
91             if upload_date_str is not None
92             else None
93         )
94
95         # subtitles
96         video_subtitles = self.extract_subtitles(video_id, info_webpage)
97
98         return {
99             'id': video_id,
100             'title': title,
101             'url': video_url,
102             'description': description,
103             'thumbnail': thumbnail,
104             'age_limit': age_limit,
105             'uploader': uploader,
106             'subtitles': video_subtitles,
107             'upload_date': upload_date,
108         }
109
110     def _get_subtitles(self, video_id, info_webpage):
111         res = {}
112         for sturl_html in re.findall(r'<track src="([^"]+)"', info_webpage):
113             sturl = unescapeHTML(sturl_html)
114             m = re.search(r'/(?P<lang>[a-z]+)\.vtt', sturl)
115             if not m:
116                 continue
117             res[m.group('lang')] = [{
118                 'url': compat_urlparse.urljoin('http://www.viki.com', sturl),
119                 'ext': 'vtt',
120             }]
121         return res