[kanalplay] Extract subtitles
[youtube-dl] / youtube_dl / extractor / kanalplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     float_or_none,
10 )
11
12
13 class KanalPlayIE(InfoExtractor):
14     IE_DESC = 'Kanal 5/9/11 Play'
15     _VALID_URL = r'https?://(?:www\.)?kanal(?P<channel_id>5|9|11)play\.se/(?:#!/)?(?:play/)?program/\d+/video/(?P<id>\d+)'
16     _TESTS = [{
17         'url': 'http://www.kanal5play.se/#!/play/program/3060212363/video/3270012277',
18         'md5': '',
19         'info_dict': {
20             'id': '2609989',
21             'ext': 'flv',
22             'title': 'Saknar både dusch och avlopp',
23             'description': 'md5:',
24             'duration': 2636.36,
25         },
26     }, {
27         'url': 'http://www.kanal9play.se/#!/play/program/335032/video/246042',
28         'only_matching': True,
29     }, {
30         'url': 'http://www.kanal11play.se/#!/play/program/232835958/video/367135199',
31         'only_matching': True,
32     }]
33
34     def _fix_subtitles(self, subs):
35         return '\r\n\r\n'.join(
36             '%s\r\n%s --> %s\r\n%s'
37             % (
38                 num,
39                 self._subtitles_timecode(item['startMillis'] / 1000.0),
40                 self._subtitles_timecode(item['endMillis'] / 1000.0),
41                 item['text'],
42             ) for num, item in enumerate(subs, 1))
43
44     def _get_subtitles(self, channel_id, video_id):
45         subs = self._download_json(
46             'http://www.kanal%splay.se/api/subtitles/%s' % (channel_id, video_id),
47             video_id, 'Downloading subtitles JSON', fatal=False)
48         return {'se': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]} if subs else {}
49
50     def _real_extract(self, url):
51         mobj = re.match(self._VALID_URL, url)
52         video_id = mobj.group('id')
53         channel_id = mobj.group('channel_id')
54
55         video = self._download_json(
56             'http://www.kanal%splay.se/api/getVideo?format=FLASH&videoId=%s' % (channel_id, video_id),
57             video_id)
58
59         reasons_for_no_streams = video.get('reasonsForNoStreams')
60         if reasons_for_no_streams:
61             raise ExtractorError(
62                 '%s returned error: %s' % (self.IE_NAME, '\n'.join(reasons_for_no_streams)),
63                 expected=True)
64
65         title = video['title']
66         description = video.get('description')
67         duration = float_or_none(video.get('length'), 1000)
68         thumbnail = video.get('posterUrl')
69
70         stream_base_url = video['streamBaseUrl']
71
72         formats = [{
73             'url': stream_base_url,
74             'play_path': stream['source'],
75             'ext': 'flv',
76             'tbr': float_or_none(stream.get('bitrate'), 1000),
77             'rtmp_real_time': True,
78         } for stream in video['streams']]
79         self._sort_formats(formats)
80
81         subtitles = {}
82         if video.get('hasSubtitle'):
83             subtitles = self.extract_subtitles(channel_id, video_id)
84
85         return {
86             'id': video_id,
87             'title': title,
88             'description': description,
89             'thumbnail': thumbnail,
90             'duration': duration,
91             'formats': formats,
92             'subtitles': subtitles,
93         }