[RTP] Fix RTP Play support

See #4661
See https://github.com/ytdl-org/youtube-dl/pull/29824
This commit is contained in:
somini 2024-07-08 23:06:05 +01:00
parent 39bc699d2e
commit 9ae42f7ed0
No known key found for this signature in database
GPG Key ID: 2B21E7CA6F6A8FD4
1 changed files with 64 additions and 37 deletions

View File

@ -4,23 +4,29 @@ import re
import urllib.parse
from .common import InfoExtractor
from ..utils import js_to_json
from ..utils import ExtractorError, determine_ext, join_nonempty, js_to_json
def decode_b64_url(code):
decoded_url = re.match(r'[^[]*\[([^]]*)\]', code).groups()[0]
return base64.b64decode(
urllib.parse.unquote(re.sub(r'[\s"\',]', '', decoded_url)),
).decode('utf-8')
class RTPIE(InfoExtractor):
_VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
_VALID_URL = r'https?://(?:(?:(?:www\.)?rtp\.pt/play/(?P<subarea>.*/)?p(?P<program_id>[0-9]+)/(?P<episode_id>e[0-9]+/)?)|(?:arquivos\.rtp\.pt/conteudos/))(?P<id>[^/?#]+)/?'
_TESTS = [{
'url': 'http://www.rtp.pt/play/p405/e174042/paixoes-cruzadas',
'md5': 'e736ce0c665e459ddb818546220b4ef8',
'url': 'https://www.rtp.pt/play/p9165/e562949/por-do-sol',
'info_dict': {
'id': 'e174042',
'ext': 'mp3',
'title': 'Paixões Cruzadas',
'description': 'As paixões musicais de António Cartaxo e António Macedo',
'id': 'por-do-sol',
'ext': 'mp4',
'title': 'Pôr do Sol Episódio 1 - de 16 Ago 2021',
'description': 'Madalena Bourbon de Linhaça vive atormentada pelo segredo que esconde desde 1990. Matilde Bourbon de Linhaça sonha fugir com o seu amor proibido. O en',
'thumbnail': r're:^https?://.*\.jpg',
},
}, {
'url': 'http://www.rtp.pt/play/p831/a-quimica-das-coisas',
'url': 'https://www.rtp.pt/play/p510/aleixo-fm',
'only_matching': True,
}]
@ -44,39 +50,60 @@ class RTPIE(InfoExtractor):
video_id = self._match_id(url)
webpage = self._download_webpage(url, video_id)
title = self._html_search_meta(
'twitter:title', webpage, display_name='title', fatal=True)
f, config = self._search_regex(
r'''(?sx)
var\s+f\s*=\s*(?P<f>".*?"|{[^;]+?});\s*
var\s+player1\s+=\s+new\s+RTPPlayer\s*\((?P<config>{(?:(?!\*/).)+?})\);(?!\s*\*/)
''', webpage,
'player config', group=('f', 'config'))
# Remove comments from webpage source
webpage = re.sub(r'(?s)/\*.*\*/', '', webpage)
webpage = re.sub(r'(?m)(?:^|\s)//.*$', '', webpage)
f = self._parse_json(
f, video_id,
lambda data: self.__unobfuscate(data, video_id=video_id))
config = self._parse_json(
config, video_id,
lambda data: self.__unobfuscate(data, video_id=video_id))
title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title', default='')
# Replace irrelevant text in title
title = title.replace(' - RTP Play - RTP', '') or self._html_search_meta('twitter:title', webpage)
formats = []
if isinstance(f, dict):
f_hls = f.get('hls')
if f_hls is not None:
formats.extend(self._extract_m3u8_formats(
f_hls, video_id, 'mp4', 'm3u8_native', m3u8_id='hls'))
if 'Este episódio não se encontra disponí' in title:
raise ExtractorError('Episode unavailable', expected=True)
f_dash = f.get('dash')
if f_dash is not None:
formats.extend(self._extract_mpd_formats(f_dash, video_id, mpd_id='dash'))
part = self._html_search_regex(r'section\-parts.*<span.*>(.+?)</span>.*</ul>', webpage, 'part', default=None)
title = join_nonempty(title, part, delim=' ')
# Get JS object
js_object = self._search_regex(r'(?s)RTPPlayer *\( *({.+?}) *\);', webpage, 'player config')
json_string_for_config = ''
full_url = None
# Verify JS object since it isn't pure JSON and maybe it needs some tuning
for line in js_object.splitlines():
stripped_line = line.strip()
# key == 'fileKey', then we found what we wanted
if re.match(r'fileKey:', stripped_line):
if re.match(r'fileKey: *""', stripped_line):
raise ExtractorError('Episode not found (probably removed)', expected=True)
url = decode_b64_url(stripped_line)
if 'mp3' in url:
full_url = 'https://cdn-ondemand.rtp.pt' + url
else:
full_url = f'https://streaming-vod.rtp.pt/dash{url}/manifest.mpd'
elif not stripped_line.startswith('//') and not re.match('file *:', stripped_line) and not re.match('.*extraSettings ?:', stripped_line):
# Ignore commented lines, `extraSettings` and `f`. The latter seems to some random unrelated video.
json_string_for_config += '\n' + line
if not full_url:
raise ExtractorError('No valid media source found in page')
# Finally send pure JSON string for JSON parsing
config = self._parse_json(json_string_for_config, video_id, js_to_json)
full_url = full_url.replace('drm-dash', 'dash')
ext = determine_ext(full_url)
if ext == 'mpd':
# Download via mpd file
formats = self._extract_mpd_formats(full_url, video_id)
else:
formats.append({
'format_id': 'f',
'url': f,
'vcodec': 'none' if config.get('mediaType') == 'audio' else None,
})
formats = [{
'url': full_url,
'ext': ext,
}]
subtitles = {}