2 from __future__
import unicode_literals
10 from .common
import InfoExtractor
11 from ..compat
import (
16 compat_urllib_parse_urlencode
,
17 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'
45 def _handle_error(self
, response
):
46 if not isinstance(response
, dict):
48 error
= response
.get('error')
51 '%s returned error: %s - %s' % (self
.IE_NAME
, error
, response
.get('message')),
54 def _call_api(self
, path
, item_id
, *args
, **kwargs
):
55 headers
= kwargs
.get('headers', {}).copy()
57 'Accept': 'application/vnd.twitchtv.v5+json; charset=UTF-8',
58 'Client-ID': self
._CLIENT
_ID
,
62 'expected_status': (400, 410),
64 response
= self
._download
_json
(
65 '%s/%s' % (self
._API
_BASE
, path
), item_id
,
66 *args
, **compat_kwargs(kwargs
))
67 self
._handle
_error
(response
)
70 def _real_initialize(self
):
74 username
, password
= self
._get
_login
_info
()
80 'Unable to login. Twitch said: %s' % message
, expected
=True)
82 def login_step(page
, urlh
, note
, data
):
83 form
= self
._hidden
_inputs
(page
)
86 page_url
= urlh
.geturl()
87 post_url
= self
._search
_regex
(
88 r
'<form[^>]+action=(["\'])(?P
<url
>.+?
)\
1', page,
89 'post url
', default=self._LOGIN_POST_URL, group='url
')
90 post_url = urljoin(page_url, post_url)
95 'Content
-Type
': 'text
/plain
;charset
=UTF
-8',
98 response = self._download_json(
99 post_url, None, note, data=json.dumps(form).encode(),
100 headers=headers, expected_status=400)
101 error = response.get('error_description
') or response.get('error_code
')
105 if 'Authenticated successfully
' in response.get('message
', ''):
108 redirect_url = urljoin(
110 response.get('redirect
') or response['redirect_path
'])
111 return self._download_webpage_handle(
112 redirect_url, None, 'Downloading login redirect page
',
115 login_page, handle = self._download_webpage_handle(
116 self._LOGIN_FORM_URL, None, 'Downloading login page
')
118 # Some TOR nodes and public proxies are blocked completely
119 if 'blacklist_message
' in login_page:
120 fail(clean_html(login_page))
122 redirect_page, handle = login_step(
123 login_page, handle, 'Logging
in', {
124 'username
': username,
125 'password
': password,
126 'client_id
': self._CLIENT_ID,
130 if not redirect_page:
133 if re.search(r'(?i
)<form
[^
>]+id="two-factor-submit"', redirect_page) is not None:
134 # TODO: Add mechanism to request an SMS or phone call
135 tfa_token = self._get_tfa_info('two
-factor authentication token
')
136 login_step(redirect_page, handle, 'Submitting TFA token
', {
137 'authy_token
': tfa_token,
138 'remember_2fa
': 'true
',
141 def _prefer_source(self, formats):
143 source = next(f for f in formats if f['format_id
'] == 'Source
')
144 source['quality
'] = 10
145 except StopIteration:
147 if '/chunked
/' in f['url
']:
150 'format_note
': 'Source
',
152 self._sort_formats(formats)
154 def _download_access_token(self, channel_name):
155 return self._call_api(
156 'api
/channels
/%s/access_token
' % channel_name, channel_name,
157 'Downloading access token JSON
')
159 def _extract_channel_id(self, token, channel_name):
160 return compat_str(self._parse_json(token, channel_name)['channel_id
'])
163 class TwitchVodIE(TwitchBaseIE):
164 IE_NAME = 'twitch
:vod
'
165 _VALID_URL = r'''(?x)
168 (?:(?:www|go|m)\.)?twitch\.tv/(?:[^/]+/v(?:ideo)?|videos)/|
169 player\.twitch\.tv/\?.*?\bvideo=v?
177 'url
': 'http
://www
.twitch
.tv
/riotgames
/v
/6528877?t
=5m10s
',
181 'title
': 'LCK Summer Split
- Week
6 Day
1',
182 'thumbnail
': r're
:^https?
://.*\
.jpg$
',
184 'timestamp
': 1435131709,
185 'upload_date
': '20150624',
186 'uploader
': 'Riot Games
',
187 'uploader_id
': 'riotgames
',
193 'skip_download
': True,
196 # Untitled broadcast (title is None)
197 'url
': 'http
://www
.twitch
.tv
/belkao_o
/v
/11230755',
201 'title
': 'Untitled Broadcast
',
202 'thumbnail
': r're
:^https?
://.*\
.jpg$
',
204 'timestamp
': 1439746708,
205 'upload_date
': '20150816',
206 'uploader
': 'BelkAO_o
',
207 'uploader_id
': 'belkao_o
',
212 'skip_download
': True,
214 'skip
': 'HTTP Error
404: Not Found
',
216 'url
': 'http
://player
.twitch
.tv
/?t
=5m10s
&video
=v6528877
',
217 'only_matching
': True,
219 'url
': 'https
://www
.twitch
.tv
/videos
/6528877',
220 'only_matching
': True,
222 'url
': 'https
://m
.twitch
.tv
/beagsandjam
/v
/247478721',
223 'only_matching
': True,
225 'url
': 'https
://www
.twitch
.tv
/northernlion
/video
/291940395',
226 'only_matching
': True,
228 'url
': 'https
://player
.twitch
.tv
/?video
=480452374',
229 'only_matching
': True,
232 def _download_info(self, item_id):
233 return self._extract_info(
235 'kraken
/videos
/%s' % item_id, item_id,
236 'Downloading video info JSON
'))
239 def _extract_info(info):
240 status = info.get('status
')
241 if status == 'recording
':
243 elif status == 'recorded
':
247 _QUALITIES = ('small
', 'medium
', 'large
')
248 quality_key = qualities(_QUALITIES)
250 preview = info.get('preview
')
251 if isinstance(preview, dict):
252 for thumbnail_id, thumbnail_url in preview.items():
253 thumbnail_url = url_or_none(thumbnail_url)
254 if not thumbnail_url:
256 if thumbnail_id not in _QUALITIES:
259 'url
': thumbnail_url,
260 'preference
': quality_key(thumbnail_id),
264 'title
': info.get('title
') or 'Untitled Broadcast
',
265 'description
': info.get('description
'),
266 'duration
': int_or_none(info.get('length
')),
267 'thumbnails
': thumbnails,
268 'uploader
': info.get('channel
', {}).get('display_name
'),
269 'uploader_id
': info.get('channel
', {}).get('name
'),
270 'timestamp
': parse_iso8601(info.get('recorded_at
')),
271 'view_count
': int_or_none(info.get('views
')),
275 def _real_extract(self, url):
276 vod_id = self._match_id(url)
278 info = self._download_info(vod_id)
279 access_token = self._call_api(
280 'api
/vods
/%s/access_token
' % vod_id, vod_id,
281 'Downloading
%s access token
' % self._ITEM_TYPE)
283 formats = self._extract_m3u8_formats(
284 '%s/vod
/%s.m3u8?
%s' % (
285 self._USHER_BASE, vod_id,
286 compat_urllib_parse_urlencode({
287 'allow_source
': 'true
',
288 'allow_audio_only
': 'true
',
289 'allow_spectre
': 'true
',
290 'player
': 'twitchweb
',
291 'playlist_include_framerate
': 'true
',
292 'nauth
': access_token['token
'],
293 'nauthsig
': access_token['sig
'],
295 vod_id, 'mp4
', entry_protocol='m3u8_native
')
297 self._prefer_source(formats)
298 info['formats
'] = formats
300 parsed_url = compat_urllib_parse_urlparse(url)
301 query = compat_parse_qs(parsed_url.query)
303 info['start_time
'] = parse_duration(query['t
'][0])
305 if info.get('timestamp
') is not None:
306 info['subtitles
'] = {
308 'url
': update_url_query(
309 'https
://api
.twitch
.tv
/v5
/videos
/%s/comments
' % vod_id, {
310 'client_id
': self._CLIENT_ID,
319 def _make_video_result(node):
320 assert isinstance(node, dict)
321 video_id = node.get('id')
325 '_type
': 'url_transparent
',
326 'ie_key
': TwitchVodIE.ie_key(),
328 'url
': 'https
://www
.twitch
.tv
/videos
/%s' % video_id,
329 'title
': node.get('title
'),
330 'thumbnail
': node.get('previewThumbnailURL
'),
331 'duration
': float_or_none(node.get('lengthSeconds
')),
332 'view_count
': int_or_none(node.get('viewCount
')),
336 class TwitchGraphQLBaseIE(TwitchBaseIE):
339 _OPERATION_HASHES = {
340 'CollectionSideBar
': '27111f1b382effad0b6def325caef1909c733fe6a4fbabf54f8d491ef2cf2f14
',
341 'FilterableVideoTower_Videos
': 'a937f1d22e269e39a03b509f65a7490f9fc247d7f83d6ac1421523e3b68042cb
',
342 'ClipsCards__User
': 'b73ad2bfaecfd30a9e6c28fada15bd97032c83ec77a0440766a56fe0bd632777
',
343 'ChannelCollectionsContent
': '07e3691a1bad77a36aba590c351180439a40baefc1c275356f40fc7082419a84
',
344 'StreamMetadata
': '1c719a40e481453e5c48d9bb585d971b8b372f8ebb105b17076722264dfa5b3e
',
345 'ComscoreStreamingQuery
': 'e1edae8122517d013405f237ffcc124515dc6ded82480a88daef69c83b53ac01
',
346 'VideoPreviewOverlay
': '3006e77e51b128d838fa4e835723ca4dc9a05c5efd4466c1085215c6e437e65c
',
349 def _download_gql(self, video_id, ops, note, fatal=True):
354 'sha256Hash
': self._OPERATION_HASHES[op['operationName
']],
357 return self._download_json(
358 'https
://gql
.twitch
.tv
/gql
', video_id, note,
359 data=json.dumps(ops).encode(),
361 'Content
-Type
': 'text
/plain
;charset
=UTF
-8',
362 'Client
-ID
': self._CLIENT_ID,
366 class TwitchCollectionIE(TwitchGraphQLBaseIE):
367 _VALID_URL = r'https?
://(?
:(?
:www|go|m
)\
.)?twitch\
.tv
/collections
/(?P
<id>[^
/]+)'
370 'url
': 'https
://www
.twitch
.tv
/collections
/wlDCoH0zEBZZbQ
',
372 'id': 'wlDCoH0zEBZZbQ
',
373 'title
': 'Overthrow Nook
, capitalism
for children
',
375 'playlist_mincount
': 13,
378 _OPERATION_NAME = 'CollectionSideBar
'
380 def _real_extract(self, url):
381 collection_id = self._match_id(url)
382 collection = self._download_gql(
384 'operationName
': self._OPERATION_NAME,
385 'variables
': {'collectionID': collection_id},
387 'Downloading collection GraphQL
')[0]['data
']['collection
']
388 title = collection.get('title
')
390 for edge in collection['items
']['edges
']:
391 if not isinstance(edge, dict):
393 node = edge.get('node
')
394 if not isinstance(node, dict):
396 video = _make_video_result(node)
398 entries.append(video)
399 return self.playlist_result(
400 entries, playlist_id=collection_id, playlist_title=title)
403 class TwitchPlaylistBaseIE(TwitchGraphQLBaseIE):
404 def _entries(self, channel_name, *args):
406 variables_common = self._make_variables(channel_name, *args)
407 entries_key = '%ss' % self._ENTRY_KIND
408 for page_num in itertools.count(1):
409 variables = variables_common.copy()
410 variables['limit
'] = self._PAGE_LIMIT
412 variables['cursor
'] = cursor
413 page = self._download_gql(
415 'operationName
': self._OPERATION_NAME,
416 'variables
': variables,
418 'Downloading
%ss GraphQL page
%s' % (self._NODE_KIND, page_num),
423 page, lambda x: x[0]['data
']['user
'][entries_key]['edges
'], list)
427 if not isinstance(edge, dict):
429 if edge.get('__typename
') != self._EDGE_KIND:
431 node = edge.get('node
')
432 if not isinstance(node, dict):
434 if node.get('__typename
') != self._NODE_KIND:
436 entry = self._extract_entry(node)
438 cursor = edge.get('cursor
')
440 if not cursor or not isinstance(cursor, compat_str):
443 # Deprecated kraken v5 API
444 def _entries_kraken(self, channel_name, broadcast_type, sort):
445 access_token = self._download_access_token(channel_name)
446 channel_id = self._extract_channel_id(access_token['token
'], channel_name)
448 counter_override = None
449 for counter in itertools.count(1):
450 response = self._call_api(
451 'kraken
/channels
/%s/videos
/' % channel_id,
453 'Downloading video JSON page
%s' % (counter_override or counter),
456 'limit
': self._PAGE_LIMIT,
457 'broadcast_type
': broadcast_type,
460 videos = response.get('videos
')
461 if not isinstance(videos, list):
464 if not isinstance(video, dict):
466 video_url = url_or_none(video.get('url
'))
470 '_type
': 'url_transparent
',
471 'ie_key
': TwitchVodIE.ie_key(),
472 'id': video.get('_id
'),
474 'title
': video.get('title
'),
475 'description
': video.get('description
'),
476 'timestamp
': unified_timestamp(video.get('published_at
')),
477 'duration
': float_or_none(video.get('length
')),
478 'view_count
': int_or_none(video.get('views
')),
479 'language
': video.get('language
'),
481 offset += self._PAGE_LIMIT
482 total = int_or_none(response.get('_total
'))
483 if total and offset >= total:
487 class TwitchVideosIE(TwitchPlaylistBaseIE):
488 _VALID_URL = r'https?
://(?
:(?
:www|go|m
)\
.)?twitch\
.tv
/(?P
<id>[^
/]+)/(?
:videos|profile
)'
491 # All Videos sorted by Date
492 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=all
',
495 'title
': 'spamfish
- All Videos
sorted by Date
',
497 'playlist_mincount
': 924,
499 # All Videos sorted by Popular
500 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=all
&sort
=views
',
503 'title
': 'spamfish
- All Videos
sorted by Popular
',
505 'playlist_mincount
': 931,
507 # Past Broadcasts sorted by Date
508 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=archives
',
511 'title
': 'spamfish
- Past Broadcasts
sorted by Date
',
513 'playlist_mincount
': 27,
515 # Highlights sorted by Date
516 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=highlights
',
519 'title
': 'spamfish
- Highlights
sorted by Date
',
521 'playlist_mincount
': 901,
523 # Uploads sorted by Date
524 'url
': 'https
://www
.twitch
.tv
/esl_csgo
/videos?
filter=uploads
&sort
=time
',
527 'title
': 'esl_csgo
- Uploads
sorted by Date
',
529 'playlist_mincount
': 5,
531 # Past Premieres sorted by Date
532 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=past_premieres
',
535 'title
': 'spamfish
- Past Premieres
sorted by Date
',
537 'playlist_mincount
': 1,
539 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos
/all
',
540 'only_matching
': True,
542 'url
': 'https
://m
.twitch
.tv
/spamfish
/videos
/all
',
543 'only_matching
': True,
545 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos
',
546 'only_matching
': True,
549 Broadcast = collections.namedtuple('Broadcast
', ['type', 'label
'])
551 _DEFAULT_BROADCAST = Broadcast(None, 'All Videos
')
553 'archives
': Broadcast('ARCHIVE
', 'Past Broadcasts
'),
554 'highlights
': Broadcast('HIGHLIGHT
', 'Highlights
'),
555 'uploads
': Broadcast('UPLOAD
', 'Uploads
'),
556 'past_premieres
': Broadcast('PAST_PREMIERE
', 'Past Premieres
'),
557 'all
': _DEFAULT_BROADCAST,
560 _DEFAULT_SORTED_BY = 'Date
'
562 'time
': _DEFAULT_SORTED_BY,
566 _OPERATION_NAME = 'FilterableVideoTower_Videos
'
567 _ENTRY_KIND = 'video
'
568 _EDGE_KIND = 'VideoEdge
'
572 def suitable(cls, url):
574 if any(ie.suitable(url) for ie in (
576 TwitchVideosCollectionsIE))
577 else super(TwitchVideosIE, cls).suitable(url))
580 def _make_variables(channel_name, broadcast_type, sort):
582 'channelOwnerLogin
': channel_name,
583 'broadcastType
': broadcast_type,
584 'videoSort
': sort.upper(),
588 def _extract_entry(node):
589 return _make_video_result(node)
591 def _real_extract(self, url):
592 channel_name = self._match_id(url)
593 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
594 filter = qs.get('filter', ['all
'])[0]
595 sort = qs.get('sort
', ['time
'])[0]
596 broadcast = self._BROADCASTS.get(filter, self._DEFAULT_BROADCAST)
597 return self.playlist_result(
598 self._entries(channel_name, broadcast.type, sort),
599 playlist_id=channel_name,
600 playlist_title='%s - %s sorted by
%s'
601 % (channel_name, broadcast.label,
602 self._SORTED_BY.get(sort, self._DEFAULT_SORTED_BY)))
605 class TwitchVideosClipsIE(TwitchPlaylistBaseIE):
606 _VALID_URL = r'https?
://(?
:(?
:www|go|m
)\
.)?twitch\
.tv
/(?P
<id>[^
/]+)/(?
:clips|videos
/*?
\?.*?
\bfilter
=clips
)'
610 'url
': 'https
://www
.twitch
.tv
/vanillatv
/clips?
filter=clips
&range=all
',
613 'title
': 'vanillatv
- Clips Top All
',
615 'playlist_mincount
': 1,
617 'url
': 'https
://www
.twitch
.tv
/dota2ruhub
/videos?
filter=clips
&range=7d
',
618 'only_matching
': True,
621 Clip = collections.namedtuple('Clip
', ['filter', 'label
'])
623 _DEFAULT_CLIP = Clip('LAST_WEEK
', 'Top
7D
')
625 '24hr
': Clip('LAST_DAY
', 'Top
24H
'),
627 '30d
': Clip('LAST_MONTH
', 'Top
30D
'),
628 'all
': Clip('ALL_TIME
', 'Top All
'),
631 # NB: values other than 20 result in skipped videos
634 _OPERATION_NAME = 'ClipsCards__User
'
636 _EDGE_KIND = 'ClipEdge
'
640 def _make_variables(channel_name, filter):
642 'login
': channel_name,
649 def _extract_entry(node):
650 assert isinstance(node, dict)
651 clip_url = url_or_none(node.get('url
'))
655 '_type
': 'url_transparent
',
656 'ie_key
': TwitchClipsIE.ie_key(),
657 'id': node.get('id'),
659 'title
': node.get('title
'),
660 'thumbnail
': node.get('thumbnailURL
'),
661 'duration
': float_or_none(node.get('durationSeconds
')),
662 'timestamp
': unified_timestamp(node.get('createdAt
')),
663 'view_count
': int_or_none(node.get('viewCount
')),
664 'language
': node.get('language
'),
667 def _real_extract(self, url):
668 channel_name = self._match_id(url)
669 qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
670 range = qs.get('range', ['7d
'])[0]
671 clip = self._RANGE.get(range, self._DEFAULT_CLIP)
672 return self.playlist_result(
673 self._entries(channel_name, clip.filter),
674 playlist_id=channel_name,
675 playlist_title='%s - Clips
%s' % (channel_name, clip.label))
678 class TwitchVideosCollectionsIE(TwitchPlaylistBaseIE):
679 _VALID_URL = r'https?
://(?
:(?
:www|go|m
)\
.)?twitch\
.tv
/(?P
<id>[^
/]+)/videos
/*?
\?.*?
\bfilter
=collections
'
683 'url
': 'https
://www
.twitch
.tv
/spamfish
/videos?
filter=collections
',
686 'title
': 'spamfish
- Collections
',
688 'playlist_mincount
': 3,
691 _OPERATION_NAME = 'ChannelCollectionsContent
'
692 _ENTRY_KIND = 'collection
'
693 _EDGE_KIND = 'CollectionsItemEdge
'
694 _NODE_KIND = 'Collection
'
697 def _make_variables(channel_name):
699 'ownerLogin
': channel_name,
703 def _extract_entry(node):
704 assert isinstance(node, dict)
705 collection_id = node.get('id')
706 if not collection_id:
709 '_type
': 'url_transparent
',
710 'ie_key
': TwitchCollectionIE.ie_key(),
712 'url
': 'https
://www
.twitch
.tv
/collections
/%s' % collection_id,
713 'title
': node.get('title
'),
714 'thumbnail
': node.get('thumbnailURL
'),
715 'duration
': float_or_none(node.get('lengthSeconds
')),
716 'timestamp
': unified_timestamp(node.get('updatedAt
')),
717 'view_count
': int_or_none(node.get('viewCount
')),
720 def _real_extract(self, url):
721 channel_name = self._match_id(url)
722 return self.playlist_result(
723 self._entries(channel_name), playlist_id=channel_name,
724 playlist_title='%s - Collections
' % channel_name)
727 class TwitchStreamIE(TwitchGraphQLBaseIE):
728 IE_NAME = 'twitch
:stream
'
729 _VALID_URL = r'''(?x)
732 (?:(?:www|go|m)\.)?twitch\.tv/|
733 player\.twitch\.tv/\?.*?\bchannel=
739 'url
': 'http
://www
.twitch
.tv
/shroomztv
',
742 'display_id
': 'shroomztv
',
744 'title
': 're
:^ShroomzTV
[0-9]{4}
-[0-9]{2}
-[0-9]{2}
[0-9]{2}
:[0-9]{2}$
',
745 'description
': 'H1Z1
- lonewolfing
with ShroomzTV | A3 Battle Royale later
- @ShroomzTV',
747 'timestamp
': 1421928037,
748 'upload_date
': '20150122',
749 'uploader
': 'ShroomzTV
',
750 'uploader_id
': 'shroomztv
',
755 'skip_download
': True,
758 'url
': 'http
://www
.twitch
.tv
/miracle_doto
#profile-0',
759 'only_matching': True,
761 'url': 'https://player.twitch.tv/?channel=lotsofs',
762 'only_matching': True,
764 'url': 'https://go.twitch.tv/food',
765 'only_matching': True,
767 'url': 'https://m.twitch.tv/food',
768 'only_matching': True,
772 def suitable(cls
, url
):
774 if any(ie
.suitable(url
) for ie
in (
779 TwitchVideosCollectionsIE
,
781 else super(TwitchStreamIE
, cls
).suitable(url
))
783 def _real_extract(self
, url
):
784 channel_name
= self
._match
_id
(url
).lower()
786 gql
= self
._download
_gql
(
788 'operationName': 'StreamMetadata',
789 'variables': {'channelLogin': channel_name}
,
791 'operationName': 'ComscoreStreamingQuery',
793 'channel': channel_name
,
797 'isVodOrCollection': False,
801 'operationName': 'VideoPreviewOverlay',
802 'variables': {'login': channel_name}
,
804 'Downloading stream GraphQL')
806 user
= gql
[0]['data']['user']
809 raise ExtractorError(
810 '%s does not exist' % channel_name
, expected
=True)
812 stream
= user
['stream']
815 raise ExtractorError('%s is offline' % channel_name
, expected
=True)
817 access_token
= self
._download
_access
_token
(channel_name
)
818 token
= access_token
['token']
820 stream_id
= stream
.get('id') or channel_name
822 'allow_source': 'true',
823 'allow_audio_only': 'true',
824 'allow_spectre': 'true',
825 'p': random
.randint(1000000, 10000000),
826 'player': 'twitchweb',
827 'playlist_include_framerate': 'true',
828 'segment_preference': '4',
829 'sig': access_token
['sig'].encode('utf-8'),
830 'token': token
.encode('utf-8'),
832 formats
= self
._extract
_m
3u8_formats
(
833 '%s/api/channel/hls/%s.m3u8' % (self
._USHER
_BASE
, channel_name
),
834 stream_id
, 'mp4', query
=query
)
835 self
._prefer
_source
(formats
)
837 view_count
= stream
.get('viewers')
838 timestamp
= unified_timestamp(stream
.get('createdAt'))
840 sq_user
= try_get(gql
, lambda x
: x
[1]['data']['user'], dict) or {}
841 uploader
= sq_user
.get('displayName')
842 description
= try_get(
843 sq_user
, lambda x
: x
['broadcastSettings']['title'], compat_str
)
845 thumbnail
= url_or_none(try_get(
846 gql
, lambda x
: x
[2]['data']['user']['stream']['previewImageURL'],
849 title
= uploader
or channel_name
850 stream_type
= stream
.get('type')
851 if stream_type
in ['rerun', 'live']:
852 title
+= ' (%s)' % stream_type
856 'display_id': channel_name
,
857 'title': self
._live
_title
(title
),
858 'description': description
,
859 'thumbnail': thumbnail
,
860 'uploader': uploader
,
861 'uploader_id': channel_name
,
862 'timestamp': timestamp
,
863 'view_count': view_count
,
865 'is_live': stream_type
== 'live',
869 class TwitchClipsIE(TwitchBaseIE
):
870 IE_NAME
= 'twitch:clips'
871 _VALID_URL
= r
'''(?x)
874 clips\.twitch\.tv/(?:embed\?.*?\bclip=|(?:[^/]+/)*)|
875 (?:(?:www|go|m)\.)?twitch\.tv/[^/]+/clip/
881 'url': 'https://clips.twitch.tv/FaintLightGullWholeWheat',
882 'md5': '761769e1eafce0ffebfb4089cb3847cd',
886 'title': 'EA Play 2016 Live from the Novo Theatre',
887 'thumbnail': r
're:^https?://.*\.jpg',
888 'timestamp': 1465767393,
889 'upload_date': '20160612',
891 'uploader': 'stereotype_',
892 'uploader_id': '43566419',
896 'url': 'https://clips.twitch.tv/rflegendary/UninterestedBeeDAESuppy',
897 'only_matching': True,
899 'url': 'https://www.twitch.tv/sergeynixon/clip/StormyThankfulSproutFutureMan',
900 'only_matching': True,
902 'url': 'https://clips.twitch.tv/embed?clip=InquisitiveBreakableYogurtJebaited',
903 'only_matching': True,
905 'url': 'https://m.twitch.tv/rossbroadcast/clip/ConfidentBraveHumanChefFrank',
906 'only_matching': True,
908 'url': 'https://go.twitch.tv/rossbroadcast/clip/ConfidentBraveHumanChefFrank',
909 'only_matching': True,
912 def _real_extract(self
, url
):
913 video_id
= self
._match
_id
(url
)
915 clip
= self
._download
_json
(
916 'https://gql.twitch.tv/gql', video_id
, data
=json
.dumps({
929 tiny: thumbnailURL(width: 86, height: 45)
930 small: thumbnailURL(width: 260, height: 147)
931 medium: thumbnailURL(width: 480, height: 272)
941 }).encode(), headers
={
942 'Client-ID': self
._CLIENT
_ID
,
946 raise ExtractorError(
947 'This clip is no longer available', expected
=True)
950 for option
in clip
.get('videoQualities', []):
951 if not isinstance(option
, dict):
953 source
= url_or_none(option
.get('sourceURL'))
958 'format_id': option
.get('quality'),
959 'height': int_or_none(option
.get('quality')),
960 'fps': int_or_none(option
.get('frameRate')),
962 self
._sort
_formats
(formats
)
965 for thumbnail_id
in ('tiny', 'small', 'medium'):
966 thumbnail_url
= clip
.get(thumbnail_id
)
967 if not thumbnail_url
:
971 'url': thumbnail_url
,
973 mobj
= re
.search(r
'-(\d+)x(\d+)\.', thumbnail_url
)
976 'height': int(mobj
.group(2)),
977 'width': int(mobj
.group(1)),
979 thumbnails
.append(thumb
)
982 'id': clip
.get('id') or video_id
,
983 'title': clip
.get('title') or video_id
,
985 'duration': int_or_none(clip
.get('durationSeconds')),
986 'views': int_or_none(clip
.get('viewCount')),
987 'timestamp': unified_timestamp(clip
.get('createdAt')),
988 'thumbnails': thumbnails
,
989 'creator': try_get(clip
, lambda x
: x
['broadcaster']['displayName'], compat_str
),
990 'uploader': try_get(clip
, lambda x
: x
['curator']['displayName'], compat_str
),
991 'uploader_id': try_get(clip
, lambda x
: x
['curator']['id'], compat_str
),