[twitch] Improve access token extraction and remove unused code (closes #27646)

This commit is contained in:
Sergey M․ 2021-01-06 02:10:44 +07:00
parent 5b24f8f505
commit ccc7112291
No known key found for this signature in database
GPG Key ID: 2C393E0F18A9236D

View File

@ -192,13 +192,8 @@ class TwitchGraphQLBaseIE(TwitchBaseIE):
} }
return self._download_base_gql(video_id, ops, note) return self._download_base_gql(video_id, ops, note)
def _download_access_token_gql(self, video_id, item_type=None): def _download_access_token_gql(self, video_id, token_kind, param_name):
if item_type == 'vod': method = '%sPlaybackAccessToken' % token_kind
method = 'videoPlaybackAccessToken'
param_name = 'id'
else:
method = 'streamPlaybackAccessToken'
param_name = 'channelName'
ops = { ops = {
'query': '''{ 'query': '''{
%s( %s(
@ -207,14 +202,17 @@ class TwitchGraphQLBaseIE(TwitchBaseIE):
platform: "web", platform: "web",
playerBackend: "mediaplayer", playerBackend: "mediaplayer",
playerType: "site" playerType: "site"
}) { }
)
{
value value
signature signature
} }
}''' % (method, param_name, video_id), }''' % (method, param_name, video_id),
} }
note = 'Downloading access token GraphQL' return self._download_base_gql(
return self._download_base_gql(video_id, ops, note)['data'][method] video_id, ops,
'Downloading %s access token GraphQL' % token_kind)['data'][method]
class TwitchVodIE(TwitchGraphQLBaseIE): class TwitchVodIE(TwitchGraphQLBaseIE):
@ -227,8 +225,6 @@ class TwitchVodIE(TwitchGraphQLBaseIE):
) )
(?P<id>\d+) (?P<id>\d+)
''' '''
_ITEM_TYPE = 'vod'
_ITEM_SHORTCUT = 'v'
_TESTS = [{ _TESTS = [{
'url': 'http://www.twitch.tv/riotgames/v/6528877?t=5m10s', 'url': 'http://www.twitch.tv/riotgames/v/6528877?t=5m10s',
@ -333,7 +329,7 @@ class TwitchVodIE(TwitchGraphQLBaseIE):
vod_id = self._match_id(url) vod_id = self._match_id(url)
info = self._download_info(vod_id) info = self._download_info(vod_id)
access_token = self._download_access_token_gql(vod_id, self._ITEM_TYPE) access_token = self._download_access_token_gql(vod_id, 'video', 'id')
formats = self._extract_m3u8_formats( formats = self._extract_m3u8_formats(
'%s/vod/%s.m3u8?%s' % ( '%s/vod/%s.m3u8?%s' % (
@ -839,7 +835,8 @@ class TwitchStreamIE(TwitchGraphQLBaseIE):
if not stream: if not stream:
raise ExtractorError('%s is offline' % channel_name, expected=True) raise ExtractorError('%s is offline' % channel_name, expected=True)
access_token = self._download_access_token_gql(channel_name) access_token = self._download_access_token_gql(
channel_name, 'stream', 'channelName')
token = access_token['value'] token = access_token['value']
stream_id = stream.get('id') or channel_name stream_id = stream.get('id') or channel_name