From 6160d2b6b5fe27fffead008630d919b1697d7a08 Mon Sep 17 00:00:00 2001 From: v3DJG6GL <72495210+v3DJG6GL@users.noreply.github.com> Date: Mon, 24 Feb 2025 01:53:19 +0100 Subject: [PATCH] playsuisse.py: handle locale parameters from URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Currently, the locale header is statically set to “de”, regardless of which locale is set in the URL parameters. - Therefore, all retrieved metadata (description, names, etc.) were in German. - This PR implements a mechanism that extracts the locale value from the specified URL. - As a fallback, URLs without locale parameters are automatically set to de, which corresponds to the original behavior. Advantages: All metadata is retrieved in the specified language. --- yt_dlp/extractor/playsuisse.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/yt_dlp/extractor/playsuisse.py b/yt_dlp/extractor/playsuisse.py index 59231d840..20e2e7950 100644 --- a/yt_dlp/extractor/playsuisse.py +++ b/yt_dlp/extractor/playsuisse.py @@ -214,27 +214,16 @@ def _perform_login(self, username, password): if not self._ID_TOKEN: raise ExtractorError('Login failed') - - def _get_media_data(self, media_id): - # NOTE In the web app, the "locale" header is used to switch between languages, - # However this doesn't seem to take effect when passing the header here. - response = self._download_json( - 'https://www.playsuisse.ch/api/graphql', - media_id, data=json.dumps({ - 'operationName': 'AssetWatch', - 'query': self._GRAPHQL_QUERY, - 'variables': {'assetId': media_id}, - }).encode(), - headers={'Content-Type': 'application/json', 'locale': 'de'}) - - return response['data']['assetV2'] - + def _real_extract(self, url): if not self._ID_TOKEN: self.raise_login_required(method='password') - + media_id = self._match_id(url) - media_data = self._get_media_data(media_id) + query = parse_qs(url) + locale_param = (query.get('locale') or ['de'])[0].lower() + locale = locale_param if locale_param in {'fr', 'en', 'it', 'de', 'rm'} else 'de' + media_data = self._get_media_data(media_id, locale) info = self._extract_single(media_data) if media_data.get('episodes'): info.update({ @@ -242,6 +231,17 @@ def _real_extract(self, url): 'entries': map(self._extract_single, media_data['episodes']), }) return info + + def _get_media_data(self, media_id, locale): + response = self._download_json( + 'https://www.playsuisse.ch/api/graphql', + media_id, data=json.dumps({ + 'operationName': 'AssetWatch', + 'query': self._GRAPHQL_QUERY, + 'variables': {'assetId': media_id}, + }).encode(), + headers={'Content-Type': 'application/json', 'locale': locale}) + return response['data']['assetV2'] def _extract_single(self, media_data): thumbnails = traverse_obj(media_data, lambda k, _: k.startswith('thumbnail'))