2 from __future__
import unicode_literals
10 from .common
import InfoExtractor
11 from ..compat
import (
15 compat_urllib_parse_urlencode
,
16 compat_urllib_parse_urlparse
,
35 class TwitchBaseIE(InfoExtractor
):
36 _VALID_URL_BASE
= r
'https?://(?:(?:www|go|m)\.)?twitch\.tv'
38 _API_BASE
= 'https://api.twitch.tv'
39 _USHER_BASE
= 'https://usher.ttvnw.net'
40 _LOGIN_FORM_URL
= 'https://www.twitch.tv/login'
41 _LOGIN_POST_URL
= 'https://passport.twitch.tv/login'
42 _CLIENT_ID
= 'kimne78kx3ncx6brgo4mv6wki5h1ko'
43 _NETRC_MACHINE
= 'twitch'
46 'CollectionSideBar': '27111f1b382effad0b6def325caef1909c733fe6a4fbabf54f8d491ef2cf2f14',
47 'FilterableVideoTower_Videos': 'a937f1d22e269e39a03b509f65a7490f9fc247d7f83d6ac1421523e3b68042cb',
48 'ClipsCards__User': 'b73ad2bfaecfd30a9e6c28fada15bd97032c83ec77a0440766a56fe0bd632777',
49 'ChannelCollectionsContent': '07e3691a1bad77a36aba590c351180439a40baefc1c275356f40fc7082419a84',
50 'StreamMetadata': '1c719a40e481453e5c48d9bb585d971b8b372f8ebb105b17076722264dfa5b3e',
51 'ComscoreStreamingQuery': 'e1edae8122517d013405f237ffcc124515dc6ded82480a88daef69c83b53ac01',
52 'VideoPreviewOverlay': '3006e77e51b128d838fa4e835723ca4dc9a05c5efd4466c1085215c6e437e65c',
53 'VideoMetadata': '226edb3e692509f727fd56821f5653c05740242c82b0388883e0c0e75dcbf687',
56 def _real_initialize(self
):
60 username
, password
= self
._get
_login
_info
()
66 'Unable to login. Twitch said: %s' % message
, expected
=True)
68 def login_step(page
, urlh
, note
, data
):
69 form
= self
._hidden
_inputs
(page
)
72 page_url
= urlh
.geturl()
73 post_url
= self
._search
_regex
(
74 r
'<form[^>]+action=(["\'])(?P
<url
>.+?
)\
1', page,
75 'post url
', default=self._LOGIN_POST_URL, group='url
')
76 post_url = urljoin(page_url, post_url)
80 'Origin
': 'https
://www
.twitch
.tv
',
81 'Content
-Type
': 'text
/plain
;charset
=UTF
-8',
84 response = self._download_json(
85 post_url, None, note, data=json.dumps(form).encode(),
86 headers=headers, expected_status=400)
87 error = dict_get(response, ('error
', 'error_description
', 'error_code
'))
91 if 'Authenticated successfully
' in response.get('message
', ''):
94 redirect_url = urljoin(
96 response.get('redirect
') or response['redirect_path
'])
97 return self._download_webpage_handle(
98 redirect_url, None, 'Downloading login redirect page
',
101 login_page, handle = self._download_webpage_handle(
102 self._LOGIN_FORM_URL, None, 'Downloading login page
')
104 # Some TOR nodes and public proxies are blocked completely
105 if 'blacklist_message
' in login_page:
106 fail(clean_html(login_page))
108 redirect_page, handle = login_step(
109 login_page, handle, 'Logging
in', {
110 'username
': username,
111 'password
': password,
112 'client_id
': self._CLIENT_ID,
116 if not redirect_page:
119 if re.search(r'(?i
)<form
[^
>]+id="two-factor-submit"', redirect_page) is not None:
120 # TODO: Add mechanism to request an SMS or phone call
121 tfa_token = self._get_tfa_info('two
-factor authentication token
')
122 login_step(redirect_page, handle, 'Submitting TFA token
', {
123 'authy_token
': tfa_token,
124 'remember_2fa
': 'true
',
127 def _prefer_source(self, formats):
129 source = next(f for f in formats if f['format_id
'] == 'Source
')
130 source['quality
'] = 10
131 except StopIteration:
133 if '/chunked
/' in f['url
']:
136 'format_note
': 'Source
',
138 self._sort_formats(formats)
140 def _download_base_gql(self, video_id, ops, note, fatal=True):
142 'Content
-Type
': 'text
/plain
;charset
=UTF
-8',
143 'Client
-ID
': self._CLIENT_ID,
145 gql_auth = self._get_cookies('https
://gql
.twitch
.tv
').get('auth
-token
')
147 headers['Authorization
'] = 'OAuth
' + gql_auth.value
148 return self._download_json(
149 'https
://gql
.twitch
.tv
/gql
', video_id, note,
150 data=json.dumps(ops).encode(),
151 headers=headers, fatal=fatal)
153 def _download_gql(self, video_id, ops, note, fatal=True):
158 'sha256Hash
': self._OPERATION_HASHES[op['operationName
']],
161 return self._download_base_gql(video_id, ops, note)
163 def _download_access_token(self, video_id, token_kind, param_name):
164 method = '%sPlaybackAccessToken
' % token_kind
171 playerBackend: "mediaplayer",
179 }''' % (method, param_name, video_id),
181 return self._download_base_gql(
183 'Downloading
%s access token GraphQL
' % token_kind)['data
'][method]
186 class TwitchVodIE(TwitchBaseIE):
187 IE_NAME = 'twitch
:vod
'
188 _VALID_URL = r'''(?x)
191 (?:(?:www|go|m)\.)?twitch\.tv/(?:[^/]+/v(?:ideo)?|videos)/|
192 player\.twitch\.tv/\?.*?\bvideo=v?
198 'url
': 'http
://www
.twitch
.tv
/riotgames
/v
/6528877?t
=5m10s
',
202 'title
': 'LCK Summer Split
- Week
6 Day
1',
203 'thumbnail
': r're
:^https?
://.*\
.jpg$
',
205 'timestamp
': 1435131734,
206 'upload_date
': '20150624',
207 'uploader
': 'Riot Games
',
208 'uploader_id
': 'riotgames
',
214 'skip_download
': True,
217 # Untitled broadcast (title is None)
218 'url
': 'http
://www
.twitch
.tv
/belkao_o
/v
/11230755',
222 'title
': 'Untitled Broadcast
',
223 'thumbnail
': r're
:^https?
://.*\
.jpg$
',
225 'timestamp
': 1439746708,
226 'upload_date
': '20150816',
227 'uploader
': 'BelkAO_o
',
228 'uploader_id
': 'belkao_o
',
233 'skip_download
': True,
235 'skip
': 'HTTP Error
404: Not Found
',
237 'url
': 'http
://player
.twitch
.tv
/?t
=5m10s
&video
=v6528877
',
238 'only_matching
': True,
240 'url
': 'https
://www
.twitch
.tv
/videos
/6528877',
241 'only_matching
': True,
243 'url
': 'https
://m
.twitch
.tv
/beagsandjam
/v
/247478721',
244 'only_matching
': True,
246 'url
': 'https
://www
.twitch
.tv
/northernlion
/video
/291940395',
247 'only_matching
': True,
249 'url
': 'https
://player
.twitch
.tv
/?video
=480452374',
250 'only_matching
': True,
253 def _download_info(self, item_id):
254 data = self._download_gql(
256 'operationName
': 'VideoMetadata
',
262 'Downloading stream metadata GraphQL
')[0]['data
']
263 video = data.get('video
')
265 raise ExtractorError(
266 'Video
%s does
not exist
' % item_id, expected=True)
267 return self._extract_info_gql(video, item_id)
270 def _extract_info(info):
271 status = info.get('status
')
272 if status == 'recording
':
274 elif status == 'recorded
':
278 _QUALITIES = ('small
', 'medium
', 'large
')
279 quality_key = qualities(_QUALITIES)
281 preview = info.get('preview
')
282 if isinstance(preview, dict):
283 for thumbnail_id, thumbnail_url in preview.items():
284 thumbnail_url = url_or_none(thumbnail_url)
285 if not thumbnail_url:
287 if thumbnail_id not in _QUALITIES:
290 'url
': thumbnail_url,
291 'preference
': quality_key(thumbnail_id),
295 'title
': info.get('title
') or 'Untitled Broadcast
',
296 'description
': info.get('description
'),
297 'duration
': int_or_none(info.get('length
')),
298 'thumbnails
': thumbnails,
299 'uploader
': info.get('channel
', {}).get('display_name
'),
300 'uploader_id
': info.get('channel
', {}).get('name
'),
301 'timestamp
': parse_iso8601(info.get('recorded_at
')),
302 'view_count
': int_or_none(info.get('views
')),
307 def _extract_info_gql(info, item_id):
308 vod_id = info.get('id') or item_id
309 # id backward compatibility for download archives
311 vod_id = 'v
%s' % vod_id
312 thumbnail = url_or_none(info.get('previewThumbnailURL
'))
314 for p in ('width
', 'height
'):
315 thumbnail = thumbnail.replace('{%s}
' % p, '0')
318 'title
': info.get('title
') or 'Untitled Broadcast
',
319 'description
': info.get('description
'),
320 'duration
': int_or_none(info.get('lengthSeconds
')),
321 'thumbnail
': thumbnail,
322 'uploader
': try_get(info, lambda x: x['owner
']['displayName
'], compat_str),
323 'uploader_id
': try_get(info, lambda x: x['owner
']['login
'], compat_str),
324 'timestamp
': unified_timestamp(info.get('publishedAt
')),
325 'view_count
': int_or_none(info.get('viewCount
')),
328 def _real_extract(self, url):
329 vod_id = self._match_id(url)
331 info = self._download_info(vod_id)
332 access_token = self._download_access_token(vod_id, 'video
', 'id')
334 formats = self._extract_m3u8_formats(
335 '%s/vod
/%s.m3u8?
%s' % (
336 self._USHER_BASE, vod_id,
337 compat_urllib_parse_urlencode({
338 'allow_source
': 'true
',
339 'allow_audio_only
': 'true
',
340 'allow_spectre
': 'true
',
341 'player
': 'twitchweb
',
342 'playlist_include_framerate
': 'true
',
343 'nauth
': access_token['value
'],
344 'nauthsig
': access_token['signature
'],
346 vod_id, 'mp4
', entry_protocol='m3u8_native
')
348 self._prefer_source(formats)
349 info['formats
'] = formats
351 parsed_url = compat_urllib_parse_urlparse(url)
352 query = compat_parse_qs(parsed_url.query)
354 info['start_time
'] = parse_duration(query['t
'][0])
356 if info.get('timestamp
') is not None:
357 info['subtitles
'] = {
359 'url
': update_url_query(
360 'https
://api
.twitch
.tv
/v5
/videos
/%s/comments
' % vod_id, {
361 'client_id
': self._CLIENT_ID,
370 def _make_video_result(node):
371 assert isinstance(node, dict)
372 video_id = node.get('id')
376 '_type
': 'url_transparent
',
377 'ie_key
': TwitchVodIE.ie_key(),
378 'id': 'v
' + video_id,
379 'url
': 'https
://www
.twitch
.tv
/videos
/%s' % video_id,
380 'title
': node.get('title
'),
381 'thumbnail
': node.get('previewThumbnailURL
'),
382 'duration
': float_or_none(node.get('lengthSeconds
')),
383 'view_count
': int_or_none(node.get('viewCount
')),
387 class TwitchCollectionIE(TwitchBaseIE):
388 _VALID_URL = r'https?
://(?
:(?
:www|go|m
)\
.)?twitch\
.tv
/collections
/(?P
<id>[^
/]+)'
391 'url
': 'https
://www
.twitch
.tv
/collections
/wlDCoH0zEBZZbQ
',
393 'id': 'wlDCoH0zEBZZbQ
',
394 'title
': 'Overthrow Nook
, capitalism
for children
',
396 'playlist_mincount
': 13,
399 _OPERATION_NAME = 'CollectionSideBar
'
401 def _real_extract(self, url):
402 collection_id = self._match_id(url)
403 collection = self._download_gql(
405 'operationName
': self._OPERATION_NAME,
406 'variables
': {'collectionID': collection_id},
408 'Downloading collection GraphQL
')[0]['data
']['collection
']
409 title = collection.get('title
')
411 for edge in collection['items
']['edges
']:
412 if not isinstance(edge, dict):
414 node = edge.get('node
')
415 if not isinstance(node, dict):
417 video = _make_video_result(node)
419 entries.append(video)
420 return self.playlist_result(
421 entries, playlist_id=collection_id, playlist_title=title)
424 class TwitchPlaylistBaseIE(TwitchBaseIE):
427 def _entries(self, channel_name, *args):
429 variables_common = self._make_variables(channel_name, *args)
430 entries_key = '%ss' % self._ENTRY_KIND
431 for page_num in itertools.count(1):
432 variables = variables_common.copy()
433 variables['limit
'] = self._PAGE_LIMIT
435 variables['cursor
'] = cursor
436 page = self._download_gql(
438 'operationName
': self._OPERATION_NAME,
439 'variables
': variables,
441 'Downloading
%ss GraphQL page
%s' % (self._NODE_KIND, page_num),
446 page, lambda x: x[0]['data
']['user
'][entries_key]['edges
'], list)
450 if not isinstance(edge, dict):
452 if edge.get('__typename
') != self._EDGE_KIND:
454 node = edge.get('node
')
455 if not isinstance(node, dict):
457 if node.get('__typename
') != self._NODE_KIND:
459 entry = self._extract_entry(node)
461 cursor = edge.get('cursor
')
463 if not cursor or not isinstance(cursor, compat_str):
467 class TwitchVideosIE(TwitchPlaylistBaseIE):
468 _VALID_URL = r'https?
://(?
:(?
:www|go|m
)\
.)?twitch\
.tv
/(?P
<id>[^
/]+)/(?
:videos|profile
)'
471 # All Videos sorted by Date
472 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=all
',
475 'title
': 'spamfish
- All Videos
sorted by Date
',
477 'playlist_mincount
': 924,
479 # All Videos sorted by Popular
480 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=all
&sort
=views
',
483 'title
': 'spamfish
- All Videos
sorted by Popular
',
485 'playlist_mincount
': 931,
487 # Past Broadcasts sorted by Date
488 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=archives
',
491 'title
': 'spamfish
- Past Broadcasts
sorted by Date
',
493 'playlist_mincount
': 27,
495 # Highlights sorted by Date
496 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=highlights
',
499 'title
': 'spamfish
- Highlights
sorted by Date
',
501 'playlist_mincount
': 901,
503 # Uploads sorted by Date
504 'url
': 'https
://www
.twitch
.tv
/esl_csgo
/videos?
filter=uploads
&sort
=time
',
507 'title
': 'esl_csgo
- Uploads
sorted by Date
',
509 'playlist_mincount
': 5,
511 # Past Premieres sorted by Date
512 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=past_premieres
',
515 'title
': 'spamfish
- Past Premieres
sorted by Date
',
517 'playlist_mincount
': 1,
519 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos
/all
',
520 'only_matching
': True,
522 'url
': 'https
://m
.twitch
.tv
/spamfish
/videos
/all
',
523 'only_matching
': True,
525 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos
',
526 'only_matching
': True,
529 Broadcast = collections.namedtuple('Broadcast
', ['type', 'label
'])
531 _DEFAULT_BROADCAST = Broadcast(None, 'All Videos
')
533 'archives
': Broadcast('ARCHIVE
', 'Past Broadcasts
'),
534 'highlights
': Broadcast('HIGHLIGHT
', 'Highlights
'),
535 'uploads
': Broadcast('UPLOAD
', 'Uploads
'),
536 'past_premieres
': Broadcast('PAST_PREMIERE
', 'Past Premieres
'),
537 'all
': _DEFAULT_BROADCAST,
540 _DEFAULT_SORTED_BY = 'Date
'
542 'time
': _DEFAULT_SORTED_BY,
546 _OPERATION_NAME = 'FilterableVideoTower_Videos
'
547 _ENTRY_KIND = 'video
'
548 _EDGE_KIND = 'VideoEdge
'
552 def suitable(cls, url):
554 if any(ie.suitable(url) for ie in (
556 TwitchVideosCollectionsIE))
557 else super(TwitchVideosIE, cls).suitable(url))
560 def _make_variables(channel_name, broadcast_type, sort):
562 'channelOwnerLogin
': channel_name,
563 'broadcastType
': broadcast_type,
564 'videoSort
': sort.upper(),
568 def _extract_entry(node):
569 return _make_video_result(node)
571 def _real_extract(self, url):
572 channel_name = self._match_id(url)
573 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
574 filter = qs.get('filter', ['all
'])[0]
575 sort = qs.get('sort
', ['time
'])[0]
576 broadcast = self._BROADCASTS.get(filter, self._DEFAULT_BROADCAST)
577 return self.playlist_result(
578 self._entries(channel_name, broadcast.type, sort),
579 playlist_id=channel_name,
580 playlist_title='%s - %s sorted by
%s'
581 % (channel_name, broadcast.label,
582 self._SORTED_BY.get(sort, self._DEFAULT_SORTED_BY)))
585 class TwitchVideosClipsIE(TwitchPlaylistBaseIE):
586 _VALID_URL = r'https?
://(?
:(?
:www|go|m
)\
.)?twitch\
.tv
/(?P
<id>[^
/]+)/(?
:clips|videos
/*?
\?.*?
\bfilter
=clips
)'
590 'url
': 'https
://www
.twitch
.tv
/vanillatv
/clips?
filter=clips
&range=all
',
593 'title
': 'vanillatv
- Clips Top All
',
595 'playlist_mincount
': 1,
597 'url
': 'https
://www
.twitch
.tv
/dota2ruhub
/videos?
filter=clips
&range=7d
',
598 'only_matching
': True,
601 Clip = collections.namedtuple('Clip
', ['filter', 'label
'])
603 _DEFAULT_CLIP = Clip('LAST_WEEK
', 'Top
7D
')
605 '24hr
': Clip('LAST_DAY
', 'Top
24H
'),
607 '30d
': Clip('LAST_MONTH
', 'Top
30D
'),
608 'all
': Clip('ALL_TIME
', 'Top All
'),
611 # NB: values other than 20 result in skipped videos
614 _OPERATION_NAME = 'ClipsCards__User
'
616 _EDGE_KIND = 'ClipEdge
'
620 def _make_variables(channel_name, filter):
622 'login
': channel_name,
629 def _extract_entry(node):
630 assert isinstance(node, dict)
631 clip_url = url_or_none(node.get('url
'))
635 '_type
': 'url_transparent
',
636 'ie_key
': TwitchClipsIE.ie_key(),
637 'id': node.get('id'),
639 'title
': node.get('title
'),
640 'thumbnail
': node.get('thumbnailURL
'),
641 'duration
': float_or_none(node.get('durationSeconds
')),
642 'timestamp
': unified_timestamp(node.get('createdAt
')),
643 'view_count
': int_or_none(node.get('viewCount
')),
644 'language
': node.get('language
'),
647 def _real_extract(self, url):
648 channel_name = self._match_id(url)
649 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
650 range = qs.get('range', ['7d
'])[0]
651 clip = self._RANGE.get(range, self._DEFAULT_CLIP)
652 return self.playlist_result(
653 self._entries(channel_name, clip.filter),
654 playlist_id=channel_name,
655 playlist_title='%s - Clips
%s' % (channel_name, clip.label))
658 class TwitchVideosCollectionsIE(TwitchPlaylistBaseIE):
659 _VALID_URL = r'https?
://(?
:(?
:www|go|m
)\
.)?twitch\
.tv
/(?P
<id>[^
/]+)/videos
/*?
\?.*?
\bfilter
=collections
'
663 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=collections
',
666 'title
': 'spamfish
- Collections
',
668 'playlist_mincount
': 3,
671 _OPERATION_NAME = 'ChannelCollectionsContent
'
672 _ENTRY_KIND = 'collection
'
673 _EDGE_KIND = 'CollectionsItemEdge
'
674 _NODE_KIND = 'Collection
'
677 def _make_variables(channel_name):
679 'ownerLogin
': channel_name,
683 def _extract_entry(node):
684 assert isinstance(node, dict)
685 collection_id = node.get('id')
686 if not collection_id:
689 '_type
': 'url_transparent
',
690 'ie_key
': TwitchCollectionIE.ie_key(),
692 'url
': 'https
://www
.twitch
.tv
/collections
/%s' % collection_id,
693 'title
': node.get('title
'),
694 'thumbnail
': node.get('thumbnailURL
'),
695 'duration
': float_or_none(node.get('lengthSeconds
')),
696 'timestamp
': unified_timestamp(node.get('updatedAt
')),
697 'view_count
': int_or_none(node.get('viewCount
')),
700 def _real_extract(self, url):
701 channel_name = self._match_id(url)
702 return self.playlist_result(
703 self._entries(channel_name), playlist_id=channel_name,
704 playlist_title='%s - Collections
' % channel_name)
707 class TwitchStreamIE(TwitchBaseIE):
708 IE_NAME = 'twitch
:stream
'
709 _VALID_URL = r'''(?x)
712 (?:(?:www|go|m)\.)?twitch\.tv/|
713 player\.twitch\.tv/\?.*?\bchannel=
719 'url
': 'http
://www
.twitch
.tv
/shroomztv
',
722 'display_id
': 'shroomztv
',
724 'title
': 're
:^ShroomzTV
[0-9]{4}
-[0-9]{2}
-[0-9]{2}
[0-9]{2}
:[0-9]{2}$
',
725 'description
': 'H1Z1
- lonewolfing
with ShroomzTV | A3 Battle Royale later
- @ShroomzTV',
727 'timestamp
': 1421928037,
728 'upload_date
': '20150122',
729 'uploader
': 'ShroomzTV
',
730 'uploader_id
': 'shroomztv
',
735 'skip_download
': True,
738 'url
': 'http
://www
.twitch
.tv
/miracle_doto
#profile-0',
739 'only_matching': True,
741 'url': 'https://player.twitch.tv/?channel=lotsofs',
742 'only_matching': True,
744 'url': 'https://go.twitch.tv/food',
745 'only_matching': True,
747 'url': 'https://m.twitch.tv/food',
748 'only_matching': True,
752 def suitable(cls
, url
):
754 if any(ie
.suitable(url
) for ie
in (
759 TwitchVideosCollectionsIE
,
761 else super(TwitchStreamIE
, cls
).suitable(url
))
763 def _real_extract(self
, url
):
764 channel_name
= self
._match
_id
(url
).lower()
766 gql
= self
._download
_gql
(
768 'operationName': 'StreamMetadata',
769 'variables': {'channelLogin': channel_name}
,
771 'operationName': 'ComscoreStreamingQuery',
773 'channel': channel_name
,
777 'isVodOrCollection': False,
781 'operationName': 'VideoPreviewOverlay',
782 'variables': {'login': channel_name}
,
784 'Downloading stream GraphQL')
786 user
= gql
[0]['data']['user']
789 raise ExtractorError(
790 '%s does not exist' % channel_name
, expected
=True)
792 stream
= user
['stream']
795 raise ExtractorError('%s is offline' % channel_name
, expected
=True)
797 access_token
= self
._download
_access
_token
(
798 channel_name
, 'stream', 'channelName')
799 token
= access_token
['value']
801 stream_id
= stream
.get('id') or channel_name
803 'allow_source': 'true',
804 'allow_audio_only': 'true',
805 'allow_spectre': 'true',
806 'p': random
.randint(1000000, 10000000),
807 'player': 'twitchweb',
808 'playlist_include_framerate': 'true',
809 'segment_preference': '4',
810 'sig': access_token
['signature'].encode('utf-8'),
811 'token': token
.encode('utf-8'),
813 formats
= self
._extract
_m
3u8_formats
(
814 '%s/api/channel/hls/%s.m3u8' % (self
._USHER
_BASE
, channel_name
),
815 stream_id
, 'mp4', query
=query
)
816 self
._prefer
_source
(formats
)
818 view_count
= stream
.get('viewers')
819 timestamp
= unified_timestamp(stream
.get('createdAt'))
821 sq_user
= try_get(gql
, lambda x
: x
[1]['data']['user'], dict) or {}
822 uploader
= sq_user
.get('displayName')
823 description
= try_get(
824 sq_user
, lambda x
: x
['broadcastSettings']['title'], compat_str
)
826 thumbnail
= url_or_none(try_get(
827 gql
, lambda x
: x
[2]['data']['user']['stream']['previewImageURL'],
830 title
= uploader
or channel_name
831 stream_type
= stream
.get('type')
832 if stream_type
in ['rerun', 'live']:
833 title
+= ' (%s)' % stream_type
837 'display_id': channel_name
,
838 'title': self
._live
_title
(title
),
839 'description': description
,
840 'thumbnail': thumbnail
,
841 'uploader': uploader
,
842 'uploader_id': channel_name
,
843 'timestamp': timestamp
,
844 'view_count': view_count
,
846 'is_live': stream_type
== 'live',
850 class TwitchClipsIE(TwitchBaseIE
):
851 IE_NAME
= 'twitch:clips'
852 _VALID_URL
= r
'''(?x)
855 clips\.twitch\.tv/(?:embed\?.*?\bclip=|(?:[^/]+/)*)|
856 (?:(?:www|go|m)\.)?twitch\.tv/[^/]+/clip/
862 'url': 'https://clips.twitch.tv/FaintLightGullWholeWheat',
863 'md5': '761769e1eafce0ffebfb4089cb3847cd',
867 'title': 'EA Play 2016 Live from the Novo Theatre',
868 'thumbnail': r
're:^https?://.*\.jpg',
869 'timestamp': 1465767393,
870 'upload_date': '20160612',
872 'uploader': 'stereotype_',
873 'uploader_id': '43566419',
877 'url': 'https://clips.twitch.tv/rflegendary/UninterestedBeeDAESuppy',
878 'only_matching': True,
880 'url': 'https://www.twitch.tv/sergeynixon/clip/StormyThankfulSproutFutureMan',
881 'only_matching': True,
883 'url': 'https://clips.twitch.tv/embed?clip=InquisitiveBreakableYogurtJebaited',
884 'only_matching': True,
886 'url': 'https://m.twitch.tv/rossbroadcast/clip/ConfidentBraveHumanChefFrank',
887 'only_matching': True,
889 'url': 'https://go.twitch.tv/rossbroadcast/clip/ConfidentBraveHumanChefFrank',
890 'only_matching': True,
893 def _real_extract(self
, url
):
894 video_id
= self
._match
_id
(url
)
896 clip
= self
._download
_base
_gql
(
910 tiny: thumbnailURL(width: 86, height: 45)
911 small: thumbnailURL(width: 260, height: 147)
912 medium: thumbnailURL(width: 480, height: 272)
921 }''' % video_id
}, 'Downloading clip GraphQL')['data']['clip']
924 raise ExtractorError(
925 'This clip is no longer available', expected
=True)
928 for option
in clip
.get('videoQualities', []):
929 if not isinstance(option
, dict):
931 source
= url_or_none(option
.get('sourceURL'))
936 'format_id': option
.get('quality'),
937 'height': int_or_none(option
.get('quality')),
938 'fps': int_or_none(option
.get('frameRate')),
940 self
._sort
_formats
(formats
)
943 for thumbnail_id
in ('tiny', 'small', 'medium'):
944 thumbnail_url
= clip
.get(thumbnail_id
)
945 if not thumbnail_url
:
949 'url': thumbnail_url
,
951 mobj
= re
.search(r
'-(\d+)x(\d+)\.', thumbnail_url
)
954 'height': int(mobj
.group(2)),
955 'width': int(mobj
.group(1)),
957 thumbnails
.append(thumb
)
960 'id': clip
.get('id') or video_id
,
961 'title': clip
.get('title') or video_id
,
963 'duration': int_or_none(clip
.get('durationSeconds')),
964 'views': int_or_none(clip
.get('viewCount')),
965 'timestamp': unified_timestamp(clip
.get('createdAt')),
966 'thumbnails': thumbnails
,
967 'creator': try_get(clip
, lambda x
: x
['broadcaster']['displayName'], compat_str
),
968 'uploader': try_get(clip
, lambda x
: x
['curator']['displayName'], compat_str
),
969 'uploader_id': try_get(clip
, lambda x
: x
['curator']['id'], compat_str
),