From faa0c006ec9c26e2fa87107c575d2fc7cb266d20 Mon Sep 17 00:00:00 2001 From: MoojMidge <56883549+MoojMidge@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:02:08 +1100 Subject: [PATCH] Add client_data parameter to YouTube.api_request method - Allows flexibility in passing client_data when building client details for the request --- .../youtube_plugin/youtube/client/youtube.py | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/resources/lib/youtube_plugin/youtube/client/youtube.py b/resources/lib/youtube_plugin/youtube/client/youtube.py index 26a54314b..d1e01e3c3 100644 --- a/resources/lib/youtube_plugin/youtube/client/youtube.py +++ b/resources/lib/youtube_plugin/youtube/client/youtube.py @@ -33,7 +33,7 @@ class YouTube(LoginClient): CLIENTS = { - 1: { + 'v1': { 'url': 'https://www.youtube.com/youtubei/v1/{_endpoint}', 'method': None, 'json': { @@ -48,7 +48,7 @@ class YouTube(LoginClient): 'Host': 'www.youtube.com', }, }, - 3: { + 'v3': { '_auth_required': True, 'url': 'https://www.googleapis.com/youtube/v3/{_endpoint}', 'method': None, @@ -330,7 +330,7 @@ def unsubscribe(self, subscription_id, **kwargs): def unsubscribe_channel(self, channel_id, **kwargs): post_data = {'channelIds': [channel_id]} - return self.api_request(version=1, + return self.api_request(client='v1', method='POST', path='subscription/unsubscribe', post_data=post_data, @@ -460,7 +460,7 @@ def get_recommended_for_home(self, } post_data['context'] = context - result = self.api_request(version=1, + result = self.api_request(client='v1', method='POST', path='browse', post_data=post_data) @@ -1099,8 +1099,9 @@ def get_related_videos(self, if page_token: post_data['continuation'] = page_token - result = self.api_request(version=('tv' if retry == 1 else - 'tv_embed' if retry == 2 else 1), + result = self.api_request(client=('tv' if retry == 1 else + 'tv_embed' if retry == 2 else + 'v1'), method='POST', path='next', post_data=post_data, @@ -2052,7 +2053,7 @@ def _perform(_playlist_idx, _page_token, _offset, _result): else: _post_data['browseId'] = 'FEmy_youtube' - _json_data = self.api_request(version=1, + _json_data = self.api_request(client='v1', method='POST', path='browse', post_data=_post_data) @@ -2147,7 +2148,7 @@ def _perform(_playlist_idx, _page_token, _offset, _result): } playlist_index = None - json_data = self.api_request(version=1, + json_data = self.api_request(client='v1', method='POST', path='browse', post_data=_en_post_data) @@ -2247,18 +2248,20 @@ def _error_hook(self, **kwargs): return '', info, details, data, False, exception def api_request(self, - version=3, + client='v3', method='GET', + client_data=None, path=None, params=None, post_data=None, headers=None, no_login=False, **kwargs): - client_data = { - '_endpoint': path.strip('/'), - 'method': method, - } + if not client_data: + client_data = {} + client_data.setdefault('method', method) + if path: + client_data['_endpoint'] = path.strip('/') if headers: client_data['headers'] = headers if method in {'POST', 'PUT'}: @@ -2279,7 +2282,7 @@ def api_request(self, if self._access_token_tv: client_data['_access_token_tv'] = self._access_token_tv - client = self.build_client(version, client_data) + client = self.build_client(client, client_data) if not client: client = {} abort = True @@ -2323,13 +2326,13 @@ def api_request(self, context = self._context context.log_debug('API request:' - '\n\tversion: |{version}|' + '\n\ttype: |{type}|' '\n\tmethod: |{method}|' '\n\tpath: |{path}|' '\n\tparams: |{params}|' '\n\tpost_data: |{data}|' '\n\theaders: |{headers}|' - .format(version=version, + .format(type=client.get('_name'), method=method, path=path, params=log_params,