Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[twitch] Switch access token to GraphQL and refactor. #27646

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 68 additions & 46 deletions youtube_dl/extractor/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,64 @@ 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, 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'
_VALID_URL = r'''(?x)
https?://
Expand Down Expand Up @@ -276,9 +333,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_access_token_gql(vod_id, self._ITEM_TYPE)

formats = self._extract_m3u8_formats(
'%s/vod/%s.m3u8?%s' % (
Expand All @@ -289,8 +344,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 +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):
_VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/collections/(?P<id>[^/]+)'

Expand Down Expand Up @@ -814,8 +839,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_access_token_gql(channel_name)
token = access_token['value']

stream_id = stream.get('id') or channel_name
query = {
Expand All @@ -826,7 +851,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 +891,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,8 +937,8 @@ 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 {
Expand All @@ -937,10 +962,7 @@ def _real_extract(self, url):
}
viewCount
}
}''' % video_id,
}).encode(), headers={
'Client-ID': self._CLIENT_ID,
})['data']['clip']
}''' % video_id}, 'Downloading clip GraphQL')['data']['clip']

if not clip:
raise ExtractorError(
Expand Down