Skip to content

Commit

Permalink
Add client_data parameter to YouTube.api_request method
Browse files Browse the repository at this point in the history
- Allows flexibility in passing client_data when building client details for the request

(cherry picked from commit 8b55240)

# Conflicts:
#	resources/lib/youtube_plugin/youtube/client/youtube.py
  • Loading branch information
MoojMidge committed Dec 4, 2024
1 parent ce6863c commit a186542
Showing 1 changed file with 49 additions and 42 deletions.
91 changes: 49 additions & 42 deletions resources/lib/youtube_plugin/youtube/client/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

class YouTube(LoginClient):
CLIENTS = {
1: {
'v1': {
'url': 'https://www.youtube.com/youtubei/v1/{_endpoint}',
'method': None,
'json': {
Expand All @@ -47,13 +47,13 @@ class YouTube(LoginClient):
'Host': 'www.youtube.com',
},
},
3: {
'v3': {
'_auth_required': True,
'url': 'https://www.googleapis.com/youtube/v3/{_endpoint}',
'method': None,
'headers': {
'Host': 'www.googleapis.com',
},
'auth_required': True,
},
'tv': {
'url': 'https://www.youtube.com/youtubei/v1/{_endpoint}',
Expand Down Expand Up @@ -87,6 +87,7 @@ class YouTube(LoginClient):
},
'_common': {
'_access_token': None,
'_access_token_tv': None,
'json': {
'context': {
'client': {
Expand All @@ -108,7 +109,7 @@ class YouTube(LoginClient):
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Authorization': 'Bearer {_access_token}',
'Authorization': None,
'DNT': '1',
'User-Agent': ('Mozilla/5.0 (Linux; Android 10; SM-G981B)'
' AppleWebKit/537.36 (KHTML, like Gecko)'
Expand Down Expand Up @@ -325,7 +326,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,
Expand Down Expand Up @@ -453,7 +454,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)
Expand Down Expand Up @@ -1067,8 +1068,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,
Expand Down Expand Up @@ -1972,7 +1974,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)
Expand Down Expand Up @@ -2067,7 +2069,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)
Expand Down Expand Up @@ -2167,18 +2169,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'}:
Expand All @@ -2191,37 +2195,37 @@ def api_request(self,
client_data['params'] = params

abort = False
if no_login:
pass
# a config can decide if a token is allowed
elif self._access_token and self._config.get('token-allowed', True):
client_data['_access_token'] = self._access_token
elif self._access_token_tv:
client_data['_access_token'] = self._access_token_tv
# abort if authentication is required but not available for request
elif self.CLIENTS.get(version, {}).get('auth_required'):
if not no_login:
client_data.setdefault('_auth_required', True)
# a config can decide if a token is allowed
if self._access_token and self._config.get('token-allowed', True):
client_data['_access_token'] = self._access_token
if self._access_token_tv:
client_data['_access_token_tv'] = self._access_token_tv

client = self.build_client(client, client_data)
if not client:
client = {}
abort = True

client = self.build_client(version, client_data)
if clear_data and 'json' in client:
del client['json']

params = client.get('params')
if 'key' in params:
if params['key']:
abort = False
else:
params = params.copy()
key = self._config.get('key') or self._config_tv.get('key')
if key:
if params:
if 'key' in params:
if params['key']:
abort = False
params['key'] = key
else:
del params['key']
client['params'] = params
params = params.copy()
key = self._config.get('key') or self._config_tv.get('key')
if key:
abort = False
params['key'] = key
else:
del params['key']
client['params'] = params

if clear_data and 'json' in client:
del client['json']

if params:
log_params = params.copy()
if 'location' in log_params:
log_params['location'] = '|xx.xxxx,xx.xxxx|'
Expand All @@ -2241,21 +2245,24 @@ 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,
data=client.get('json'),
headers=log_headers))
if abort:
if kwargs.get('notify', True):
context.get_ui().on_ok(context.get_name(), context.localize('key.requirement'))
context.get_ui().on_ok(
context.get_name(),
context.localize('key.requirement'),
)
context.log_warning('API request: aborted')
return {}
return self.request(response_hook=self._response_hook,
Expand Down

0 comments on commit a186542

Please sign in to comment.