Skip to content

Commit

Permalink
[twitch] Switch access token to GraphQL and refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
23rd committed Jan 3, 2021
1 parent 19ec468 commit dfcf44e
Showing 1 changed file with 96 additions and 75 deletions.
171 changes: 96 additions & 75 deletions youtube_dl/extractor/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,71 @@ def _prefer_source(self, formats):
})
self._sort_formats(formats)

def _download_access_token(self, channel_name):
return self._call_api(
'api/channels/%s/access_token' % channel_name, channel_name,
'Downloading access token JSON')

def _extract_channel_id(self, token, channel_name):
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, method, param_name, note):
ops = {
'query': '''{
%s(
%s: \"%s\",
params: {
platform: \"web\",
playerBackend: \"mediaplayer\",
playerType: \"site\"
}) {
value
signature
}
}''' % (method, param_name, video_id),
}
return self._download_base_gql(video_id, ops, note)['data'][method]

def _download_vod_access_token_gql(self, video_id):
return self._download_access_token_gql(
video_id, 'videoPlaybackAccessToken', 'id',
'Downloading %s access token JSON GraphQL' % video_id)

def _download_stream_access_token_gql(self, channel_name):
return self._download_access_token_gql(
channel_name, 'streamPlaybackAccessToken', 'channelName',
'Downloading access token JSON GraphQL')


class TwitchVodIE(TwitchGraphQLBaseIE):
IE_NAME = 'twitch:vod'
_VALID_URL = r'''(?x)
https?://
Expand Down Expand Up @@ -276,9 +331,7 @@ def _real_extract(self, url):
vod_id = self._match_id(url)

info = self._download_info(vod_id)
access_token = self._call_api(
'api/vods/%s/access_token' % vod_id, vod_id,
'Downloading %s access token' % self._ITEM_TYPE)
access_token = self._download_vod_access_token_gql(vod_id)

formats = self._extract_m3u8_formats(
'%s/vod/%s.m3u8?%s' % (
Expand All @@ -289,8 +342,8 @@ def _real_extract(self, url):
'allow_spectre': 'true',
'player': 'twitchweb',
'playlist_include_framerate': 'true',
'nauth': access_token['token'],
'nauthsig': access_token['sig'],
'nauth': access_token['value'],
'nauthsig': access_token['signature'],
})),
vod_id, 'mp4', entry_protocol='m3u8_native')

Expand Down Expand Up @@ -333,36 +386,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):
_VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/collections/(?P<id>[^/]+)'

Expand Down Expand Up @@ -442,8 +465,8 @@ def _entries(self, channel_name, *args):

# Deprecated kraken v5 API
def _entries_kraken(self, channel_name, broadcast_type, sort):
access_token = self._download_access_token(channel_name)
channel_id = self._extract_channel_id(access_token['token'], channel_name)
access_token = self._download_stream_access_token_gql(channel_name)
channel_id = self._extract_channel_id(access_token['value'], channel_name)
offset = 0
counter_override = None
for counter in itertools.count(1):
Expand Down Expand Up @@ -814,8 +837,8 @@ def _real_extract(self, url):
if not stream:
raise ExtractorError('%s is offline' % channel_name, expected=True)

access_token = self._download_access_token(channel_name)
token = access_token['token']
access_token = self._download_stream_access_token_gql(channel_name)
token = access_token['value']

stream_id = stream.get('id') or channel_name
query = {
Expand All @@ -826,7 +849,7 @@ def _real_extract(self, url):
'player': 'twitchweb',
'playlist_include_framerate': 'true',
'segment_preference': '4',
'sig': access_token['sig'].encode('utf-8'),
'sig': access_token['signature'].encode('utf-8'),
'token': token.encode('utf-8'),
}
formats = self._extract_m3u8_formats(
Expand Down Expand Up @@ -866,7 +889,7 @@ def _real_extract(self, url):
}


class TwitchClipsIE(TwitchBaseIE):
class TwitchClipsIE(TwitchGraphQLBaseIE):
IE_NAME = 'twitch:clips'
_VALID_URL = r'''(?x)
https?://
Expand Down Expand Up @@ -912,35 +935,33 @@ class TwitchClipsIE(TwitchBaseIE):
def _real_extract(self, url):
video_id = self._match_id(url)

clip = self._download_json(
'https://gql.twitch.tv/gql', video_id, data=json.dumps({
clip = self._download_base_gql(
video_id, {
'query': '''{
clip(slug: "%s") {
broadcaster {
displayName
}
createdAt
curator {
displayName
id
}
durationSeconds
id
tiny: thumbnailURL(width: 86, height: 45)
small: thumbnailURL(width: 260, height: 147)
medium: thumbnailURL(width: 480, height: 272)
title
videoQualities {
frameRate
quality
sourceURL
}
viewCount
}
}''' % video_id,
}).encode(), headers={
'Client-ID': self._CLIENT_ID,
})['data']['clip']
clip(slug: "%s") {
broadcaster {
displayName
}
createdAt
curator {
displayName
id
}
durationSeconds
id
tiny: thumbnailURL(width: 86, height: 45)
small: thumbnailURL(width: 260, height: 147)
medium: thumbnailURL(width: 480, height: 272)
title
videoQualities {
frameRate
quality
sourceURL
}
viewCount
}
}''' % video_id
}, 'Downloading clip GraphQL')['data']['clip']

if not clip:
raise ExtractorError(
Expand Down

0 comments on commit dfcf44e

Please sign in to comment.