From 5ca5b56cfc9cff1afae36ae9d68496d541c06d34 Mon Sep 17 00:00:00 2001 From: c0d3d3v Date: Wed, 15 Mar 2023 11:31:13 +0100 Subject: [PATCH] [extractor/echo360] Implement all suggestions --- yt_dlp/extractor/echo360.py | 64 ++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/yt_dlp/extractor/echo360.py b/yt_dlp/extractor/echo360.py index ee853c682..8f95b61a4 100644 --- a/yt_dlp/extractor/echo360.py +++ b/yt_dlp/extractor/echo360.py @@ -1,28 +1,20 @@ import re +from urllib.parse import urlparse, urlunparse + from .common import InfoExtractor from ..utils import ( determine_ext, float_or_none, traverse_obj, - variadic, ) class Echo360IE(InfoExtractor): - _INSTANCES_RE = r'''(?: - echo360\.ca| - echo360\.net\.au| - echo360\.org\.au| - echo360\.org\.uk| - echo360\.org| - )''' - _UUID_RE = r'[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}' - _VALID_URL = rf'''(?x) - https?://(?P{_INSTANCES_RE}) - /media/(?P{_UUID_RE})/public''' - - _API_BASE = 'https://%s/api/ui/echoplayer/public-links/%s/media/%s/player-properties' + _VALID_URL = r'''(?x) + https?://(?Pecho360\.(?:ca|net\.au|org|org\.au|org\.uk))/ + media/(?P[\da-fA-F]{8}-(?:[\da-fA-F]{4}-){3}[\da-fA-F]{12})/public + ''' _TESTS = [ { @@ -49,21 +41,22 @@ class Echo360IE(InfoExtractor): def _call_api(self, host, video_id, media_id, session_token, **kwargs): return self._download_json( - self._API_BASE % (host, video_id, media_id), video_id, - headers={'Authorization': f'Bearer {session_token}'}, **kwargs) + f'https://{host}/api/ui/echoplayer/public-links/{video_id}/media/{media_id}/player-properties', + video_id, headers={'Authorization': f'Bearer {session_token}'}, **kwargs) - @staticmethod - def _update_url_query(uri, query_string): + def _replace_url_query(self, url, query_string): if query_string is not None: - return f'{uri.split("?", 1)[0]}?{query_string}' - return uri + return urlunparse(urlparse(url)._replace(query=query_string)) + return url - @staticmethod - def _get_query_string(uri, query_strings): - uri_base = uri.split("?", 1)[0] + def _get_query_string(self, uri, query_strings): + uri_base = urlparse(uri)._replace(query='', fragment='').geturl() for query_string in query_strings: - if re.match(query_string['uriPattern'], uri_base): - return query_string['queryString'] + try: + if re.match(query_string['uriPattern'], uri_base): + return query_string['queryString'] + except re.error as re_error: + self.report_warning(f'Error in query string pattern `{re_error.pattern}`: {re_error.msg}') return None def _parse_mediapackage(self, video): @@ -71,11 +64,11 @@ class Echo360IE(InfoExtractor): query_strings = traverse_obj(video, ('sourceQueryStrings', 'queryStrings')) or [] formats = [] - for track in variadic(traverse_obj(video, ('playableAudioVideo', 'playableMedias')) or []): + for track in traverse_obj(video, ('playableAudioVideo', 'playableMedias', ...)): href = track.get('uri') if href is None: continue - href = self._update_url_query(href, self._get_query_string(href, query_strings)) + href = self._replace_url_query(href, self._get_query_string(href, query_strings)) if track.get('isHls') or determine_ext(href, None) == 'm3u8': hls_formats = self._extract_m3u8_formats( href, video_id, live=track.get('isLive'), m3u8_id='hls', @@ -85,7 +78,7 @@ class Echo360IE(InfoExtractor): for hls_format in hls_formats: query_string = self._get_query_string(hls_format['url'], query_strings) hls_format['extra_param_to_segment_url'] = query_string - hls_format['url'] = self._update_url_query(hls_format['url'], query_string) + hls_format['url'] = self._replace_url_query(hls_format['url'], query_string) formats.extend(hls_formats) @@ -94,24 +87,21 @@ class Echo360IE(InfoExtractor): 'formats': formats, 'title': video.get('mediaName'), 'duration': float_or_none(self._search_regex( - r'PT(\d+\.?\d+)S', traverse_obj(video, ('playableAudioVideo', 'duration')), - 'video duration', default=None, fatal=False)), + r'PT([\d.]+)S', traverse_obj(video, ('playableAudioVideo', 'duration')), + 'video duration', fatal=False)), } def _real_extract(self, url): host, video_id = self._match_valid_url(url).group('host', 'id') webpage = self._download_webpage(url, video_id) - player_config = self._search_json(r'Echo\["mediaPlayerBootstrapApp"\]\("', webpage, - 'player config', video_id, - transform_source=lambda x: x.replace(R'\"', '"')) + player_config = self._search_json( + r'Echo\["mediaPlayerBootstrapApp"\]\("', webpage, 'player config', + video_id, transform_source=lambda x: x.replace(R'\"', '"')) urlh = self._request_webpage( f'https://{host}/api/ui/sessions/{player_config["sessionId"]}', - video_id, - note='Open video session', - errnote='Unable to open video session', - ) + video_id, 'Open video session', 'Unable to open video session') return self._parse_mediapackage(self._call_api( host, player_config.get('shareLinkId') or player_config['publicLinkId'],