1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-06-25 21:29:35 +00:00
youtube-dl/youtube_dl/extractor/rtp.py

103 lines
3.7 KiB
Python
Raw Normal View History

2014-12-12 18:22:24 +00:00
# coding: utf-8
from __future__ import unicode_literals
from .common import InfoExtractor
2019-05-28 03:58:12 +00:00
from ..utils import (
determine_ext,
js_to_json,
)
2021-02-19 14:10:10 +00:00
from ..compat import (
compat_b64decode,
compat_urllib_parse_unquote,
)
import re
2014-12-12 18:22:24 +00:00
class RTPIE(InfoExtractor):
2021-02-19 14:10:10 +00:00
_VALID_URL = r'https?://(?:www\.)?rtp\.pt/play/(.*\/)?p(?P<program_id>[0-9]+)/(?P<id>[^/?#]+)/?'
_TESTS = [{
2021-02-19 14:10:10 +00:00
'url': 'https://www.rtp.pt/play/p117/e476527/os-contemporaneos',
2014-12-12 18:22:24 +00:00
'info_dict': {
2021-02-19 14:10:10 +00:00
'id': 'e476527',
'ext': 'mp4',
'title': 'Os Contemporâneos Episódio 1 - RTP Play - RTP',
'description': 'Os Contemporâneos, um programa de humor com um olhar na sociedade portuguesa!',
'thumbnail': r're:^https?://.*\.jpg',
2014-12-12 18:22:24 +00:00
},
}, {
2021-02-19 14:10:10 +00:00
'url': 'https://www.rtp.pt/play/p510/aleixo-fm',
'only_matching': True,
}]
2014-12-12 18:22:24 +00:00
def _real_extract(self, url):
video_id = self._match_id(url)
webpage = self._download_webpage(url, video_id)
2021-02-19 14:10:10 +00:00
title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
# Get JS object
js_object = self._search_regex(r'(?s)RTPPlayer *\( *({.+?}) *\);', webpage, 'player config')
json_string_for_config = ''
# Verify JS object since it isn't pure JSON and maybe it needs some decodings
for line in js_object.splitlines():
stripped_line = line.strip()
# If JS object key is 'file'
if re.match('file ?:', stripped_line):
if 'decodeURIComponent' in stripped_line:
# 1) The file URL is inside object and with HLS encoded...
hls_encoded = re.match(r"[^[]*\[([^]]*)\]", stripped_line).groups()[0]
hls_encoded = hls_encoded.replace('"', '').replace('\'', '').replace(',', '')
decoded_file_url = compat_b64decode(
compat_urllib_parse_unquote(
hls_encoded.replace('"', '').replace(',', ''))).decode('utf-8')
# Insert the decoded HLS file URL into pure JSON string
json_string_for_config += '\nfile: "' + decoded_file_url + '",'
else:
# 2) ... or the file URL is not encoded so keep it that way
2021-02-19 14:10:10 +00:00
json_string_for_config += '\n' + line
elif not stripped_line.startswith("//") and not re.match('fileKey ?:', stripped_line):
# Ignore commented lines and 'fileKey' entry since it is no longer supported by RTP
json_string_for_config += '\n' + line
# Finally send pure JSON string for JSON parsing
config = self._parse_json(json_string_for_config, video_id, js_to_json)
# Check if file URL is directly a string or is still inside object
if isinstance(config['file'], str):
file_url = config['file']
else:
file_url = config['file']['hls']
2019-05-28 03:58:12 +00:00
ext = determine_ext(file_url)
2021-02-19 14:10:10 +00:00
2019-05-28 03:58:12 +00:00
if ext == 'm3u8':
2021-02-19 14:10:10 +00:00
# Download via m3u8 file
2019-05-28 03:58:12 +00:00
formats = self._extract_m3u8_formats(
file_url, video_id, 'mp4', 'm3u8_native',
2021-02-19 14:10:10 +00:00
m3u8_id='hls')
2019-05-28 03:58:12 +00:00
self._sort_formats(formats)
else:
formats = [{
'url': file_url,
'ext': ext,
}]
2021-02-19 14:10:10 +00:00
2019-05-28 03:58:12 +00:00
if config.get('mediaType') == 'audio':
for f in formats:
f['vcodec'] = 'none'
2014-12-12 18:22:24 +00:00
return {
'id': video_id,
'title': title,
'formats': formats,
2019-05-28 03:58:12 +00:00
'description': self._html_search_meta(['description', 'twitter:description'], webpage),
'thumbnail': config.get('poster') or self._og_search_thumbnail(webpage),
2014-12-12 18:22:24 +00:00
}