mirror of
				https://github.com/ytdl-org/youtube-dl
				synced 2025-11-04 03:33:46 +00:00 
			
		
		
		
	[twitch] Refactor
This commit is contained in:
		
							parent
							
								
									8f20ad36dc
								
							
						
					
					
						commit
						4ae243fc6c
					
				@ -41,6 +41,17 @@ class TwitchBaseIE(InfoExtractor):
 | 
				
			|||||||
    _CLIENT_ID = 'kimne78kx3ncx6brgo4mv6wki5h1ko'
 | 
					    _CLIENT_ID = 'kimne78kx3ncx6brgo4mv6wki5h1ko'
 | 
				
			||||||
    _NETRC_MACHINE = 'twitch'
 | 
					    _NETRC_MACHINE = 'twitch'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _OPERATION_HASHES = {
 | 
				
			||||||
 | 
					        'CollectionSideBar': '27111f1b382effad0b6def325caef1909c733fe6a4fbabf54f8d491ef2cf2f14',
 | 
				
			||||||
 | 
					        'FilterableVideoTower_Videos': 'a937f1d22e269e39a03b509f65a7490f9fc247d7f83d6ac1421523e3b68042cb',
 | 
				
			||||||
 | 
					        'ClipsCards__User': 'b73ad2bfaecfd30a9e6c28fada15bd97032c83ec77a0440766a56fe0bd632777',
 | 
				
			||||||
 | 
					        'ChannelCollectionsContent': '07e3691a1bad77a36aba590c351180439a40baefc1c275356f40fc7082419a84',
 | 
				
			||||||
 | 
					        'StreamMetadata': '1c719a40e481453e5c48d9bb585d971b8b372f8ebb105b17076722264dfa5b3e',
 | 
				
			||||||
 | 
					        'ComscoreStreamingQuery': 'e1edae8122517d013405f237ffcc124515dc6ded82480a88daef69c83b53ac01',
 | 
				
			||||||
 | 
					        'VideoPreviewOverlay': '3006e77e51b128d838fa4e835723ca4dc9a05c5efd4466c1085215c6e437e65c',
 | 
				
			||||||
 | 
					        'VideoMetadata': '226edb3e692509f727fd56821f5653c05740242c82b0388883e0c0e75dcbf687',
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _real_initialize(self):
 | 
					    def _real_initialize(self):
 | 
				
			||||||
        self._login()
 | 
					        self._login()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -125,21 +136,6 @@ class TwitchBaseIE(InfoExtractor):
 | 
				
			|||||||
                    })
 | 
					                    })
 | 
				
			||||||
        self._sort_formats(formats)
 | 
					        self._sort_formats(formats)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
class TwitchGraphQLBaseIE(TwitchBaseIE):
 | 
					 | 
				
			||||||
    _PAGE_LIMIT = 100
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    _OPERATION_HASHES = {
 | 
					 | 
				
			||||||
        'CollectionSideBar': '27111f1b382effad0b6def325caef1909c733fe6a4fbabf54f8d491ef2cf2f14',
 | 
					 | 
				
			||||||
        'FilterableVideoTower_Videos': 'a937f1d22e269e39a03b509f65a7490f9fc247d7f83d6ac1421523e3b68042cb',
 | 
					 | 
				
			||||||
        'ClipsCards__User': 'b73ad2bfaecfd30a9e6c28fada15bd97032c83ec77a0440766a56fe0bd632777',
 | 
					 | 
				
			||||||
        'ChannelCollectionsContent': '07e3691a1bad77a36aba590c351180439a40baefc1c275356f40fc7082419a84',
 | 
					 | 
				
			||||||
        'StreamMetadata': '1c719a40e481453e5c48d9bb585d971b8b372f8ebb105b17076722264dfa5b3e',
 | 
					 | 
				
			||||||
        'ComscoreStreamingQuery': 'e1edae8122517d013405f237ffcc124515dc6ded82480a88daef69c83b53ac01',
 | 
					 | 
				
			||||||
        'VideoPreviewOverlay': '3006e77e51b128d838fa4e835723ca4dc9a05c5efd4466c1085215c6e437e65c',
 | 
					 | 
				
			||||||
        'VideoMetadata': '226edb3e692509f727fd56821f5653c05740242c82b0388883e0c0e75dcbf687',
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def _download_base_gql(self, video_id, ops, note, fatal=True):
 | 
					    def _download_base_gql(self, video_id, ops, note, fatal=True):
 | 
				
			||||||
        return self._download_json(
 | 
					        return self._download_json(
 | 
				
			||||||
            'https://gql.twitch.tv/gql', video_id, note,
 | 
					            'https://gql.twitch.tv/gql', video_id, note,
 | 
				
			||||||
@ -159,7 +155,7 @@ 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, token_kind, param_name):
 | 
					    def _download_access_token(self, video_id, token_kind, param_name):
 | 
				
			||||||
        method = '%sPlaybackAccessToken' % token_kind
 | 
					        method = '%sPlaybackAccessToken' % token_kind
 | 
				
			||||||
        ops = {
 | 
					        ops = {
 | 
				
			||||||
            'query': '''{
 | 
					            'query': '''{
 | 
				
			||||||
@ -182,7 +178,7 @@ class TwitchGraphQLBaseIE(TwitchBaseIE):
 | 
				
			|||||||
            'Downloading %s access token GraphQL' % token_kind)['data'][method]
 | 
					            'Downloading %s access token GraphQL' % token_kind)['data'][method]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TwitchVodIE(TwitchGraphQLBaseIE):
 | 
					class TwitchVodIE(TwitchBaseIE):
 | 
				
			||||||
    IE_NAME = 'twitch:vod'
 | 
					    IE_NAME = 'twitch:vod'
 | 
				
			||||||
    _VALID_URL = r'''(?x)
 | 
					    _VALID_URL = r'''(?x)
 | 
				
			||||||
                    https?://
 | 
					                    https?://
 | 
				
			||||||
@ -249,7 +245,7 @@ class TwitchVodIE(TwitchGraphQLBaseIE):
 | 
				
			|||||||
        'only_matching': True,
 | 
					        'only_matching': True,
 | 
				
			||||||
    }]
 | 
					    }]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _download_info_gql(self, item_id):
 | 
					    def _download_info(self, item_id):
 | 
				
			||||||
        data = self._download_gql(
 | 
					        data = self._download_gql(
 | 
				
			||||||
            item_id, [{
 | 
					            item_id, [{
 | 
				
			||||||
                'operationName': 'VideoMetadata',
 | 
					                'operationName': 'VideoMetadata',
 | 
				
			||||||
@ -327,8 +323,8 @@ class TwitchVodIE(TwitchGraphQLBaseIE):
 | 
				
			|||||||
    def _real_extract(self, url):
 | 
					    def _real_extract(self, url):
 | 
				
			||||||
        vod_id = self._match_id(url)
 | 
					        vod_id = self._match_id(url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        info = self._download_info_gql(vod_id)
 | 
					        info = self._download_info(vod_id)
 | 
				
			||||||
        access_token = self._download_access_token_gql(vod_id, 'video', 'id')
 | 
					        access_token = self._download_access_token(vod_id, 'video', 'id')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        formats = self._extract_m3u8_formats(
 | 
					        formats = self._extract_m3u8_formats(
 | 
				
			||||||
            '%s/vod/%s.m3u8?%s' % (
 | 
					            '%s/vod/%s.m3u8?%s' % (
 | 
				
			||||||
@ -383,7 +379,7 @@ def _make_video_result(node):
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TwitchCollectionIE(TwitchGraphQLBaseIE):
 | 
					class TwitchCollectionIE(TwitchBaseIE):
 | 
				
			||||||
    _VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/collections/(?P<id>[^/]+)'
 | 
					    _VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/collections/(?P<id>[^/]+)'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _TESTS = [{
 | 
					    _TESTS = [{
 | 
				
			||||||
@ -420,7 +416,9 @@ class TwitchCollectionIE(TwitchGraphQLBaseIE):
 | 
				
			|||||||
            entries, playlist_id=collection_id, playlist_title=title)
 | 
					            entries, playlist_id=collection_id, playlist_title=title)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TwitchPlaylistBaseIE(TwitchGraphQLBaseIE):
 | 
					class TwitchPlaylistBaseIE(TwitchBaseIE):
 | 
				
			||||||
 | 
					    _PAGE_LIMIT = 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _entries(self, channel_name, *args):
 | 
					    def _entries(self, channel_name, *args):
 | 
				
			||||||
        cursor = None
 | 
					        cursor = None
 | 
				
			||||||
        variables_common = self._make_variables(channel_name, *args)
 | 
					        variables_common = self._make_variables(channel_name, *args)
 | 
				
			||||||
@ -701,7 +699,7 @@ class TwitchVideosCollectionsIE(TwitchPlaylistBaseIE):
 | 
				
			|||||||
            playlist_title='%s - Collections' % channel_name)
 | 
					            playlist_title='%s - Collections' % channel_name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TwitchStreamIE(TwitchGraphQLBaseIE):
 | 
					class TwitchStreamIE(TwitchBaseIE):
 | 
				
			||||||
    IE_NAME = 'twitch:stream'
 | 
					    IE_NAME = 'twitch:stream'
 | 
				
			||||||
    _VALID_URL = r'''(?x)
 | 
					    _VALID_URL = r'''(?x)
 | 
				
			||||||
                    https?://
 | 
					                    https?://
 | 
				
			||||||
@ -791,7 +789,7 @@ 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(
 | 
					        access_token = self._download_access_token(
 | 
				
			||||||
            channel_name, 'stream', 'channelName')
 | 
					            channel_name, 'stream', 'channelName')
 | 
				
			||||||
        token = access_token['value']
 | 
					        token = access_token['value']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -844,7 +842,7 @@ class TwitchStreamIE(TwitchGraphQLBaseIE):
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TwitchClipsIE(TwitchGraphQLBaseIE):
 | 
					class TwitchClipsIE(TwitchBaseIE):
 | 
				
			||||||
    IE_NAME = 'twitch:clips'
 | 
					    IE_NAME = 'twitch:clips'
 | 
				
			||||||
    _VALID_URL = r'''(?x)
 | 
					    _VALID_URL = r'''(?x)
 | 
				
			||||||
                    https?://
 | 
					                    https?://
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user