2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
8 compat_urllib_parse_unquote
,
9 compat_urllib_parse_urlparse
,
21 class CeskaTelevizeIE(InfoExtractor
):
22 _VALID_URL
= r
'https?://(?:www\.)?ceskatelevize\.cz/(?:ivysilani|porady)/(?:[^/?#&]+/)*(?P<id>[^/#?]+)'
24 'url': 'http://www.ceskatelevize.cz/ivysilani/10441294653-hyde-park-civilizace/215411058090502/bonus/20641-bonus-01-en',
26 'id': '61924494877028507',
28 'title': 'Hyde Park Civilizace: Bonus 01 - En',
29 'description': 'English Subtittles',
30 'thumbnail': r
're:^https?://.*\.jpg',
35 'skip_download': True,
39 'url': 'http://www.ceskatelevize.cz/ivysilani/zive/ct4/',
43 'title': r
're:^ČT Sport \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
48 'skip_download': True,
50 'skip': 'Georestricted to Czech Republic',
52 'url': 'http://www.ceskatelevize.cz/ivysilani/embed/iFramePlayer.php?hash=d6a3e1370d2e4fa76296b90bad4dfc19673b641e&IDEC=217 562 22150/0004&channelID=1&width=100%25',
53 'only_matching': True,
55 # video with 18+ caution trailer
56 'url': 'http://www.ceskatelevize.cz/porady/10520528904-queer/215562210900007-bogotart/',
58 'id': '215562210900007-bogotart',
59 'title': 'Queer: Bogotart',
60 'description': 'Hlavní město Kolumbie v doprovodu queer umělců. Vroucí svět plný vášně, sebevědomí, ale i násilí a bolesti. Připravil Peter Serge Butko',
64 'id': '61924494877311053',
66 'title': 'Queer: Bogotart (Varování 18+)',
71 'id': '61924494877068022',
73 'title': 'Queer: Bogotart (Queer)',
74 'thumbnail': r
're:^https?://.*\.jpg',
80 'skip_download': True,
84 'url': 'http://www.ceskatelevize.cz/porady/10614999031-neviditelni/21251212048/',
85 'only_matching': True,
88 def _real_extract(self
, url
):
89 playlist_id
= self
._match
_id
(url
)
90 parsed_url
= compat_urllib_parse_urlparse(url
)
91 webpage
= self
._download
_webpage
(url
, playlist_id
)
92 site_name
= self
._og
_search
_property
('site_name', webpage
, fatal
=False, default
=None)
93 playlist_title
= self
._og
_search
_title
(webpage
, default
=None)
94 if site_name
and playlist_title
:
95 playlist_title
= playlist_title
.replace(f
' — {site_name}', '', 1)
96 playlist_description
= self
._og
_search
_description
(webpage
, default
=None)
97 if playlist_description
:
98 playlist_description
= playlist_description
.replace('\xa0', ' ')
100 if parsed_url
.path
.startswith('/porady/'):
101 next_data
= self
._search
_nextjs
_data
(webpage
, playlist_id
)
102 idec
= traverse_obj(next_data
, ('props', 'pageProps', 'data', ('show', 'mediaMeta'), 'idec'), get_all
=False)
104 raise ExtractorError('Failed to find IDEC id')
105 iframe_hash
= self
._download
_webpage
('https://www.ceskatelevize.cz/v-api/iframe-hash/', playlist_id
)
106 webpage
= self
._download
_webpage
('https://www.ceskatelevize.cz/ivysilani/embed/iFramePlayer.php', playlist_id
,
107 query
={'hash': iframe_hash, 'origin': 'iVysilani', 'autoStart': 'true', 'IDEC': idec}
)
109 NOT_AVAILABLE_STRING
= 'This content is not available at your territory due to limited copyright.'
110 if '%s</p>' % NOT_AVAILABLE_STRING
in webpage
:
111 raise ExtractorError(NOT_AVAILABLE_STRING
, expected
=True)
116 playlist
= self
._parse
_json
(
118 r
'getPlaylistUrl\(\[({.+?})\]', webpage
, 'playlist',
119 default
='{}'), playlist_id
)
121 type_
= playlist
.get('type')
122 episode_id
= playlist
.get('id')
125 type_
= self
._html
_search
_regex
(
126 r
'getPlaylistUrl\(\[\{"type":"(.+?)","id":".+?"\}\],',
129 episode_id
= self
._html
_search
_regex
(
130 r
'getPlaylistUrl\(\[\{"type":".+?","id":"(.+?)"\}\],',
131 webpage
, 'episode_id')
134 'playlist[0][type]': type_
,
135 'playlist[0][id]': episode_id
,
136 'requestUrl': parsed_url
.path
,
137 'requestSource': 'iVysilani',
142 for user_agent
in (None, USER_AGENTS
['Safari']):
143 req
= sanitized_Request(
144 'https://www.ceskatelevize.cz/ivysilani/ajax/get-client-playlist/',
145 data
=urlencode_postdata(data
))
147 req
.add_header('Content-type', 'application/x-www-form-urlencoded')
148 req
.add_header('x-addr', '127.0.0.1')
149 req
.add_header('X-Requested-With', 'XMLHttpRequest')
151 req
.add_header('User-Agent', user_agent
)
152 req
.add_header('Referer', url
)
154 playlistpage
= self
._download
_json
(req
, playlist_id
, fatal
=False)
159 playlist_url
= playlistpage
['url']
160 if playlist_url
== 'error_region':
161 raise ExtractorError(NOT_AVAILABLE_STRING
, expected
=True)
163 req
= sanitized_Request(compat_urllib_parse_unquote(playlist_url
))
164 req
.add_header('Referer', url
)
166 playlist
= self
._download
_json
(req
, playlist_id
, fatal
=False)
170 playlist
= playlist
.get('playlist')
171 if not isinstance(playlist
, list):
174 playlist_len
= len(playlist
)
176 for num
, item
in enumerate(playlist
):
177 is_live
= item
.get('type') == 'LIVE'
179 for format_id
, stream_url
in item
.get('streamUrls', {}).items():
180 stream_url
= stream_url
.replace('https://', 'http://')
181 if 'playerType=flash' in stream_url
:
182 stream_formats
= self
._extract
_m
3u8_formats
(
183 stream_url
, playlist_id
, 'mp4', 'm3u8_native',
184 m3u8_id
='hls-%s' % format_id
, fatal
=False)
186 stream_formats
= self
._extract
_mpd
_formats
(
187 stream_url
, playlist_id
,
188 mpd_id
='dash-%s' % format_id
, fatal
=False)
189 if 'drmOnly=true' in stream_url
:
190 for f
in stream_formats
:
192 # See https://github.com/ytdl-org/youtube-dl/issues/12119#issuecomment-280037031
193 if format_id
== 'audioDescription':
194 for f
in stream_formats
:
195 f
['source_preference'] = -10
196 formats
.extend(stream_formats
)
198 if user_agent
and len(entries
) == playlist_len
:
199 entries
[num
]['formats'].extend(formats
)
202 item_id
= item
.get('id') or item
['assetId']
203 title
= item
['title']
205 duration
= float_or_none(item
.get('duration'))
206 thumbnail
= item
.get('previewImageUrl')
209 if item
.get('type') == 'VOD':
210 subs
= item
.get('subtitles')
212 subtitles
= self
.extract_subtitles(episode_id
, subs
)
214 if playlist_len
== 1:
215 final_title
= playlist_title
or title
217 final_title
= '%s (%s)' % (playlist_title
, title
)
221 'title': final_title
,
222 'description': playlist_description
if playlist_len
== 1 else None,
223 'thumbnail': thumbnail
,
224 'duration': duration
,
226 'subtitles': subtitles
,
231 self
._sort
_formats
(e
['formats'])
233 return self
.playlist_result(entries
, playlist_id
, playlist_title
, playlist_description
)
235 def _get_subtitles(self
, episode_id
, subs
):
236 original_subtitles
= self
._download
_webpage
(
237 subs
[0]['url'], episode_id
, 'Downloading subtitles')
238 srt_subs
= self
._fix
_subtitles
(original_subtitles
)
247 def _fix_subtitles(subtitles
):
248 """ Convert millisecond-based subtitles to SRT """
250 def _msectotimecode(msec
):
251 """ Helper utility to convert milliseconds to timecode """
253 for divider
in [1000, 60, 60, 100]:
254 components
.append(msec
% divider
)
256 return '{3:02}:{2:02}:{1:02},{0:03}'.format(*components
)
258 def _fix_subtitle(subtitle
):
259 for line
in subtitle
.splitlines():
260 m
= re
.match(r
'^\s*([0-9]+);\s*([0-9]+)\s+([0-9]+)\s*$', line
)
263 start
, stop
= (_msectotimecode(int(t
)) for t
in m
.groups()[1:])
264 yield '{0} --> {1}'.format(start
, stop
)
268 return '\r\n'.join(_fix_subtitle(subtitles
))