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',
21 'title': 'Saknar både dusch och avlopp',
22 'description': 'md5:6023a95832a06059832ae93bc3c7efb7',
27 'skip_download': True,
30 'url': 'http://www.kanal9play.se/#!/play/program/335032/video/246042',
31 'only_matching': True,
33 'url': 'http://www.kanal11play.se/#!/play/program/232835958/video/367135199',
34 'only_matching': True,
37 def _fix_subtitles(self, subs):
38 return '\r\n\r\n'.join(
39 '%s\r\n%s --> %s\r\n%s'
42 self._subtitles_timecode(item['startMillis'] / 1000.0),
43 self._subtitles_timecode(item['endMillis'] / 1000.0),
45 ) for num, item in enumerate(subs, 1))
47 def _get_subtitles(self, channel_id, video_id):
48 subs = self._download_json(
49 'http://www.kanal%splay.se/api/subtitles/%s' % (channel_id, video_id),
50 video_id, 'Downloading subtitles JSON', fatal=False)
51 return {'se': [{'ext': 'srt', 'data': self._fix_subtitles(subs)}]} if subs else {}
53 def _real_extract(self, url):
54 mobj = re.match(self._VALID_URL, url)
55 video_id = mobj.group('id')
56 channel_id = mobj.group('channel_id')
58 video = self._download_json(
59 'http://www.kanal%splay.se/api/getVideo?format=FLASH&videoId=%s' % (channel_id, video_id),
62 reasons_for_no_streams = video.get('reasonsForNoStreams')
63 if reasons_for_no_streams:
65 '%s returned error: %s' % (self.IE_NAME, '\n'.join(reasons_for_no_streams)),
68 title = video['title']
69 description = video.get('description')
70 duration = float_or_none(video.get('length'), 1000)
71 thumbnail = video.get('posterUrl')
73 stream_base_url = video['streamBaseUrl']
76 'url': stream_base_url,
77 'play_path': stream['source'],
79 'tbr': float_or_none(stream.get('bitrate'), 1000),
80 'rtmp_real_time': True,
81 } for stream in video['streams']]
82 self._sort_formats(formats)
85 if video.get('hasSubtitle'):
86 subtitles = self.extract_subtitles(channel_id, video_id)
91 'description': description,
92 'thumbnail': thumbnail,
95 'subtitles': subtitles,