Fix once again RTP downloads

pull/29824/head
Pedro Ferreira 3 years ago
parent 31595de72b
commit 526ef898bf

@ -3,6 +3,7 @@ from __future__ import unicode_literals
from .common import InfoExtractor from .common import InfoExtractor
from ..utils import ( from ..utils import (
ExtractorError,
determine_ext, determine_ext,
js_to_json, js_to_json,
) )
@ -14,15 +15,22 @@ from ..compat import (
import re import re
def decode_b64_url(code):
decoded_url = re.match(r"[^[]*\[([^]]*)\]", code).groups()[0]
return compat_b64decode(
compat_urllib_parse_unquote(
decoded_url.replace('"', '').replace('\'', '').replace(',', ''))).decode('utf-8')
class RTPIE(InfoExtractor): class RTPIE(InfoExtractor):
_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>[^/?#]+)/?' _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 = [{ _TESTS = [{
'url': 'https://www.rtp.pt/play/p117/e476527/os-contemporaneos', 'url': 'https://www.rtp.pt/play/p9165/e562949/por-do-sol',
'info_dict': { 'info_dict': {
'id': 'os-contemporaneos', 'id': 'por-do-sol',
'ext': 'mp4', 'ext': 'mp4',
'title': 'Os Contemporâneos Episódio 1 - RTP Play - RTP', 'title': 'Pôr do Sol Episódio 1 - de 16 Ago 2021',
'description': 'Os Contemporâneos, um programa de humor com um olhar na sociedade portuguesa!', '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', 'thumbnail': r're:^https?://.*\.jpg',
}, },
}, { }, {
@ -50,60 +58,42 @@ class RTPIE(InfoExtractor):
# Get JS object # Get JS object
js_object = self._search_regex(r'(?s)RTPPlayer *\( *({.+?}) *\);', webpage, 'player config') js_object = self._search_regex(r'(?s)RTPPlayer *\( *({.+?}) *\);', webpage, 'player config')
json_string_for_config = '' json_string_for_config = ''
full_url = None
# Verify JS object since it isn't pure JSON and maybe it needs some decodings # Verify JS object since it isn't pure JSON and maybe it needs some tuning
for line in js_object.splitlines(): for line in js_object.splitlines():
stripped_line = line.strip() stripped_line = line.strip()
# If JS object key is 'file' # key == 'fileKey', then we found what we wanted
if re.match('file ?:', stripped_line): if re.match(r'fileKey:', stripped_line):
if 'decodeURIComponent' in stripped_line: if re.match(r'fileKey: *""', stripped_line):
# 1) The file URL is inside object and with HLS encoded... raise ExtractorError("Episode not found (probably removed)", expected=True)
hls_encoded = re.match(r"[^[]*\[([^]]*)\]", stripped_line).groups()[0] url = decode_b64_url(stripped_line)
hls_encoded = hls_encoded.replace('"', '').replace('\'', '').replace(',', '') if 'mp3' in url:
if 'atob' in stripped_line: full_url = 'https://cdn-ondemand.rtp.pt' + url
decoded_file_url = compat_b64decode(
compat_urllib_parse_unquote(
hls_encoded.replace('"', '').replace(',', ''))).decode('utf-8')
else:
decoded_file_url = compat_urllib_parse_unquote(hls_encoded)
# Workaround for new behaviour
decoded_file_url = decoded_file_url.replace('streaming-vod.rtp.pt/hls/', 'streaming-ondemand.rtp.pt/').replace('.mp4/', '/')
# Insert the decoded HLS file URL into pure JSON string
json_string_for_config += '\nfile: "' + decoded_file_url + '",'
else: else:
# 2) ... or the file URL is not encoded so keep it that way full_url = 'https://streaming-vod.rtp.pt/dash{}/manifest.mpd'.format(url)
json_string_for_config += '\n' + line
elif not stripped_line.startswith("//") and not re.match('fileKey ?:', stripped_line) and not re.match('.*extraSettings ?:', stripped_line): elif not stripped_line.startswith("//") and not re.match('file *:', stripped_line) and not re.match('.*extraSettings ?:', stripped_line):
# Ignore commented lines, 'fileKey' entry since it is no longer supported by RTP and also 'extraSettings' # Ignore commented lines, `extraSettings` and `f`. The latter seems to some random unrelated video.
json_string_for_config += '\n' + line 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 # Finally send pure JSON string for JSON parsing
config = self._parse_json(json_string_for_config, video_id, js_to_json) 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)
# Check if file URL is directly a string or is still inside object if ext == 'mpd':
if isinstance(config['file'], str): # Download via mpd file
file_url = config['file'] formats = self._extract_mpd_formats(full_url, video_id)
else:
file_url = config['file']['hls']
ext = determine_ext(file_url)
if ext == 'm3u8':
# Download via m3u8 file
formats = self._extract_m3u8_formats(
file_url, video_id, 'mp4', 'm3u8_native',
m3u8_id='hls')
self._sort_formats(formats) self._sort_formats(formats)
else: else:
formats = [{ formats = [{
'url': file_url, 'url': full_url,
'ext': ext, 'ext': ext,
}] }]

Loading…
Cancel
Save