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