]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/polskieradio.py
[tiktok] Fix `extractor_key` used in archive
[yt-dlp.git] / yt_dlp / extractor / polskieradio.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import json
6 import math
7 import re
8
9 from .common import InfoExtractor
10 from ..compat import (
11 compat_str,
12 compat_urllib_parse_unquote,
13 compat_urlparse
14 )
15 from ..utils import (
16 extract_attributes,
17 ExtractorError,
18 InAdvancePagedList,
19 int_or_none,
20 js_to_json,
21 parse_iso8601,
22 strip_or_none,
23 unified_timestamp,
24 unescapeHTML,
25 url_or_none,
26 )
27
28
29 class PolskieRadioBaseExtractor(InfoExtractor):
30 def _extract_webpage_player_entries(self, webpage, playlist_id, base_data):
31 media_urls = set()
32
33 for data_media in re.findall(r'<[^>]+data-media="?({[^>]+})"?', webpage):
34 media = self._parse_json(data_media, playlist_id, transform_source=unescapeHTML, fatal=False)
35 if not media.get('file') or not media.get('desc'):
36 continue
37 media_url = self._proto_relative_url(media['file'])
38 if media_url in media_urls:
39 continue
40 media_urls.add(media_url)
41 entry = base_data.copy()
42 entry.update({
43 'id': compat_str(media['id']),
44 'url': media_url,
45 'duration': int_or_none(media.get('length')),
46 'vcodec': 'none' if media.get('provider') == 'audio' else None,
47 })
48 entry_title = compat_urllib_parse_unquote(media['desc'])
49 if entry_title:
50 entry['title'] = entry_title
51 yield entry
52
53
54 class PolskieRadioIE(PolskieRadioBaseExtractor):
55 _VALID_URL = r'https?://(?:www\.)?polskieradio(?:24)?\.pl/\d+/\d+/Artykul/(?P<id>[0-9]+)'
56 _TESTS = [{ # Old-style single broadcast.
57 'url': 'http://www.polskieradio.pl/7/5102/Artykul/1587943,Prof-Andrzej-Nowak-o-historii-nie-da-sie-myslec-beznamietnie',
58 'info_dict': {
59 'id': '1587943',
60 'title': 'Prof. Andrzej Nowak: o historii nie da się myśleć beznamiętnie',
61 'description': 'md5:12f954edbf3120c5e7075e17bf9fc5c5',
62 },
63 'playlist': [{
64 'md5': '2984ee6ce9046d91fc233bc1a864a09a',
65 'info_dict': {
66 'id': '1540576',
67 'ext': 'mp3',
68 'title': 'md5:d4623290d4ac983bf924061c75c23a0d',
69 'timestamp': 1456594200,
70 'upload_date': '20160227',
71 'duration': 2364,
72 'thumbnail': r're:^https?://static\.prsa\.pl/images/.*\.jpg$'
73 },
74 }],
75 }, { # New-style single broadcast.
76 'url': 'https://www.polskieradio.pl/8/2382/Artykul/2534482,Zagarysci-Poezja-jak-spoiwo',
77 'info_dict': {
78 'id': '2534482',
79 'title': 'Żagaryści. Poezja jak spoiwo',
80 'description': 'md5:f18d95d5dcba747a09b635e21a4c0695',
81 },
82 'playlist': [{
83 'md5': 'd07559829f61d5a93a75755987ded760',
84 'info_dict': {
85 'id': '2516679',
86 'ext': 'mp3',
87 'title': 'md5:c6e1234e0b747ad883cb91b7ad06b98c',
88 'timestamp': 1592654400,
89 'upload_date': '20200620',
90 'duration': 1430,
91 'thumbnail': r're:^https?://static\.prsa\.pl/images/.*\.jpg$'
92 },
93 }],
94 }, {
95 # PR4 audition - other frontend
96 'url': 'https://www.polskieradio.pl/10/6071/Artykul/2610977,Poglos-29-pazdziernika-godz-2301',
97 'info_dict': {
98 'id': '2610977',
99 'ext': 'mp3',
100 'title': 'Pogłos 29 października godz. 23:01',
101 },
102 }, {
103 'url': 'http://polskieradio.pl/9/305/Artykul/1632955,Bardzo-popularne-slowo-remis',
104 'only_matching': True,
105 }, {
106 'url': 'http://www.polskieradio.pl/7/5102/Artykul/1587943',
107 'only_matching': True,
108 }, {
109 # with mp4 video
110 'url': 'http://www.polskieradio.pl/9/299/Artykul/1634903,Brexit-Leszek-Miller-swiat-sie-nie-zawali-Europa-bedzie-trwac-dalej',
111 'only_matching': True,
112 }, {
113 'url': 'https://polskieradio24.pl/130/4503/Artykul/2621876,Narusza-nasza-suwerennosc-Publicysci-o-uzaleznieniu-funduszy-UE-od-praworzadnosci',
114 'only_matching': True,
115 }]
116
117 def _real_extract(self, url):
118 playlist_id = self._match_id(url)
119
120 webpage = self._download_webpage(url, playlist_id)
121
122 content = self._search_regex(
123 r'(?s)<div[^>]+class="\s*this-article\s*"[^>]*>(.+?)<div[^>]+class="tags"[^>]*>',
124 webpage, 'content', default=None)
125
126 timestamp = unified_timestamp(self._html_search_regex(
127 r'(?s)<span[^>]+id="datetime2"[^>]*>(.+?)</span>',
128 webpage, 'timestamp', default=None))
129
130 thumbnail_url = self._og_search_thumbnail(webpage, default=None)
131
132 title = self._og_search_title(webpage).strip()
133
134 description = strip_or_none(self._og_search_description(webpage, default=None))
135 description = description.replace('\xa0', ' ') if description is not None else None
136
137 if not content:
138 return {
139 'id': playlist_id,
140 'url': self._proto_relative_url(
141 self._search_regex(
142 r"source:\s*'(//static\.prsa\.pl/[^']+)'",
143 webpage, 'audition record url')),
144 'title': title,
145 'description': description,
146 'timestamp': timestamp,
147 'thumbnail': thumbnail_url,
148 }
149
150 entries = self._extract_webpage_player_entries(content, playlist_id, {
151 'title': title,
152 'timestamp': timestamp,
153 'thumbnail': thumbnail_url,
154 })
155
156 return self.playlist_result(entries, playlist_id, title, description)
157
158
159 class PolskieRadioCategoryIE(InfoExtractor):
160 _VALID_URL = r'https?://(?:www\.)?polskieradio\.pl/\d+(?:,[^/]+)?/(?P<id>\d+)'
161 _TESTS = [{
162 'url': 'http://www.polskieradio.pl/7/5102,HISTORIA-ZYWA',
163 'info_dict': {
164 'id': '5102',
165 'title': 'HISTORIA ŻYWA',
166 },
167 'playlist_mincount': 38,
168 }, {
169 'url': 'http://www.polskieradio.pl/7/4807',
170 'info_dict': {
171 'id': '4807',
172 'title': 'Vademecum 1050. rocznicy Chrztu Polski'
173 },
174 'playlist_mincount': 5
175 }, {
176 'url': 'http://www.polskieradio.pl/7/129,Sygnaly-dnia?ref=source',
177 'only_matching': True
178 }, {
179 'url': 'http://www.polskieradio.pl/37,RedakcjaKatolicka/4143,Kierunek-Krakow',
180 'info_dict': {
181 'id': '4143',
182 'title': 'Kierunek Kraków',
183 },
184 'playlist_mincount': 61
185 }, {
186 'url': 'http://www.polskieradio.pl/10,czworka/214,muzyka',
187 'info_dict': {
188 'id': '214',
189 'title': 'Muzyka',
190 },
191 'playlist_mincount': 61
192 }, {
193 'url': 'http://www.polskieradio.pl/7,Jedynka/5102,HISTORIA-ZYWA',
194 'only_matching': True,
195 }, {
196 'url': 'http://www.polskieradio.pl/8,Dwojka/196,Publicystyka',
197 'only_matching': True,
198 }]
199
200 @classmethod
201 def suitable(cls, url):
202 return False if PolskieRadioIE.suitable(url) else super(PolskieRadioCategoryIE, cls).suitable(url)
203
204 def _entries(self, url, page, category_id):
205 content = page
206 for page_num in itertools.count(2):
207 for a_entry, entry_id in re.findall(
208 r'(?s)<article[^>]+>.*?(<a[^>]+href=["\']/\d+/\d+/Artykul/(\d+)[^>]+>).*?</article>',
209 content):
210 entry = extract_attributes(a_entry)
211 href = entry.get('href')
212 if not href:
213 continue
214 yield self.url_result(
215 compat_urlparse.urljoin(url, href), PolskieRadioIE.ie_key(),
216 entry_id, entry.get('title'))
217 mobj = re.search(
218 r'<div[^>]+class=["\']next["\'][^>]*>\s*<a[^>]+href=(["\'])(?P<url>(?:(?!\1).)+)\1',
219 content)
220 if not mobj:
221 break
222 next_url = compat_urlparse.urljoin(url, mobj.group('url'))
223 content = self._download_webpage(
224 next_url, category_id, 'Downloading page %s' % page_num)
225
226 def _real_extract(self, url):
227 category_id = self._match_id(url)
228 webpage = self._download_webpage(url, category_id)
229 title = self._html_search_regex(
230 r'<title>([^<]+) - [^<]+ - [^<]+</title>',
231 webpage, 'title', fatal=False)
232 return self.playlist_result(
233 self._entries(url, webpage, category_id),
234 category_id, title)
235
236
237 class PolskieRadioPlayerIE(InfoExtractor):
238 IE_NAME = 'polskieradio:player'
239 _VALID_URL = r'https?://player\.polskieradio\.pl/anteny/(?P<id>[^/]+)'
240
241 _BASE_URL = 'https://player.polskieradio.pl'
242 _PLAYER_URL = 'https://player.polskieradio.pl/main.bundle.js'
243 _STATIONS_API_URL = 'https://apipr.polskieradio.pl/api/stacje'
244
245 _TESTS = [{
246 'url': 'https://player.polskieradio.pl/anteny/trojka',
247 'info_dict': {
248 'id': '3',
249 'ext': 'm4a',
250 'title': 'Trójka',
251 },
252 'params': {
253 'format': 'bestaudio',
254 'skip_download': 'endless stream',
255 },
256 }]
257
258 def _get_channel_list(self, channel_url='no_channel'):
259 player_code = self._download_webpage(
260 self._PLAYER_URL, channel_url,
261 note='Downloading js player')
262 channel_list = js_to_json(self._search_regex(
263 r';var r="anteny",a=(\[.+?\])},', player_code, 'channel list'))
264 return self._parse_json(channel_list, channel_url)
265
266 def _real_extract(self, url):
267 channel_url = self._match_id(url)
268 channel_list = self._get_channel_list(channel_url)
269
270 channel = next((c for c in channel_list if c.get('url') == channel_url), None)
271
272 if not channel:
273 raise ExtractorError('Channel not found')
274
275 station_list = self._download_json(self._STATIONS_API_URL, channel_url,
276 note='Downloading stream url list',
277 headers={
278 'Accept': 'application/json',
279 'Referer': url,
280 'Origin': self._BASE_URL,
281 })
282 station = next((s for s in station_list
283 if s.get('Name') == (channel.get('streamName') or channel.get('name'))), None)
284 if not station:
285 raise ExtractorError('Station not found even though we extracted channel')
286
287 formats = []
288 for stream_url in station['Streams']:
289 stream_url = self._proto_relative_url(stream_url)
290 if stream_url.endswith('/playlist.m3u8'):
291 formats.extend(self._extract_m3u8_formats(stream_url, channel_url, live=True))
292 elif stream_url.endswith('/manifest.f4m'):
293 formats.extend(self._extract_mpd_formats(stream_url, channel_url))
294 elif stream_url.endswith('/Manifest'):
295 formats.extend(self._extract_ism_formats(stream_url, channel_url))
296 else:
297 formats.append({
298 'url': stream_url,
299 })
300
301 self._sort_formats(formats)
302
303 return {
304 'id': compat_str(channel['id']),
305 'formats': formats,
306 'title': channel.get('name') or channel.get('streamName'),
307 'display_id': channel_url,
308 'thumbnail': f'{self._BASE_URL}/images/{channel_url}-color-logo.png',
309 'is_live': True,
310 }
311
312
313 class PolskieRadioPodcastBaseExtractor(InfoExtractor):
314 _API_BASE = 'https://apipodcasts.polskieradio.pl/api'
315
316 def _parse_episode(self, data):
317 return {
318 'id': data['guid'],
319 'formats': [{
320 'url': data['url'],
321 'filesize': int_or_none(data.get('fileSize')),
322 }],
323 'title': data['title'],
324 'description': data.get('description'),
325 'duration': int_or_none(data.get('length')),
326 'timestamp': parse_iso8601(data.get('publishDate')),
327 'thumbnail': url_or_none(data.get('image')),
328 'series': data.get('podcastTitle'),
329 'episode': data['title'],
330 }
331
332
333 class PolskieRadioPodcastListIE(PolskieRadioPodcastBaseExtractor):
334 IE_NAME = 'polskieradio:podcast:list'
335 _VALID_URL = r'https?://podcasty\.polskieradio\.pl/podcast/(?P<id>\d+)'
336 _TESTS = [{
337 'url': 'https://podcasty.polskieradio.pl/podcast/8/',
338 'info_dict': {
339 'id': '8',
340 'title': 'Śniadanie w Trójce',
341 'description': 'md5:57abcc27bc4c6a6b25baa3061975b9ef',
342 'uploader': 'Beata Michniewicz',
343 },
344 'playlist_mincount': 714,
345 }]
346 _PAGE_SIZE = 10
347
348 def _call_api(self, podcast_id, page):
349 return self._download_json(
350 f'{self._API_BASE}/Podcasts/{podcast_id}/?pageSize={self._PAGE_SIZE}&page={page}',
351 podcast_id, f'Downloading page {page}')
352
353 def _real_extract(self, url):
354 podcast_id = self._match_id(url)
355 data = self._call_api(podcast_id, 1)
356
357 def get_page(page_num):
358 page_data = self._call_api(podcast_id, page_num + 1) if page_num else data
359 yield from (self._parse_episode(ep) for ep in page_data['items'])
360
361 return {
362 '_type': 'playlist',
363 'entries': InAdvancePagedList(
364 get_page, math.ceil(data['itemCount'] / self._PAGE_SIZE), self._PAGE_SIZE),
365 'id': str(data['id']),
366 'title': data['title'],
367 'description': data.get('description'),
368 'uploader': data.get('announcer'),
369 }
370
371
372 class PolskieRadioPodcastIE(PolskieRadioPodcastBaseExtractor):
373 IE_NAME = 'polskieradio:podcast'
374 _VALID_URL = r'https?://podcasty\.polskieradio\.pl/track/(?P<id>[a-f\d]{8}(?:-[a-f\d]{4}){4}[a-f\d]{8})'
375 _TESTS = [{
376 'url': 'https://podcasty.polskieradio.pl/track/6eafe403-cb8f-4756-b896-4455c3713c32',
377 'info_dict': {
378 'id': '6eafe403-cb8f-4756-b896-4455c3713c32',
379 'ext': 'mp3',
380 'title': 'Theresa May rezygnuje. Co dalej z brexitem?',
381 'description': 'md5:e41c409a29d022b70ef0faa61dbded60',
382 },
383 }]
384
385 def _real_extract(self, url):
386 podcast_id = self._match_id(url)
387 data = self._download_json(
388 f'{self._API_BASE}/audio',
389 podcast_id, 'Downloading podcast metadata',
390 data=json.dumps({
391 'guids': [podcast_id],
392 }).encode('utf-8'),
393 headers={
394 'Content-Type': 'application/json',
395 })
396 return self._parse_episode(data[0])
397
398
399 class PolskieRadioRadioKierowcowIE(PolskieRadioBaseExtractor):
400 _VALID_URL = r'https?://(?:www\.)?radiokierowcow\.pl/artykul/(?P<id>[0-9]+)'
401 IE_NAME = 'polskieradio:kierowcow'
402
403 _TESTS = [{
404 'url': 'https://radiokierowcow.pl/artykul/2694529',
405 'info_dict': {
406 'id': '2694529',
407 'title': 'Zielona fala reliktem przeszłości?',
408 'description': 'md5:343950a8717c9818fdfd4bd2b8ca9ff2',
409 },
410 'playlist_count': 3,
411 }]
412
413 def _real_extract(self, url):
414 media_id = self._match_id(url)
415 webpage = self._download_webpage(url, media_id)
416 nextjs_build = self._search_nextjs_data(webpage, media_id)['buildId']
417 article = self._download_json(
418 f'https://radiokierowcow.pl/_next/data/{nextjs_build}/artykul/{media_id}.json?articleId={media_id}',
419 media_id)
420 data = article['pageProps']['data']
421 title = data['title']
422 entries = self._extract_webpage_player_entries(data['content'], media_id, {
423 'title': title,
424 })
425
426 return {
427 '_type': 'playlist',
428 'id': media_id,
429 'entries': entries,
430 'title': title,
431 'description': data.get('lead'),
432 }