[viki] Skip travis test
[youtube-dl] / youtube_dl / extractor / viki.py
1 import re
2
3 from ..utils import (
4     ExtractorError,
5     unified_strdate,
6 )
7 from .subtitles import SubtitlesInfoExtractor
8
9
10 class VikiIE(SubtitlesInfoExtractor):
11     IE_NAME = u'viki'
12
13     _VALID_URL = r'^https?://(?:www\.)?viki\.com/videos/(?P<id>[0-9]+v)'
14     _TEST = {
15         u'url': u'http://www.viki.com/videos/1023585v-heirs-episode-14',
16         u'file': u'1023585v.mp4',
17         u'md5': u'a21454021c2646f5433514177e2caa5f',
18         u'info_dict': {
19             u'title': u'Heirs Episode 14',
20             u'uploader': u'SBS',
21             u'description': u'md5:c4b17b9626dd4b143dcc4d855ba3474e',
22             u'upload_date': u'20131121',
23             u'age_limit': 13,
24         },
25         u'skip': u'Blocked in the US',
26     }
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         video_id = mobj.group(1)
31
32         webpage = self._download_webpage(url, video_id)
33         title = self._og_search_title(webpage)
34         description = self._og_search_description(webpage)
35         thumbnail = self._og_search_thumbnail(webpage)
36
37         uploader = self._html_search_regex(
38             r'<strong>Broadcast Network: </strong>\s*([^<]*)<', webpage,
39             u'uploader')
40         if uploader is not None:
41             uploader = uploader.strip()
42
43         rating_str = self._html_search_regex(
44             r'<strong>Rating: </strong>\s*([^<]*)<', webpage,
45             u'rating information', default='').strip()
46         RATINGS = {
47             'G': 0,
48             'PG': 10,
49             'PG-13': 13,
50             'R': 16,
51             'NC': 18,
52         }
53         age_limit = RATINGS.get(rating_str)
54
55         info_url = 'http://www.viki.com/player5_fragment/%s?action=show&controller=videos' % video_id
56         info_webpage = self._download_webpage(
57             info_url, video_id, note=u'Downloading info page')
58         if re.match(r'\s*<div\s+class="video-error', info_webpage):
59             raise ExtractorError(
60                 u'Video %s is blocked from your location.' % video_id,
61                 expected=True)
62         video_url = self._html_search_regex(
63             r'<source[^>]+src="([^"]+)"', info_webpage, u'video URL')
64
65         upload_date_str = self._html_search_regex(
66             r'"created_at":"([^"]+)"', info_webpage, u'upload date')
67         upload_date = (
68             unified_strdate(upload_date_str)
69             if upload_date_str is not None
70             else None
71         )
72
73         # subtitles
74         video_subtitles = self.extract_subtitles(video_id, info_webpage)
75         if self._downloader.params.get('listsubtitles', False):
76             self._list_available_subtitles(video_id, info_webpage)
77             return
78
79         return {
80             'id': video_id,
81             'title': title,
82             'url': video_url,
83             'description': description,
84             'thumbnail': thumbnail,
85             'age_limit': age_limit,
86             'uploader': uploader,
87             'subtitles': video_subtitles,
88             'upload_date': upload_date,
89         }
90
91     def _get_available_subtitles(self, video_id, info_webpage):
92         res = {}
93         for sturl in re.findall(r'<track src="([^"]+)"/>'):
94             m = re.search(r'/(?P<lang>[a-z]+)\.vtt', sturl)
95             if not m:
96                 continue
97             res[m.group('lang')] = sturl
98         return res