]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/rozhlas.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / rozhlas.py
1 import itertools
2
3 from .common import InfoExtractor
4 from ..networking.exceptions import HTTPError
5 from ..utils import (
6 ExtractorError,
7 extract_attributes,
8 int_or_none,
9 remove_start,
10 str_or_none,
11 traverse_obj,
12 unified_timestamp,
13 url_or_none,
14 )
15
16
17 class RozhlasIE(InfoExtractor):
18 _VALID_URL = r'https?://(?:www\.)?prehravac\.rozhlas\.cz/audio/(?P<id>[0-9]+)'
19 _TESTS = [{
20 'url': 'http://prehravac.rozhlas.cz/audio/3421320',
21 'md5': '504c902dbc9e9a1fd50326eccf02a7e2',
22 'info_dict': {
23 'id': '3421320',
24 'ext': 'mp3',
25 'title': 'Echo Pavla Klusáka (30.06.2015 21:00)',
26 'description': 'Osmdesátiny Terryho Rileyho jsou skvělou příležitostí proletět se elektronickými i akustickými díly zakladatatele minimalismu, který je aktivní už přes padesát let',
27 },
28 }, {
29 'url': 'http://prehravac.rozhlas.cz/audio/3421320/embed',
30 'only_matching': True,
31 }]
32
33 def _real_extract(self, url):
34 audio_id = self._match_id(url)
35
36 webpage = self._download_webpage(
37 f'http://prehravac.rozhlas.cz/audio/{audio_id}', audio_id)
38
39 title = self._html_search_regex(
40 r'<h3>(.+?)</h3>\s*<p[^>]*>.*?</p>\s*<div[^>]+id=["\']player-track',
41 webpage, 'title', default=None) or remove_start(
42 self._og_search_title(webpage), 'Radio Wave - ')
43 description = self._html_search_regex(
44 r'<p[^>]+title=(["\'])(?P<url>(?:(?!\1).)+)\1[^>]*>.*?</p>\s*<div[^>]+id=["\']player-track',
45 webpage, 'description', fatal=False, group='url')
46 duration = int_or_none(self._search_regex(
47 r'data-duration=["\'](\d+)', webpage, 'duration', default=None))
48
49 return {
50 'id': audio_id,
51 'url': f'http://media.rozhlas.cz/_audio/{audio_id}.mp3',
52 'title': title,
53 'description': description,
54 'duration': duration,
55 'vcodec': 'none',
56 }
57
58
59 class RozhlasBaseIE(InfoExtractor):
60 def _extract_formats(self, entry, audio_id):
61 formats = []
62 for audio in traverse_obj(entry, ('audioLinks', lambda _, v: url_or_none(v['url']))):
63 ext = audio.get('variant')
64 for retry in self.RetryManager():
65 if retry.attempt > 1:
66 self._sleep(1, audio_id)
67 try:
68 if ext == 'dash':
69 formats.extend(self._extract_mpd_formats(
70 audio['url'], audio_id, mpd_id=ext))
71 elif ext == 'hls':
72 formats.extend(self._extract_m3u8_formats(
73 audio['url'], audio_id, 'm4a', m3u8_id=ext))
74 else:
75 formats.append({
76 'url': audio['url'],
77 'ext': ext,
78 'format_id': ext,
79 'abr': int_or_none(audio.get('bitrate')),
80 'acodec': ext,
81 'vcodec': 'none',
82 })
83 except ExtractorError as e:
84 if isinstance(e.cause, HTTPError) and e.cause.status == 429:
85 retry.error = e.cause
86 else:
87 self.report_warning(e.msg)
88
89 return formats
90
91
92 class RozhlasVltavaIE(RozhlasBaseIE):
93 _VALID_URL = r'https?://(?:\w+\.rozhlas|english\.radio)\.cz/[\w-]+-(?P<id>\d+)'
94 _TESTS = [{
95 'url': 'https://wave.rozhlas.cz/papej-masicko-porcujeme-a-bilancujeme-filmy-a-serialy-ktere-letos-zabily-8891337',
96 'md5': 'ba2fdbc1242fc16771c7695d271ec355',
97 'info_dict': {
98 'id': '8891337',
99 'title': 'md5:21f99739d04ab49d8c189ec711eef4ec',
100 },
101 'playlist_count': 1,
102 'playlist': [{
103 'md5': 'ba2fdbc1242fc16771c7695d271ec355',
104 'info_dict': {
105 'id': '10520988',
106 'ext': 'mp3',
107 'title': 'Papej masíčko! Porcujeme a bilancujeme filmy a seriály, které to letos zabily',
108 'description': 'md5:1c6d29fb9564e1f17fc1bb83ae7da0bc',
109 'duration': 1574,
110 'artist': 'Aleš Stuchlý',
111 'channel_id': 'radio-wave',
112 },
113 }],
114 }, {
115 'url': 'https://wave.rozhlas.cz/poslechnete-si-neklid-podcastovy-thriller-o-vine-strachu-a-vztahu-ktery-zasel-8554744',
116 'info_dict': {
117 'id': '8554744',
118 'title': 'Poslechněte si Neklid. Podcastový thriller o vině, strachu a vztahu, který zašel příliš daleko',
119 },
120 'playlist_count': 5,
121 'playlist': [{
122 'md5': '93d4109cf8f40523699ae9c1d4600bdd',
123 'info_dict': {
124 'id': '9890713',
125 'ext': 'mp3',
126 'title': 'Neklid #1',
127 'description': '1. díl: Neklid: 1. díl',
128 'duration': 1025,
129 'artist': 'Josef Kokta',
130 'channel_id': 'radio-wave',
131 'chapter': 'Neklid #1',
132 'chapter_number': 1,
133 },
134 }, {
135 'md5': 'e9763235be4a6dcf94bc8a5bac1ca126',
136 'info_dict': {
137 'id': '9890716',
138 'ext': 'mp3',
139 'title': 'Neklid #2',
140 'description': '2. díl: Neklid: 2. díl',
141 'duration': 768,
142 'artist': 'Josef Kokta',
143 'channel_id': 'radio-wave',
144 'chapter': 'Neklid #2',
145 'chapter_number': 2,
146 },
147 }, {
148 'md5': '00b642ea94b78cc949ac84da09f87895',
149 'info_dict': {
150 'id': '9890722',
151 'ext': 'mp3',
152 'title': 'Neklid #3',
153 'description': '3. díl: Neklid: 3. díl',
154 'duration': 607,
155 'artist': 'Josef Kokta',
156 'channel_id': 'radio-wave',
157 'chapter': 'Neklid #3',
158 'chapter_number': 3,
159 },
160 }, {
161 'md5': 'faef97b1b49da7df874740f118c19dea',
162 'info_dict': {
163 'id': '9890728',
164 'ext': 'mp3',
165 'title': 'Neklid #4',
166 'description': '4. díl: Neklid: 4. díl',
167 'duration': 621,
168 'artist': 'Josef Kokta',
169 'channel_id': 'radio-wave',
170 'chapter': 'Neklid #4',
171 'chapter_number': 4,
172 },
173 }, {
174 'md5': '6e729fa39b647325b868d419c76f3efa',
175 'info_dict': {
176 'id': '9890734',
177 'ext': 'mp3',
178 'title': 'Neklid #5',
179 'description': '5. díl: Neklid: 5. díl',
180 'duration': 908,
181 'artist': 'Josef Kokta',
182 'channel_id': 'radio-wave',
183 'chapter': 'Neklid #5',
184 'chapter_number': 5,
185 },
186 }],
187 }, {
188 'url': 'https://dvojka.rozhlas.cz/karel-siktanc-cerny-jezdec-bily-kun-napinava-pohadka-o-tajemnem-prizraku-8946969',
189 'info_dict': {
190 'id': '8946969',
191 'title': 'Karel Šiktanc: Černý jezdec, bílý kůň. Napínavá pohádka o tajemném přízraku',
192 },
193 'playlist_count': 1,
194 'playlist': [{
195 'info_dict': {
196 'id': '10631121',
197 'ext': 'm4a',
198 'title': 'Karel Šiktanc: Černý jezdec, bílý kůň. Napínavá pohádka o tajemném přízraku',
199 'description': 'Karel Šiktanc: Černý jezdec, bílý kůň',
200 'duration': 2656,
201 'artist': 'Tvůrčí skupina Drama a literatura',
202 'channel_id': 'dvojka',
203 },
204 }],
205 'params': {'skip_download': 'dash'},
206 }]
207
208 def _extract_video(self, entry):
209 audio_id = entry['meta']['ga']['contentId']
210 chapter_number = traverse_obj(entry, ('meta', 'ga', 'contentSerialPart', {int_or_none}))
211
212 return {
213 'id': audio_id,
214 'chapter': traverse_obj(entry, ('meta', 'ga', 'contentNameShort')) if chapter_number else None,
215 'chapter_number': chapter_number,
216 'formats': self._extract_formats(entry, audio_id),
217 **traverse_obj(entry, {
218 'title': ('meta', 'ga', 'contentName'),
219 'description': 'title',
220 'duration': ('duration', {int_or_none}),
221 'artist': ('meta', 'ga', 'contentAuthor'),
222 'channel_id': ('meta', 'ga', 'contentCreator'),
223 }),
224 }
225
226 def _real_extract(self, url):
227 video_id = self._match_id(url)
228 webpage = self._download_webpage(url, video_id)
229
230 # FIXME: Use get_element_text_and_html_by_tag when it accepts less strict html
231 data = self._parse_json(extract_attributes(self._search_regex(
232 r'(<div class="mujRozhlasPlayer" data-player=\'[^\']+\'>)',
233 webpage, 'player'))['data-player'], video_id)['data']
234
235 return {
236 '_type': 'playlist',
237 'id': str_or_none(data.get('embedId')) or video_id,
238 'title': traverse_obj(data, ('series', 'title')),
239 'entries': map(self._extract_video, data['playlist']),
240 }
241
242
243 class MujRozhlasIE(RozhlasBaseIE):
244 _VALID_URL = r'https?://(?:www\.)?mujrozhlas\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)'
245 _TESTS = [{
246 # single episode extraction
247 'url': 'https://www.mujrozhlas.cz/vykopavky/ach-jo-zase-teleci-rizek-je-mnohem-min-cesky-nez-jsme-si-mysleli',
248 'md5': '6f8fd68663e64936623e67c152a669e0',
249 'info_dict': {
250 'id': '10787730',
251 'ext': 'mp3',
252 'title': 'Ach jo, zase to telecí! Řízek je mnohem míň český, než jsme si mysleli',
253 'description': 'md5:db7141e9caaedc9041ec7cefb9a62908',
254 'timestamp': 1684915200,
255 'modified_timestamp': 1687550432,
256 'series': 'Vykopávky',
257 'thumbnail': 'https://portal.rozhlas.cz/sites/default/files/images/84377046610af6ddc54d910b1dd7a22b.jpg',
258 'channel_id': 'radio-wave',
259 'upload_date': '20230524',
260 'modified_date': '20230623',
261 },
262 }, {
263 # serial extraction
264 'url': 'https://www.mujrozhlas.cz/radiokniha/jaroslava-janackova-pribeh-tajemneho-psani-o-pramenech-genezi-babicky',
265 'playlist_mincount': 7,
266 'info_dict': {
267 'id': 'bb2b5f4e-ffb4-35a6-a34a-046aa62d6f6b',
268 'title': 'Jaroslava Janáčková: Příběh tajemného psaní. O pramenech a genezi Babičky',
269 'description': 'md5:7434d8fac39ac9fee6df098e11dfb1be',
270 },
271 }, {
272 # show extraction
273 'url': 'https://www.mujrozhlas.cz/nespavci',
274 'playlist_mincount': 14,
275 'info_dict': {
276 'id': '09db9b37-d0f4-368c-986a-d3439f741f08',
277 'title': 'Nespavci',
278 'description': 'md5:c430adcbf9e2b9eac88b745881e814dc',
279 },
280 }, {
281 # serialPart
282 'url': 'https://www.mujrozhlas.cz/povidka/gustavo-adolfo-becquer-hora-duchu',
283 'info_dict': {
284 'id': '8889035',
285 'ext': 'm4a',
286 'title': 'Gustavo Adolfo Bécquer: Hora duchů',
287 'description': 'md5:343a15257b376c276e210b78e900ffea',
288 'chapter': 'Hora duchů a Polibek – dva tajemné příběhy Gustava Adolfa Bécquera',
289 'thumbnail': 'https://portal.rozhlas.cz/sites/default/files/images/2adfe1387fb140634be725c1ccf26214.jpg',
290 'timestamp': 1708173000,
291 'episode': 'Episode 1',
292 'episode_number': 1,
293 'series': 'Povídka',
294 'modified_date': '20240217',
295 'upload_date': '20240217',
296 'modified_timestamp': 1708173198,
297 'channel_id': 'vltava',
298 },
299 'params': {'skip_download': 'dash'},
300 }]
301
302 def _call_api(self, path, item_id, msg='API JSON'):
303 return self._download_json(
304 f'https://api.mujrozhlas.cz/{path}/{item_id}', item_id,
305 note=f'Downloading {msg}', errnote=f'Failed to download {msg}')['data']
306
307 def _extract_audio_entry(self, entry):
308 audio_id = entry['meta']['ga']['contentId']
309
310 return {
311 'id': audio_id,
312 'formats': self._extract_formats(entry['attributes'], audio_id),
313 **traverse_obj(entry, {
314 'title': ('attributes', 'title'),
315 'description': ('attributes', 'description'),
316 'episode_number': ('attributes', 'part'),
317 'series': ('attributes', 'mirroredShow', 'title'),
318 'chapter': ('attributes', 'mirroredSerial', 'title'),
319 'artist': ('meta', 'ga', 'contentAuthor'),
320 'channel_id': ('meta', 'ga', 'contentCreator'),
321 'timestamp': ('attributes', 'since', {unified_timestamp}),
322 'modified_timestamp': ('attributes', 'updated', {unified_timestamp}),
323 'thumbnail': ('attributes', 'asset', 'url', {url_or_none}),
324 }),
325 }
326
327 def _entries(self, api_url, playlist_id):
328 for page in itertools.count(1):
329 episodes = self._download_json(
330 api_url, playlist_id, note=f'Downloading episodes page {page}',
331 errnote=f'Failed to download episodes page {page}', fatal=False)
332 for episode in traverse_obj(episodes, ('data', lambda _, v: v['meta']['ga']['contentId'])):
333 yield self._extract_audio_entry(episode)
334 api_url = traverse_obj(episodes, ('links', 'next', {url_or_none}))
335 if not api_url:
336 break
337
338 def _real_extract(self, url):
339 display_id = self._match_id(url)
340 webpage = self._download_webpage(url, display_id)
341 info = self._search_json(r'\bvar\s+dl\s*=', webpage, 'info json', display_id)
342
343 entity = info['siteEntityBundle']
344
345 if entity in ('episode', 'serialPart'):
346 return self._extract_audio_entry(self._call_api(
347 'episodes', info['contentId'], 'episode info API JSON'))
348
349 elif entity in ('show', 'serial'):
350 playlist_id = info['contentShow'].split(':')[0] if entity == 'show' else info['contentId']
351 data = self._call_api(f'{entity}s', playlist_id, f'{entity} playlist JSON')
352 api_url = data['relationships']['episodes']['links']['related']
353 return self.playlist_result(
354 self._entries(api_url, playlist_id), playlist_id,
355 **traverse_obj(data, ('attributes', {
356 'title': 'title',
357 'description': 'description',
358 })))
359
360 else:
361 # `entity == 'person'` not implemented yet by API, ref:
362 # https://api.mujrozhlas.cz/persons/8367e456-2a57-379a-91bb-e699619bea49/participation
363 raise ExtractorError(f'Unsupported entity type "{entity}"')