[extractor/polskieradio] Improve extractors (#5948)

Authored by: selfisekai
This commit is contained in:
lauren n. liberda 2023-05-29 05:22:38 +02:00 committed by GitHub
parent 93e12ed76e
commit 738c90a463
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 137 additions and 72 deletions

View File

@ -1479,7 +1479,6 @@ from .polskieradio import (
PolskieRadioPlayerIE, PolskieRadioPlayerIE,
PolskieRadioPodcastIE, PolskieRadioPodcastIE,
PolskieRadioPodcastListIE, PolskieRadioPodcastListIE,
PolskieRadioRadioKierowcowIE,
) )
from .popcorntimes import PopcorntimesIE from .popcorntimes import PopcorntimesIE
from .popcorntv import PopcornTVIE from .popcorntv import PopcornTVIE

View File

@ -2,26 +2,24 @@ import itertools
import json import json
import math import math
import re import re
import urllib.parse
from .common import InfoExtractor from .common import InfoExtractor
from ..compat import ( from ..compat import compat_str
compat_str,
compat_urllib_parse_unquote,
compat_urlparse
)
from ..utils import ( from ..utils import (
determine_ext,
extract_attributes,
ExtractorError, ExtractorError,
InAdvancePagedList, InAdvancePagedList,
determine_ext,
extract_attributes,
int_or_none, int_or_none,
js_to_json, js_to_json,
parse_iso8601, parse_iso8601,
strip_or_none, strip_or_none,
traverse_obj, traverse_obj,
unified_timestamp,
unescapeHTML, unescapeHTML,
unified_timestamp,
url_or_none, url_or_none,
urljoin,
) )
@ -44,7 +42,7 @@ class PolskieRadioBaseExtractor(InfoExtractor):
'duration': int_or_none(media.get('length')), 'duration': int_or_none(media.get('length')),
'vcodec': 'none' if media.get('provider') == 'audio' else None, 'vcodec': 'none' if media.get('provider') == 'audio' else None,
}) })
entry_title = compat_urllib_parse_unquote(media['desc']) entry_title = urllib.parse.unquote(media['desc'])
if entry_title: if entry_title:
entry['title'] = entry_title entry['title'] = entry_title
yield entry yield entry
@ -130,10 +128,11 @@ class PolskieRadioLegacyIE(PolskieRadioBaseExtractor):
return self.playlist_result(entries, playlist_id, title, description) return self.playlist_result(entries, playlist_id, title, description)
class PolskieRadioIE(InfoExtractor): class PolskieRadioIE(PolskieRadioBaseExtractor):
# new next.js sites, excluding radiokierowcow.pl # new next.js sites
_VALID_URL = r'https?://(?:[^/]+\.)?polskieradio(?:24)?\.pl/artykul/(?P<id>\d+)' _VALID_URL = r'https?://(?:[^/]+\.)?(?:polskieradio(?:24)?|radiokierowcow)\.pl/artykul/(?P<id>\d+)'
_TESTS = [{ _TESTS = [{
# articleData, attachments
'url': 'https://jedynka.polskieradio.pl/artykul/1587943', 'url': 'https://jedynka.polskieradio.pl/artykul/1587943',
'info_dict': { 'info_dict': {
'id': '1587943', 'id': '1587943',
@ -148,6 +147,31 @@ class PolskieRadioIE(InfoExtractor):
'title': 'md5:d4623290d4ac983bf924061c75c23a0d', 'title': 'md5:d4623290d4ac983bf924061c75c23a0d',
}, },
}], }],
}, {
# post, legacy html players
'url': 'https://trojka.polskieradio.pl/artykul/2589163,Czy-wciaz-otrzymujemy-zdjecia-z-sond-Voyager',
'info_dict': {
'id': '2589163',
'title': 'Czy wciąż otrzymujemy zdjęcia z sond Voyager?',
'description': 'md5:cf1a7f348d63a2db9c0d7a63d1669473',
},
'playlist': [{
'info_dict': {
'id': '2577880',
'ext': 'mp3',
'title': 'md5:a57d10a0c02abd34dd675cb33707ad5a',
'duration': 321,
},
}],
}, {
# data, legacy
'url': 'https://radiokierowcow.pl/artykul/2694529',
'info_dict': {
'id': '2694529',
'title': 'Zielona fala reliktem przeszłości?',
'description': 'md5:f20a9a7ed9cb58916c54add94eae3bc0',
},
'playlist_count': 3,
}, { }, {
'url': 'https://trojka.polskieradio.pl/artykul/1632955', 'url': 'https://trojka.polskieradio.pl/artykul/1632955',
'only_matching': True, 'only_matching': True,
@ -166,7 +190,8 @@ class PolskieRadioIE(InfoExtractor):
webpage = self._download_webpage(url, playlist_id) webpage = self._download_webpage(url, playlist_id)
article_data = traverse_obj( article_data = traverse_obj(
self._search_nextjs_data(webpage, playlist_id), ('props', 'pageProps', 'data', 'articleData')) self._search_nextjs_data(webpage, playlist_id), (
'props', 'pageProps', (('data', 'articleData'), 'post', 'data')), get_all=False)
title = strip_or_none(article_data['title']) title = strip_or_none(article_data['title'])
@ -178,7 +203,13 @@ class PolskieRadioIE(InfoExtractor):
'id': self._search_regex( 'id': self._search_regex(
r'([a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12})', entry['file'], 'entry id'), r'([a-f\d]{8}-(?:[a-f\d]{4}-){3}[a-f\d]{12})', entry['file'], 'entry id'),
'title': strip_or_none(entry.get('description')) or title, 'title': strip_or_none(entry.get('description')) or title,
} for entry in article_data.get('attachments') or () if entry['fileType'] in ('Audio', )] } for entry in article_data.get('attachments') or () if entry.get('fileType') in ('Audio', )]
if not entries:
# some legacy articles have no json attachments, but players in body
entries = self._extract_webpage_player_entries(article_data['content'], playlist_id, {
'title': title,
})
return self.playlist_result(entries, playlist_id, title, description) return self.playlist_result(entries, playlist_id, title, description)
@ -214,6 +245,15 @@ class PolskieRadioAuditionIE(InfoExtractor):
'thumbnail': r're:https://static\.prsa\.pl/images/.+', 'thumbnail': r're:https://static\.prsa\.pl/images/.+',
}, },
'playlist_mincount': 722, 'playlist_mincount': 722,
}, {
# some articles were "promoted to main page" and thus link to old frontend
'url': 'https://trojka.polskieradio.pl/audycja/305',
'info_dict': {
'id': '305',
'title': 'Co w mowie piszczy?',
'thumbnail': r're:https://static\.prsa\.pl/images/.+',
},
'playlist_count': 1523,
}] }]
def _call_lp3(self, path, query, video_id, note): def _call_lp3(self, path, query, video_id, note):
@ -254,7 +294,6 @@ class PolskieRadioAuditionIE(InfoExtractor):
for article in page['data']: for article in page['data']:
yield { yield {
'_type': 'url_transparent', '_type': 'url_transparent',
'ie_key': PolskieRadioIE.ie_key(),
'id': str(article['id']), 'id': str(article['id']),
'url': article['url'], 'url': article['url'],
'title': article.get('shortTitle'), 'title': article.get('shortTitle'),
@ -282,11 +321,8 @@ class PolskieRadioAuditionIE(InfoExtractor):
class PolskieRadioCategoryIE(InfoExtractor): class PolskieRadioCategoryIE(InfoExtractor):
# legacy sites # legacy sites
IE_NAME = 'polskieradio:category' IE_NAME = 'polskieradio:category'
_VALID_URL = r'https?://(?:www\.)?polskieradio\.pl/\d+(?:,[^/]+)?/(?P<id>\d+)' _VALID_URL = r'https?://(?:www\.)?polskieradio\.pl/(?:\d+(?:,[^/]+)?/|[^/]+/Tag)(?P<id>\d+)'
_TESTS = [{ _TESTS = [{
'url': 'http://www.polskieradio.pl/7/129,Sygnaly-dnia?ref=source',
'only_matching': True
}, {
'url': 'http://www.polskieradio.pl/37,RedakcjaKatolicka/4143,Kierunek-Krakow', 'url': 'http://www.polskieradio.pl/37,RedakcjaKatolicka/4143,Kierunek-Krakow',
'info_dict': { 'info_dict': {
'id': '4143', 'id': '4143',
@ -300,6 +336,36 @@ class PolskieRadioCategoryIE(InfoExtractor):
'title': 'Muzyka', 'title': 'Muzyka',
}, },
'playlist_mincount': 61 'playlist_mincount': 61
}, {
# billennium tabs
'url': 'https://www.polskieradio.pl/8/2385',
'info_dict': {
'id': '2385',
'title': 'Droga przez mąkę',
},
'playlist_mincount': 111,
}, {
'url': 'https://www.polskieradio.pl/10/4930',
'info_dict': {
'id': '4930',
'title': 'Teraz K-pop!',
},
'playlist_mincount': 392,
}, {
# post back pages, audio content directly without articles
'url': 'https://www.polskieradio.pl/8,dwojka/7376,nowa-mowa',
'info_dict': {
'id': '7376',
'title': 'Nowa mowa',
},
'playlist_mincount': 244,
}, {
'url': 'https://www.polskieradio.pl/Krzysztof-Dziuba/Tag175458',
'info_dict': {
'id': '175458',
'title': 'Krzysztof Dziuba',
},
'playlist_mincount': 420,
}, { }, {
'url': 'http://www.polskieradio.pl/8,Dwojka/196,Publicystyka', 'url': 'http://www.polskieradio.pl/8,Dwojka/196,Publicystyka',
'only_matching': True, 'only_matching': True,
@ -311,25 +377,61 @@ class PolskieRadioCategoryIE(InfoExtractor):
def _entries(self, url, page, category_id): def _entries(self, url, page, category_id):
content = page content = page
is_billennium_tabs = 'onclick="TB_LoadTab(' in page
is_post_back = 'onclick="__doPostBack(' in page
pagination = page if is_billennium_tabs else None
for page_num in itertools.count(2): for page_num in itertools.count(2):
for a_entry, entry_id in re.findall( for a_entry, entry_id in re.findall(
r'(?s)<article[^>]+>.*?(<a[^>]+href=["\']/\d+/\d+/Artykul/(\d+)[^>]+>).*?</article>', r'(?s)<article[^>]+>.*?(<a[^>]+href=["\'](?:(?:https?)?://[^/]+)?/\d+/\d+/Artykul/(\d+)[^>]+>).*?</article>',
content): content):
entry = extract_attributes(a_entry) entry = extract_attributes(a_entry)
href = entry.get('href') if entry.get('href'):
if not href: yield self.url_result(
continue urljoin(url, entry['href']), PolskieRadioLegacyIE, entry_id, entry.get('title'))
yield self.url_result( for a_entry in re.findall(r'<span data-media=({[^ ]+})', content):
compat_urlparse.urljoin(url, href), PolskieRadioLegacyIE, yield traverse_obj(self._parse_json(a_entry, category_id), {
entry_id, entry.get('title')) 'url': 'file',
mobj = re.search( 'id': 'uid',
r'<div[^>]+class=["\']next["\'][^>]*>\s*<a[^>]+href=(["\'])(?P<url>(?:(?!\1).)+)\1', 'duration': 'length',
content) 'title': ('title', {urllib.parse.unquote}),
if not mobj: 'description': ('desc', {urllib.parse.unquote}),
break })
next_url = compat_urlparse.urljoin(url, mobj.group('url')) if is_billennium_tabs:
content = self._download_webpage( params = self._search_json(
next_url, category_id, 'Downloading page %s' % page_num) r'<div[^>]+class=["\']next["\'][^>]*>\s*<a[^>]+onclick=["\']TB_LoadTab\(',
pagination, 'next page params', category_id, default=None, close_objects=1,
contains_pattern='.+', transform_source=lambda x: '[%s' % js_to_json(unescapeHTML(x)))
if not params:
break
tab_content = self._download_json(
'https://www.polskieradio.pl/CMS/TemplateBoxesManagement/TemplateBoxTabContent.aspx/GetTabContent',
category_id, f'Downloading page {page_num}', headers={'content-type': 'application/json'},
data=json.dumps(dict(zip((
'boxInstanceId', 'tabId', 'categoryType', 'sectionId', 'categoryId', 'pagerMode',
'subjectIds', 'tagIndexId', 'queryString', 'name', 'openArticlesInParentTemplate',
'idSectionFromUrl', 'maxDocumentAge', 'showCategoryForArticle', 'pageNumber'
), params))).encode())['d']
content, pagination = tab_content['Content'], tab_content.get('PagerContent')
elif is_post_back:
target = self._search_regex(
r'onclick=(?:["\'])__doPostBack\((?P<q1>["\'])(?P<target>[\w$]+)(?P=q1)\s*,\s*(?P<q2>["\'])Next(?P=q2)',
content, 'pagination postback target', group='target', default=None)
if not target:
break
content = self._download_webpage(
url, category_id, f'Downloading page {page_num}',
data=urllib.parse.urlencode({
**self._hidden_inputs(content),
'__EVENTTARGET': target,
'__EVENTARGUMENT': 'Next',
}).encode())
else:
next_url = urljoin(url, self._search_regex(
r'<div[^>]+class=["\']next["\'][^>]*>\s*<a[^>]+href=(["\'])(?P<url>(?:(?!\1).)+)\1',
content, 'next page url', group='url', default=None))
if not next_url:
break
content = self._download_webpage(next_url, category_id, f'Downloading page {page_num}')
def _real_extract(self, url): def _real_extract(self, url):
category_id = self._match_id(url) category_id = self._match_id(url)
@ -337,7 +439,7 @@ class PolskieRadioCategoryIE(InfoExtractor):
if PolskieRadioAuditionIE.suitable(urlh.url): if PolskieRadioAuditionIE.suitable(urlh.url):
return self.url_result(urlh.url, PolskieRadioAuditionIE, category_id) return self.url_result(urlh.url, PolskieRadioAuditionIE, category_id)
title = self._html_search_regex( title = self._html_search_regex(
r'<title>([^<]+) - [^<]+ - [^<]+</title>', r'<title>([^<]+)(?: - [^<]+ - [^<]+| w [Pp]olskie[Rr]adio\.pl\s*)</title>',
webpage, 'title', fatal=False) webpage, 'title', fatal=False)
return self.playlist_result( return self.playlist_result(
self._entries(url, webpage, category_id), self._entries(url, webpage, category_id),
@ -506,39 +608,3 @@ class PolskieRadioPodcastIE(PolskieRadioPodcastBaseExtractor):
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}) })
return self._parse_episode(data[0]) 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'),
}