2 from __future__ import unicode_literals
6 from .common import InfoExtractor
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+)'
17 'url': 'http://www.kanal5play.se/#!/play/program/3060212363/video/3270012277',
22 'title': 'Saknar både dusch och avlopp',
23 'description': 'md5:',
27 'url': 'http://www.kanal9play.se/#!/play/program/335032/video/246042',
28 'only_matching': True,
30 'url': 'http://www.kanal11play.se/#!/play/program/232835958/video/367135199',
31 'only_matching': True,
34 def _fix_subtitles(self, subs):
35 return '\r\n\r\n'.join(
36 '%s\r\n%s --> %s\r\n%s'
39 self._subtitles_timecode(item['startMillis'] / 1000.0),
40 self._subtitles_timecode(item['endMillis'] / 1000.0),
42 ) for num, item in enumerate(subs, 1))
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 {}
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')
55 video = self._download_json(
56 'http://www.kanal%splay.se/api/getVideo?format=FLASH&videoId=%s' % (channel_id, video_id),
59 reasons_for_no_streams = video.get('reasonsForNoStreams')
60 if reasons_for_no_streams:
62 '%s returned error: %s' % (self.IE_NAME, '\n'.join(reasons_for_no_streams)),
65 title = video['title']
66 description = video.get('description')
67 duration = float_or_none(video.get('length'), 1000)
68 thumbnail = video.get('posterUrl')
70 stream_base_url = video['streamBaseUrl']
73 'url': stream_base_url,
74 'play_path': stream['source'],
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)
82 if video.get('hasSubtitle'):
83 subtitles = self.extract_subtitles(channel_id, video_id)
88 'description': description,
89 'thumbnail': thumbnail,
92 'subtitles': subtitles,