+
+
+class PolskieRadioPlayerIE(InfoExtractor):
+ IE_NAME = 'polskieradio:player'
+ _VALID_URL = r'https?://player\.polskieradio\.pl/anteny/(?P<id>[^/]+)'
+
+ _BASE_URL = 'https://player.polskieradio.pl'
+ _PLAYER_URL = 'https://player.polskieradio.pl/main.bundle.js'
+ _STATIONS_API_URL = 'https://apipr.polskieradio.pl/api/stacje'
+
+ _TESTS = [{
+ 'url': 'https://player.polskieradio.pl/anteny/trojka',
+ 'info_dict': {
+ 'id': '3',
+ 'ext': 'm4a',
+ 'title': 'Trójka',
+ },
+ 'params': {
+ 'format': 'bestaudio',
+ 'skip_download': 'endless stream',
+ },
+ }]
+
+ def _get_channel_list(self, channel_url='no_channel'):
+ player_code = self._download_webpage(
+ self._PLAYER_URL, channel_url,
+ note='Downloading js player')
+ channel_list = js_to_json(self._search_regex(
+ r';var r="anteny",a=(\[.+?\])},', player_code, 'channel list'))
+ return self._parse_json(channel_list, channel_url)
+
+ def _real_extract(self, url):
+ channel_url = self._match_id(url)
+ channel_list = self._get_channel_list(channel_url)
+
+ channel = next((c for c in channel_list if c.get('url') == channel_url), None)
+
+ if not channel:
+ raise ExtractorError('Channel not found')
+
+ station_list = self._download_json(self._STATIONS_API_URL, channel_url,
+ note='Downloading stream url list',
+ headers={
+ 'Accept': 'application/json',
+ 'Referer': url,
+ 'Origin': self._BASE_URL,
+ })
+ station = next((s for s in station_list
+ if s.get('Name') == (channel.get('streamName') or channel.get('name'))), None)
+ if not station:
+ raise ExtractorError('Station not found even though we extracted channel')
+
+ formats = []
+ for stream_url in station['Streams']:
+ stream_url = self._proto_relative_url(stream_url)
+ if stream_url.endswith('/playlist.m3u8'):
+ formats.extend(self._extract_m3u8_formats(stream_url, channel_url, live=True))
+ elif stream_url.endswith('/manifest.f4m'):
+ formats.extend(self._extract_mpd_formats(stream_url, channel_url))
+ elif stream_url.endswith('/Manifest'):
+ formats.extend(self._extract_ism_formats(stream_url, channel_url))
+ else:
+ formats.append({
+ 'url': stream_url,
+ })
+
+ self._sort_formats(formats)
+
+ return {
+ 'id': compat_str(channel['id']),
+ 'formats': formats,
+ 'title': channel.get('name') or channel.get('streamName'),
+ 'display_id': channel_url,
+ 'thumbnail': f'{self._BASE_URL}/images/{channel_url}-color-logo.png',
+ 'is_live': True,
+ }
+
+
+class PolskieRadioPodcastBaseExtractor(InfoExtractor):
+ _API_BASE = 'https://apipodcasts.polskieradio.pl/api'
+
+ def _parse_episode(self, data):
+ return {
+ 'id': data['guid'],
+ 'formats': [{
+ 'url': data['url'],
+ 'filesize': int_or_none(data.get('fileSize')),
+ }],
+ 'title': data['title'],
+ 'description': data.get('description'),
+ 'duration': int_or_none(data.get('length')),
+ 'timestamp': parse_iso8601(data.get('publishDate')),
+ 'thumbnail': url_or_none(data.get('image')),
+ 'series': data.get('podcastTitle'),
+ 'episode': data['title'],
+ }
+
+
+class PolskieRadioPodcastListIE(PolskieRadioPodcastBaseExtractor):
+ IE_NAME = 'polskieradio:podcast:list'
+ _VALID_URL = r'https?://podcasty\.polskieradio\.pl/podcast/(?P<id>\d+)'
+ _TESTS = [{
+ 'url': 'https://podcasty.polskieradio.pl/podcast/8/',
+ 'info_dict': {
+ 'id': '8',
+ 'title': 'Śniadanie w Trójce',
+ 'description': 'md5:57abcc27bc4c6a6b25baa3061975b9ef',
+ 'uploader': 'Beata Michniewicz',
+ },
+ 'playlist_mincount': 714,
+ }]
+ _PAGE_SIZE = 10
+
+ def _call_api(self, podcast_id, page):
+ return self._download_json(
+ f'{self._API_BASE}/Podcasts/{podcast_id}/?pageSize={self._PAGE_SIZE}&page={page}',
+ podcast_id, f'Downloading page {page}')
+
+ def _real_extract(self, url):
+ podcast_id = self._match_id(url)
+ data = self._call_api(podcast_id, 1)
+
+ def get_page(page_num):
+ page_data = self._call_api(podcast_id, page_num + 1) if page_num else data
+ yield from (self._parse_episode(ep) for ep in page_data['items'])
+
+ return {
+ '_type': 'playlist',
+ 'entries': InAdvancePagedList(
+ get_page, math.ceil(data['itemCount'] / self._PAGE_SIZE), self._PAGE_SIZE),
+ 'id': str(data['id']),
+ 'title': data['title'],
+ 'description': data.get('description'),
+ 'uploader': data.get('announcer'),
+ }
+
+
+class PolskieRadioPodcastIE(PolskieRadioPodcastBaseExtractor):
+ IE_NAME = 'polskieradio:podcast'
+ _VALID_URL = r'https?://podcasty\.polskieradio\.pl/track/(?P<id>[a-f\d]{8}(?:-[a-f\d]{4}){4}[a-f\d]{8})'
+ _TESTS = [{
+ 'url': 'https://podcasty.polskieradio.pl/track/6eafe403-cb8f-4756-b896-4455c3713c32',
+ 'info_dict': {
+ 'id': '6eafe403-cb8f-4756-b896-4455c3713c32',
+ 'ext': 'mp3',
+ 'title': 'Theresa May rezygnuje. Co dalej z brexitem?',
+ 'description': 'md5:e41c409a29d022b70ef0faa61dbded60',
+ },
+ }]
+
+ def _real_extract(self, url):
+ podcast_id = self._match_id(url)
+ data = self._download_json(
+ f'{self._API_BASE}/audio',
+ podcast_id, 'Downloading podcast metadata',
+ data=json.dumps({
+ 'guids': [podcast_id],
+ }).encode('utf-8'),
+ headers={
+ 'Content-Type': 'application/json',
+ })
+ return self._parse_episode(data[0])
+
+
+class PolskieRadioRadioKierowcowIE(PolskieRadioBaseExtractor):
+ _VALID_URL = r'https?://(?:www\.)?radiokierowcow\.pl/artykul/(?P<id>[0-9]+)'
+ IE_NAME = 'polskieradio:kierowcow'
+
+ _TESTS = [{
+ 'url': 'https://radiokierowcow.pl/artykul/2694529',
+ 'info_dict': {
+ 'id': '2694529',
+ 'title': 'Zielona fala reliktem przeszłości?',
+ 'description': 'md5:343950a8717c9818fdfd4bd2b8ca9ff2',
+ },
+ 'playlist_count': 3,
+ }]
+
+ def _real_extract(self, url):
+ media_id = self._match_id(url)
+ webpage = self._download_webpage(url, media_id)
+ nextjs_build = self._search_nextjs_data(webpage, media_id)['buildId']
+ article = self._download_json(
+ f'https://radiokierowcow.pl/_next/data/{nextjs_build}/artykul/{media_id}.json?articleId={media_id}',
+ media_id)
+ data = article['pageProps']['data']
+ title = data['title']
+ entries = self._extract_webpage_player_entries(data['content'], media_id, {
+ 'title': title,
+ })
+
+ return {
+ '_type': 'playlist',
+ 'id': media_id,
+ 'entries': entries,
+ 'title': title,
+ 'description': data.get('lead'),
+ }