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