Merge branch 'opener-to-ydl'
[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_m = re.search(
38             r'<strong>Broadcast Network: </strong>\s*([^<]*)<', webpage)
39         if uploader_m is None:
40             uploader = None
41         else:
42             uploader = uploader.group(1).strip()
43
44         rating_str = self._html_search_regex(
45             r'<strong>Rating: </strong>\s*([^<]*)<', webpage,
46             u'rating information', default='').strip()
47         RATINGS = {
48             'G': 0,
49             'PG': 10,
50             'PG-13': 13,
51             'R': 16,
52             'NC': 18,
53         }
54         age_limit = RATINGS.get(rating_str)
55
56         info_url = 'http://www.viki.com/player5_fragment/%s?action=show&controller=videos' % video_id
57         info_webpage = self._download_webpage(
58             info_url, video_id, note=u'Downloading info page')
59         if re.match(r'\s*<div\s+class="video-error', info_webpage):
60             raise ExtractorError(
61                 u'Video %s is blocked from your location.' % video_id,
62                 expected=True)
63         video_url = self._html_search_regex(
64             r'<source[^>]+src="([^"]+)"', info_webpage, u'video URL')
65
66         upload_date_str = self._html_search_regex(
67             r'"created_at":"([^"]+)"', info_webpage, u'upload date')
68         upload_date = (
69             unified_strdate(upload_date_str)
70             if upload_date_str is not None
71             else None
72         )
73
74         # subtitles
75         video_subtitles = self.extract_subtitles(video_id, info_webpage)
76         if self._downloader.params.get('listsubtitles', False):
77             self._list_available_subtitles(video_id, info_webpage)
78             return
79
80         return {
81             'id': video_id,
82             'title': title,
83             'url': video_url,
84             'description': description,
85             'thumbnail': thumbnail,
86             'age_limit': age_limit,
87             'uploader': uploader,
88             'subtitles': video_subtitles,
89             'upload_date': upload_date,
90         }
91
92     def _get_available_subtitles(self, video_id, info_webpage):
93         res = {}
94         for sturl in re.findall(r'<track src="([^"]+)"/>'):
95             m = re.search(r'/(?P<lang>[a-z]+)\.vtt', sturl)
96             if not m:
97                 continue
98             res[m.group('lang')] = sturl
99         return res