mirror of
https://github.com/ytdl-org/youtube-dl.git
synced 2024-11-28 21:22:13 +00:00
[twitch] Switch access token to GraphQL and refactor.
This commit is contained in:
parent
fcd90d2583
commit
5b24f8f505
1 changed files with 68 additions and 46 deletions
|
@ -160,7 +160,64 @@ class TwitchBaseIE(InfoExtractor):
|
||||||
return compat_str(self._parse_json(token, channel_name)['channel_id'])
|
return compat_str(self._parse_json(token, channel_name)['channel_id'])
|
||||||
|
|
||||||
|
|
||||||
class TwitchVodIE(TwitchBaseIE):
|
class TwitchGraphQLBaseIE(TwitchBaseIE):
|
||||||
|
_PAGE_LIMIT = 100
|
||||||
|
|
||||||
|
_OPERATION_HASHES = {
|
||||||
|
'CollectionSideBar': '27111f1b382effad0b6def325caef1909c733fe6a4fbabf54f8d491ef2cf2f14',
|
||||||
|
'FilterableVideoTower_Videos': 'a937f1d22e269e39a03b509f65a7490f9fc247d7f83d6ac1421523e3b68042cb',
|
||||||
|
'ClipsCards__User': 'b73ad2bfaecfd30a9e6c28fada15bd97032c83ec77a0440766a56fe0bd632777',
|
||||||
|
'ChannelCollectionsContent': '07e3691a1bad77a36aba590c351180439a40baefc1c275356f40fc7082419a84',
|
||||||
|
'StreamMetadata': '1c719a40e481453e5c48d9bb585d971b8b372f8ebb105b17076722264dfa5b3e',
|
||||||
|
'ComscoreStreamingQuery': 'e1edae8122517d013405f237ffcc124515dc6ded82480a88daef69c83b53ac01',
|
||||||
|
'VideoPreviewOverlay': '3006e77e51b128d838fa4e835723ca4dc9a05c5efd4466c1085215c6e437e65c',
|
||||||
|
}
|
||||||
|
|
||||||
|
def _download_base_gql(self, video_id, ops, note, fatal=True):
|
||||||
|
return self._download_json(
|
||||||
|
'https://gql.twitch.tv/gql', video_id, note,
|
||||||
|
data=json.dumps(ops).encode(),
|
||||||
|
headers={
|
||||||
|
'Content-Type': 'text/plain;charset=UTF-8',
|
||||||
|
'Client-ID': self._CLIENT_ID,
|
||||||
|
}, fatal=fatal)
|
||||||
|
|
||||||
|
def _download_gql(self, video_id, ops, note, fatal=True):
|
||||||
|
for op in ops:
|
||||||
|
op['extensions'] = {
|
||||||
|
'persistedQuery': {
|
||||||
|
'version': 1,
|
||||||
|
'sha256Hash': self._OPERATION_HASHES[op['operationName']],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return self._download_base_gql(video_id, ops, note)
|
||||||
|
|
||||||
|
def _download_access_token_gql(self, video_id, item_type=None):
|
||||||
|
if item_type == 'vod':
|
||||||
|
method = 'videoPlaybackAccessToken'
|
||||||
|
param_name = 'id'
|
||||||
|
else:
|
||||||
|
method = 'streamPlaybackAccessToken'
|
||||||
|
param_name = 'channelName'
|
||||||
|
ops = {
|
||||||
|
'query': '''{
|
||||||
|
%s(
|
||||||
|
%s: "%s",
|
||||||
|
params: {
|
||||||
|
platform: "web",
|
||||||
|
playerBackend: "mediaplayer",
|
||||||
|
playerType: "site"
|
||||||
|
}) {
|
||||||
|
value
|
||||||
|
signature
|
||||||
|
}
|
||||||
|
}''' % (method, param_name, video_id),
|
||||||
|
}
|
||||||
|
note = 'Downloading access token GraphQL'
|
||||||
|
return self._download_base_gql(video_id, ops, note)['data'][method]
|
||||||
|
|
||||||
|
|
||||||
|
class TwitchVodIE(TwitchGraphQLBaseIE):
|
||||||
IE_NAME = 'twitch:vod'
|
IE_NAME = 'twitch:vod'
|
||||||
_VALID_URL = r'''(?x)
|
_VALID_URL = r'''(?x)
|
||||||
https?://
|
https?://
|
||||||
|
@ -276,9 +333,7 @@ class TwitchVodIE(TwitchBaseIE):
|
||||||
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._call_api(
|
access_token = self._download_access_token_gql(vod_id, self._ITEM_TYPE)
|
||||||
'api/vods/%s/access_token' % vod_id, vod_id,
|
|
||||||
'Downloading %s access token' % self._ITEM_TYPE)
|
|
||||||
|
|
||||||
formats = self._extract_m3u8_formats(
|
formats = self._extract_m3u8_formats(
|
||||||
'%s/vod/%s.m3u8?%s' % (
|
'%s/vod/%s.m3u8?%s' % (
|
||||||
|
@ -289,8 +344,8 @@ class TwitchVodIE(TwitchBaseIE):
|
||||||
'allow_spectre': 'true',
|
'allow_spectre': 'true',
|
||||||
'player': 'twitchweb',
|
'player': 'twitchweb',
|
||||||
'playlist_include_framerate': 'true',
|
'playlist_include_framerate': 'true',
|
||||||
'nauth': access_token['token'],
|
'nauth': access_token['value'],
|
||||||
'nauthsig': access_token['sig'],
|
'nauthsig': access_token['signature'],
|
||||||
})),
|
})),
|
||||||
vod_id, 'mp4', entry_protocol='m3u8_native')
|
vod_id, 'mp4', entry_protocol='m3u8_native')
|
||||||
|
|
||||||
|
@ -333,36 +388,6 @@ def _make_video_result(node):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class TwitchGraphQLBaseIE(TwitchBaseIE):
|
|
||||||
_PAGE_LIMIT = 100
|
|
||||||
|
|
||||||
_OPERATION_HASHES = {
|
|
||||||
'CollectionSideBar': '27111f1b382effad0b6def325caef1909c733fe6a4fbabf54f8d491ef2cf2f14',
|
|
||||||
'FilterableVideoTower_Videos': 'a937f1d22e269e39a03b509f65a7490f9fc247d7f83d6ac1421523e3b68042cb',
|
|
||||||
'ClipsCards__User': 'b73ad2bfaecfd30a9e6c28fada15bd97032c83ec77a0440766a56fe0bd632777',
|
|
||||||
'ChannelCollectionsContent': '07e3691a1bad77a36aba590c351180439a40baefc1c275356f40fc7082419a84',
|
|
||||||
'StreamMetadata': '1c719a40e481453e5c48d9bb585d971b8b372f8ebb105b17076722264dfa5b3e',
|
|
||||||
'ComscoreStreamingQuery': 'e1edae8122517d013405f237ffcc124515dc6ded82480a88daef69c83b53ac01',
|
|
||||||
'VideoPreviewOverlay': '3006e77e51b128d838fa4e835723ca4dc9a05c5efd4466c1085215c6e437e65c',
|
|
||||||
}
|
|
||||||
|
|
||||||
def _download_gql(self, video_id, ops, note, fatal=True):
|
|
||||||
for op in ops:
|
|
||||||
op['extensions'] = {
|
|
||||||
'persistedQuery': {
|
|
||||||
'version': 1,
|
|
||||||
'sha256Hash': self._OPERATION_HASHES[op['operationName']],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return self._download_json(
|
|
||||||
'https://gql.twitch.tv/gql', video_id, note,
|
|
||||||
data=json.dumps(ops).encode(),
|
|
||||||
headers={
|
|
||||||
'Content-Type': 'text/plain;charset=UTF-8',
|
|
||||||
'Client-ID': self._CLIENT_ID,
|
|
||||||
}, fatal=fatal)
|
|
||||||
|
|
||||||
|
|
||||||
class TwitchCollectionIE(TwitchGraphQLBaseIE):
|
class TwitchCollectionIE(TwitchGraphQLBaseIE):
|
||||||
_VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/collections/(?P<id>[^/]+)'
|
_VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/collections/(?P<id>[^/]+)'
|
||||||
|
|
||||||
|
@ -814,8 +839,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(channel_name)
|
access_token = self._download_access_token_gql(channel_name)
|
||||||
token = access_token['token']
|
token = access_token['value']
|
||||||
|
|
||||||
stream_id = stream.get('id') or channel_name
|
stream_id = stream.get('id') or channel_name
|
||||||
query = {
|
query = {
|
||||||
|
@ -826,7 +851,7 @@ class TwitchStreamIE(TwitchGraphQLBaseIE):
|
||||||
'player': 'twitchweb',
|
'player': 'twitchweb',
|
||||||
'playlist_include_framerate': 'true',
|
'playlist_include_framerate': 'true',
|
||||||
'segment_preference': '4',
|
'segment_preference': '4',
|
||||||
'sig': access_token['sig'].encode('utf-8'),
|
'sig': access_token['signature'].encode('utf-8'),
|
||||||
'token': token.encode('utf-8'),
|
'token': token.encode('utf-8'),
|
||||||
}
|
}
|
||||||
formats = self._extract_m3u8_formats(
|
formats = self._extract_m3u8_formats(
|
||||||
|
@ -866,7 +891,7 @@ class TwitchStreamIE(TwitchGraphQLBaseIE):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class TwitchClipsIE(TwitchBaseIE):
|
class TwitchClipsIE(TwitchGraphQLBaseIE):
|
||||||
IE_NAME = 'twitch:clips'
|
IE_NAME = 'twitch:clips'
|
||||||
_VALID_URL = r'''(?x)
|
_VALID_URL = r'''(?x)
|
||||||
https?://
|
https?://
|
||||||
|
@ -912,8 +937,8 @@ class TwitchClipsIE(TwitchBaseIE):
|
||||||
def _real_extract(self, url):
|
def _real_extract(self, url):
|
||||||
video_id = self._match_id(url)
|
video_id = self._match_id(url)
|
||||||
|
|
||||||
clip = self._download_json(
|
clip = self._download_base_gql(
|
||||||
'https://gql.twitch.tv/gql', video_id, data=json.dumps({
|
video_id, {
|
||||||
'query': '''{
|
'query': '''{
|
||||||
clip(slug: "%s") {
|
clip(slug: "%s") {
|
||||||
broadcaster {
|
broadcaster {
|
||||||
|
@ -937,10 +962,7 @@ class TwitchClipsIE(TwitchBaseIE):
|
||||||
}
|
}
|
||||||
viewCount
|
viewCount
|
||||||
}
|
}
|
||||||
}''' % video_id,
|
}''' % video_id}, 'Downloading clip GraphQL')['data']['clip']
|
||||||
}).encode(), headers={
|
|
||||||
'Client-ID': self._CLIENT_ID,
|
|
||||||
})['data']['clip']
|
|
||||||
|
|
||||||
if not clip:
|
if not clip:
|
||||||
raise ExtractorError(
|
raise ExtractorError(
|
||||||
|
|
Loading…
Reference in a new issue