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