2 from __future__ import unicode_literals
6 from .common import InfoExtractor
8 compat_urllib_parse_unquote,
9 compat_urllib_parse_urlparse,
20 class CeskaTelevizeIE(InfoExtractor):
21 _VALID_URL = r'https?://(?:www\.)?ceskatelevize\.cz/(porady|ivysilani)/(?:[^/]+/)*(?P<id>[^/#?]+)/*(?:[#?].*)?$'
23 'url': 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220',
25 'id': '61924494877246241',
27 'title': 'Hyde Park Civilizace: Život v Grónsku',
28 'description': 'md5:3fec8f6bb497be5cdb0c9e8781076626',
29 'thumbnail': r're:^https?://.*\.jpg',
34 'skip_download': True,
37 'url': 'http://www.ceskatelevize.cz/ivysilani/10441294653-hyde-park-civilizace/215411058090502/bonus/20641-bonus-01-en',
39 'id': '61924494877028507',
41 'title': 'Hyde Park Civilizace: Bonus 01 - En',
42 'description': 'English Subtittles',
43 'thumbnail': r're:^https?://.*\.jpg',
48 'skip_download': True,
52 'url': 'http://www.ceskatelevize.cz/ivysilani/zive/ct4/',
56 'title': r're:^ČT Sport \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
61 'skip_download': True,
63 'skip': 'Georestricted to Czech Republic',
65 # video with 18+ caution trailer
66 'url': 'http://www.ceskatelevize.cz/porady/10520528904-queer/215562210900007-bogotart/',
68 'id': '215562210900007-bogotart',
69 'title': 'Queer: Bogotart',
70 'description': 'Alternativní průvodce současným queer světem',
74 'id': '61924494876844842',
76 'title': 'Queer: Bogotart (Varování 18+)',
81 'id': '61924494877068022',
83 'title': 'Queer: Bogotart (Queer)',
84 'thumbnail': r're:^https?://.*\.jpg',
90 'skip_download': True,
94 def _real_extract(self, url):
95 url = url.replace('/porady/', '/ivysilani/').replace('/video/', '')
97 mobj = re.match(self._VALID_URL, url)
98 playlist_id = mobj.group('id')
100 webpage = self._download_webpage(url, playlist_id)
102 NOT_AVAILABLE_STRING = 'This content is not available at your territory due to limited copyright.'
103 if '%s</p>' % NOT_AVAILABLE_STRING in webpage:
104 raise ExtractorError(NOT_AVAILABLE_STRING, expected=True)
106 typ = self._html_search_regex(
107 r'getPlaylistUrl\(\[\{"type":"(.+?)","id":".+?"\}\],', webpage, 'type')
108 episode_id = self._html_search_regex(
109 r'getPlaylistUrl\(\[\{"type":".+?","id":"(.+?)"\}\],', webpage, 'episode_id')
112 'playlist[0][type]': typ,
113 'playlist[0][id]': episode_id,
114 'requestUrl': compat_urllib_parse_urlparse(url).path,
115 'requestSource': 'iVysilani',
120 for user_agent in (None, USER_AGENTS['Safari']):
121 req = sanitized_Request(
122 'http://www.ceskatelevize.cz/ivysilani/ajax/get-client-playlist',
123 data=urlencode_postdata(data))
125 req.add_header('Content-type', 'application/x-www-form-urlencoded')
126 req.add_header('x-addr', '127.0.0.1')
127 req.add_header('X-Requested-With', 'XMLHttpRequest')
129 req.add_header('User-Agent', user_agent)
130 req.add_header('Referer', url)
132 playlistpage = self._download_json(req, playlist_id, fatal=False)
137 playlist_url = playlistpage['url']
138 if playlist_url == 'error_region':
139 raise ExtractorError(NOT_AVAILABLE_STRING, expected=True)
141 req = sanitized_Request(compat_urllib_parse_unquote(playlist_url))
142 req.add_header('Referer', url)
144 playlist_title = self._og_search_title(webpage, default=None)
145 playlist_description = self._og_search_description(webpage, default=None)
147 playlist = self._download_json(req, playlist_id, fatal=False)
151 playlist = playlist.get('playlist')
152 if not isinstance(playlist, list):
155 playlist_len = len(playlist)
157 for num, item in enumerate(playlist):
158 is_live = item.get('type') == 'LIVE'
160 for format_id, stream_url in item.get('streamUrls', {}).items():
161 if 'playerType=flash' in stream_url:
162 stream_formats = self._extract_m3u8_formats(
163 stream_url, playlist_id, 'mp4',
164 entry_protocol='m3u8' if is_live else 'm3u8_native',
165 m3u8_id='hls-%s' % format_id, fatal=False)
167 stream_formats = self._extract_mpd_formats(
168 stream_url, playlist_id,
169 mpd_id='dash-%s' % format_id, fatal=False)
170 # See https://github.com/rg3/youtube-dl/issues/12119#issuecomment-280037031
171 if format_id == 'audioDescription':
172 for f in stream_formats:
173 f['source_preference'] = -10
174 formats.extend(stream_formats)
176 if user_agent and len(entries) == playlist_len:
177 entries[num]['formats'].extend(formats)
180 item_id = item.get('id') or item['assetId']
181 title = item['title']
183 duration = float_or_none(item.get('duration'))
184 thumbnail = item.get('previewImageUrl')
187 if item.get('type') == 'VOD':
188 subs = item.get('subtitles')
190 subtitles = self.extract_subtitles(episode_id, subs)
192 if playlist_len == 1:
193 final_title = playlist_title or title
195 final_title = self._live_title(final_title)
197 final_title = '%s (%s)' % (playlist_title, title)
201 'title': final_title,
202 'description': playlist_description if playlist_len == 1 else None,
203 'thumbnail': thumbnail,
204 'duration': duration,
206 'subtitles': subtitles,
211 self._sort_formats(e['formats'])
213 return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)
215 def _get_subtitles(self, episode_id, subs):
216 original_subtitles = self._download_webpage(
217 subs[0]['url'], episode_id, 'Downloading subtitles')
218 srt_subs = self._fix_subtitles(original_subtitles)
227 def _fix_subtitles(subtitles):
228 """ Convert millisecond-based subtitles to SRT """
230 def _msectotimecode(msec):
231 """ Helper utility to convert milliseconds to timecode """
233 for divider in [1000, 60, 60, 100]:
234 components.append(msec % divider)
236 return '{3:02}:{2:02}:{1:02},{0:03}'.format(*components)
238 def _fix_subtitle(subtitle):
239 for line in subtitle.splitlines():
240 m = re.match(r'^\s*([0-9]+);\s*([0-9]+)\s+([0-9]+)\s*$', line)
243 start, stop = (_msectotimecode(int(t)) for t in m.groups()[1:])
244 yield '{0} --> {1}'.format(start, stop)
248 return '\r\n'.join(_fix_subtitle(subtitles))