You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

731 lines
25 KiB

7 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import itertools
  4. import re
  5. import random
  6. import json
  7. from .common import InfoExtractor
  8. from ..compat import (
  9. compat_kwargs,
  10. compat_parse_qs,
  11. compat_str,
  12. compat_urllib_parse_urlencode,
  13. compat_urllib_parse_urlparse,
  14. )
  15. from ..utils import (
  16. clean_html,
  17. ExtractorError,
  18. float_or_none,
  19. int_or_none,
  20. orderedSet,
  21. parse_duration,
  22. parse_iso8601,
  23. qualities,
  24. try_get,
  25. unified_timestamp,
  26. update_url_query,
  27. url_or_none,
  28. urljoin,
  29. )
  30. class TwitchBaseIE(InfoExtractor):
  31. _VALID_URL_BASE = r'https?://(?:(?:www|go|m)\.)?twitch\.tv'
  32. _API_BASE = 'https://api.twitch.tv'
  33. _USHER_BASE = 'https://usher.ttvnw.net'
  34. _LOGIN_FORM_URL = 'https://www.twitch.tv/login'
  35. _LOGIN_POST_URL = 'https://passport.twitch.tv/login'
  36. _CLIENT_ID = 'kimne78kx3ncx6brgo4mv6wki5h1ko'
  37. _NETRC_MACHINE = 'twitch'
  38. def _handle_error(self, response):
  39. if not isinstance(response, dict):
  40. return
  41. error = response.get('error')
  42. if error:
  43. raise ExtractorError(
  44. '%s returned error: %s - %s' % (self.IE_NAME, error, response.get('message')),
  45. expected=True)
  46. def _call_api(self, path, item_id, *args, **kwargs):
  47. headers = kwargs.get('headers', {}).copy()
  48. headers['Client-ID'] = self._CLIENT_ID
  49. kwargs['headers'] = headers
  50. response = self._download_json(
  51. '%s/%s' % (self._API_BASE, path), item_id,
  52. *args, **compat_kwargs(kwargs))
  53. self._handle_error(response)
  54. return response
  55. def _real_initialize(self):
  56. self._login()
  57. def _login(self):
  58. username, password = self._get_login_info()
  59. if username is None:
  60. return
  61. def fail(message):
  62. raise ExtractorError(
  63. 'Unable to login. Twitch said: %s' % message, expected=True)
  64. def login_step(page, urlh, note, data):
  65. form = self._hidden_inputs(page)
  66. form.update(data)
  67. page_url = urlh.geturl()
  68. post_url = self._search_regex(
  69. r'<form[^>]+action=(["\'])(?P<url>.+?)\1', page,
  70. 'post url', default=self._LOGIN_POST_URL, group='url')
  71. post_url = urljoin(page_url, post_url)
  72. headers = {
  73. 'Referer': page_url,
  74. 'Origin': page_url,
  75. 'Content-Type': 'text/plain;charset=UTF-8',
  76. }
  77. response = self._download_json(
  78. post_url, None, note, data=json.dumps(form).encode(),
  79. headers=headers, expected_status=400)
  80. error = response.get('error_description') or response.get('error_code')
  81. if error:
  82. fail(error)
  83. if 'Authenticated successfully' in response.get('message', ''):
  84. return None, None
  85. redirect_url = urljoin(
  86. post_url,
  87. response.get('redirect') or response['redirect_path'])
  88. return self._download_webpage_handle(
  89. redirect_url, None, 'Downloading login redirect page',
  90. headers=headers)
  91. login_page, handle = self._download_webpage_handle(
  92. self._LOGIN_FORM_URL, None, 'Downloading login page')
  93. # Some TOR nodes and public proxies are blocked completely
  94. if 'blacklist_message' in login_page:
  95. fail(clean_html(login_page))
  96. redirect_page, handle = login_step(
  97. login_page, handle, 'Logging in', {
  98. 'username': username,
  99. 'password': password,
  100. 'client_id': self._CLIENT_ID,
  101. })
  102. # Successful login
  103. if not redirect_page:
  104. return
  105. if re.search(r'(?i)<form[^>]+id="two-factor-submit"', redirect_page) is not None:
  106. # TODO: Add mechanism to request an SMS or phone call
  107. tfa_token = self._get_tfa_info('two-factor authentication token')
  108. login_step(redirect_page, handle, 'Submitting TFA token', {
  109. 'authy_token': tfa_token,
  110. 'remember_2fa': 'true',
  111. })
  112. def _prefer_source(self, formats):
  113. try:
  114. source = next(f for f in formats if f['format_id'] == 'Source')
  115. source['preference'] = 10
  116. except StopIteration:
  117. for f in formats:
  118. if '/chunked/' in f['url']:
  119. f.update({
  120. 'source_preference': 10,
  121. 'format_note': 'Source',
  122. })
  123. self._sort_formats(formats)
  124. class TwitchItemBaseIE(TwitchBaseIE):
  125. def _download_info(self, item, item_id):
  126. return self._extract_info(self._call_api(
  127. 'kraken/videos/%s%s' % (item, item_id), item_id,
  128. 'Downloading %s info JSON' % self._ITEM_TYPE))
  129. def _extract_media(self, item_id):
  130. info = self._download_info(self._ITEM_SHORTCUT, item_id)
  131. response = self._call_api(
  132. 'api/videos/%s%s' % (self._ITEM_SHORTCUT, item_id), item_id,
  133. 'Downloading %s playlist JSON' % self._ITEM_TYPE)
  134. entries = []
  135. chunks = response['chunks']
  136. qualities = list(chunks.keys())
  137. for num, fragment in enumerate(zip(*chunks.values()), start=1):
  138. formats = []
  139. for fmt_num, fragment_fmt in enumerate(fragment):
  140. format_id = qualities[fmt_num]
  141. fmt = {
  142. 'url': fragment_fmt['url'],
  143. 'format_id': format_id,
  144. 'quality': 1 if format_id == 'live' else 0,
  145. }
  146. m = re.search(r'^(?P<height>\d+)[Pp]', format_id)
  147. if m:
  148. fmt['height'] = int(m.group('height'))
  149. formats.append(fmt)
  150. self._sort_formats(formats)
  151. entry = dict(info)
  152. entry['id'] = '%s_%d' % (entry['id'], num)
  153. entry['title'] = '%s part %d' % (entry['title'], num)
  154. entry['formats'] = formats
  155. entries.append(entry)
  156. return self.playlist_result(entries, info['id'], info['title'])
  157. def _extract_info(self, info):
  158. status = info.get('status')
  159. if status == 'recording':
  160. is_live = True
  161. elif status == 'recorded':
  162. is_live = False
  163. else:
  164. is_live = None
  165. return {
  166. 'id': info['_id'],
  167. 'title': info.get('title') or 'Untitled Broadcast',
  168. 'description': info.get('description'),
  169. 'duration': int_or_none(info.get('length')),
  170. 'thumbnail': info.get('preview'),
  171. 'uploader': info.get('channel', {}).get('display_name'),
  172. 'uploader_id': info.get('channel', {}).get('name'),
  173. 'timestamp': parse_iso8601(info.get('recorded_at')),
  174. 'view_count': int_or_none(info.get('views')),
  175. 'is_live': is_live,
  176. }
  177. def _real_extract(self, url):
  178. return self._extract_media(self._match_id(url))
  179. class TwitchVideoIE(TwitchItemBaseIE):
  180. IE_NAME = 'twitch:video'
  181. _VALID_URL = r'%s/[^/]+/b/(?P<id>\d+)' % TwitchBaseIE._VALID_URL_BASE
  182. _ITEM_TYPE = 'video'
  183. _ITEM_SHORTCUT = 'a'
  184. _TEST = {
  185. 'url': 'http://www.twitch.tv/riotgames/b/577357806',
  186. 'info_dict': {
  187. 'id': 'a577357806',
  188. 'title': 'Worlds Semifinals - Star Horn Royal Club vs. OMG',
  189. },
  190. 'playlist_mincount': 12,
  191. 'skip': 'HTTP Error 404: Not Found',
  192. }
  193. class TwitchChapterIE(TwitchItemBaseIE):
  194. IE_NAME = 'twitch:chapter'
  195. _VALID_URL = r'%s/[^/]+/c/(?P<id>\d+)' % TwitchBaseIE._VALID_URL_BASE
  196. _ITEM_TYPE = 'chapter'
  197. _ITEM_SHORTCUT = 'c'
  198. _TESTS = [{
  199. 'url': 'http://www.twitch.tv/acracingleague/c/5285812',
  200. 'info_dict': {
  201. 'id': 'c5285812',
  202. 'title': 'ACRL Off Season - Sports Cars @ Nordschleife',
  203. },
  204. 'playlist_mincount': 3,
  205. 'skip': 'HTTP Error 404: Not Found',
  206. }, {
  207. 'url': 'http://www.twitch.tv/tsm_theoddone/c/2349361',
  208. 'only_matching': True,
  209. }]
  210. class TwitchVodIE(TwitchItemBaseIE):
  211. IE_NAME = 'twitch:vod'
  212. _VALID_URL = r'''(?x)
  213. https?://
  214. (?:
  215. (?:(?:www|go|m)\.)?twitch\.tv/(?:[^/]+/v(?:ideo)?|videos)/|
  216. player\.twitch\.tv/\?.*?\bvideo=v
  217. )
  218. (?P<id>\d+)
  219. '''
  220. _ITEM_TYPE = 'vod'
  221. _ITEM_SHORTCUT = 'v'
  222. _TESTS = [{
  223. 'url': 'http://www.twitch.tv/riotgames/v/6528877?t=5m10s',
  224. 'info_dict': {
  225. 'id': 'v6528877',
  226. 'ext': 'mp4',
  227. 'title': 'LCK Summer Split - Week 6 Day 1',
  228. 'thumbnail': r're:^https?://.*\.jpg$',
  229. 'duration': 17208,
  230. 'timestamp': 1435131709,
  231. 'upload_date': '20150624',
  232. 'uploader': 'Riot Games',
  233. 'uploader_id': 'riotgames',
  234. 'view_count': int,
  235. 'start_time': 310,
  236. },
  237. 'params': {
  238. # m3u8 download
  239. 'skip_download': True,
  240. },
  241. }, {
  242. # Untitled broadcast (title is None)
  243. 'url': 'http://www.twitch.tv/belkao_o/v/11230755',
  244. 'info_dict': {
  245. 'id': 'v11230755',
  246. 'ext': 'mp4',
  247. 'title': 'Untitled Broadcast',
  248. 'thumbnail': r're:^https?://.*\.jpg$',
  249. 'duration': 1638,
  250. 'timestamp': 1439746708,
  251. 'upload_date': '20150816',
  252. 'uploader': 'BelkAO_o',
  253. 'uploader_id': 'belkao_o',
  254. 'view_count': int,
  255. },
  256. 'params': {
  257. # m3u8 download
  258. 'skip_download': True,
  259. },
  260. 'skip': 'HTTP Error 404: Not Found',
  261. }, {
  262. 'url': 'http://player.twitch.tv/?t=5m10s&video=v6528877',
  263. 'only_matching': True,
  264. }, {
  265. 'url': 'https://www.twitch.tv/videos/6528877',
  266. 'only_matching': True,
  267. }, {
  268. 'url': 'https://m.twitch.tv/beagsandjam/v/247478721',
  269. 'only_matching': True,
  270. }, {
  271. 'url': 'https://www.twitch.tv/northernlion/video/291940395',
  272. 'only_matching': True,
  273. }]
  274. def _real_extract(self, url):
  275. item_id = self._match_id(url)
  276. info = self._download_info(self._ITEM_SHORTCUT, item_id)
  277. access_token = self._call_api(
  278. 'api/vods/%s/access_token' % item_id, item_id,
  279. 'Downloading %s access token' % self._ITEM_TYPE)
  280. formats = self._extract_m3u8_formats(
  281. '%s/vod/%s?%s' % (
  282. self._USHER_BASE, item_id,
  283. compat_urllib_parse_urlencode({
  284. 'allow_source': 'true',
  285. 'allow_audio_only': 'true',
  286. 'allow_spectre': 'true',
  287. 'player': 'twitchweb',
  288. 'nauth': access_token['token'],
  289. 'nauthsig': access_token['sig'],
  290. })),
  291. item_id, 'mp4', entry_protocol='m3u8_native')
  292. self._prefer_source(formats)
  293. info['formats'] = formats
  294. parsed_url = compat_urllib_parse_urlparse(url)
  295. query = compat_parse_qs(parsed_url.query)
  296. if 't' in query:
  297. info['start_time'] = parse_duration(query['t'][0])
  298. if info.get('timestamp') is not None:
  299. info['subtitles'] = {
  300. 'rechat': [{
  301. 'url': update_url_query(
  302. 'https://rechat.twitch.tv/rechat-messages', {
  303. 'video_id': 'v%s' % item_id,
  304. 'start': info['timestamp'],
  305. }),
  306. 'ext': 'json',
  307. }],
  308. }
  309. return info
  310. class TwitchPlaylistBaseIE(TwitchBaseIE):
  311. _PLAYLIST_PATH = 'kraken/channels/%s/videos/?offset=%d&limit=%d'
  312. _PAGE_LIMIT = 100
  313. def _extract_playlist(self, channel_id):
  314. info = self._call_api(
  315. 'kraken/channels/%s' % channel_id,
  316. channel_id, 'Downloading channel info JSON')
  317. channel_name = info.get('display_name') or info.get('name')
  318. entries = []
  319. offset = 0
  320. limit = self._PAGE_LIMIT
  321. broken_paging_detected = False
  322. counter_override = None
  323. for counter in itertools.count(1):
  324. response = self._call_api(
  325. self._PLAYLIST_PATH % (channel_id, offset, limit),
  326. channel_id,
  327. 'Downloading %s JSON page %s'
  328. % (self._PLAYLIST_TYPE, counter_override or counter))
  329. page_entries = self._extract_playlist_page(response)
  330. if not page_entries:
  331. break
  332. total = int_or_none(response.get('_total'))
  333. # Since the beginning of March 2016 twitch's paging mechanism
  334. # is completely broken on the twitch side. It simply ignores
  335. # a limit and returns the whole offset number of videos.
  336. # Working around by just requesting all videos at once.
  337. # Upd: pagination bug was fixed by twitch on 15.03.2016.
  338. if not broken_paging_detected and total and len(page_entries) > limit:
  339. self.report_warning(
  340. 'Twitch pagination is broken on twitch side, requesting all videos at once',
  341. channel_id)
  342. broken_paging_detected = True
  343. offset = total
  344. counter_override = '(all at once)'
  345. continue
  346. entries.extend(page_entries)
  347. if broken_paging_detected or total and len(page_entries) >= total:
  348. break
  349. offset += limit
  350. return self.playlist_result(
  351. [self._make_url_result(entry) for entry in orderedSet(entries)],
  352. channel_id, channel_name)
  353. def _make_url_result(self, url):
  354. try:
  355. video_id = 'v%s' % TwitchVodIE._match_id(url)
  356. return self.url_result(url, TwitchVodIE.ie_key(), video_id=video_id)
  357. except AssertionError:
  358. return self.url_result(url)
  359. def _extract_playlist_page(self, response):
  360. videos = response.get('videos')
  361. return [video['url'] for video in videos] if videos else []
  362. def _real_extract(self, url):
  363. return self._extract_playlist(self._match_id(url))
  364. class TwitchProfileIE(TwitchPlaylistBaseIE):
  365. IE_NAME = 'twitch:profile'
  366. _VALID_URL = r'%s/(?P<id>[^/]+)/profile/?(?:\#.*)?$' % TwitchBaseIE._VALID_URL_BASE
  367. _PLAYLIST_TYPE = 'profile'
  368. _TESTS = [{
  369. 'url': 'http://www.twitch.tv/vanillatv/profile',
  370. 'info_dict': {
  371. 'id': 'vanillatv',
  372. 'title': 'VanillaTV',
  373. },
  374. 'playlist_mincount': 412,
  375. }, {
  376. 'url': 'http://m.twitch.tv/vanillatv/profile',
  377. 'only_matching': True,
  378. }]
  379. class TwitchVideosBaseIE(TwitchPlaylistBaseIE):
  380. _VALID_URL_VIDEOS_BASE = r'%s/(?P<id>[^/]+)/videos' % TwitchBaseIE._VALID_URL_BASE
  381. _PLAYLIST_PATH = TwitchPlaylistBaseIE._PLAYLIST_PATH + '&broadcast_type='
  382. class TwitchAllVideosIE(TwitchVideosBaseIE):
  383. IE_NAME = 'twitch:videos:all'
  384. _VALID_URL = r'%s/all' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE
  385. _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'archive,upload,highlight'
  386. _PLAYLIST_TYPE = 'all videos'
  387. _TESTS = [{
  388. 'url': 'https://www.twitch.tv/spamfish/videos/all',
  389. 'info_dict': {
  390. 'id': 'spamfish',
  391. 'title': 'Spamfish',
  392. },
  393. 'playlist_mincount': 869,
  394. }, {
  395. 'url': 'https://m.twitch.tv/spamfish/videos/all',
  396. 'only_matching': True,
  397. }]
  398. class TwitchUploadsIE(TwitchVideosBaseIE):
  399. IE_NAME = 'twitch:videos:uploads'
  400. _VALID_URL = r'%s/uploads' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE
  401. _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'upload'
  402. _PLAYLIST_TYPE = 'uploads'
  403. _TESTS = [{
  404. 'url': 'https://www.twitch.tv/spamfish/videos/uploads',
  405. 'info_dict': {
  406. 'id': 'spamfish',
  407. 'title': 'Spamfish',
  408. },
  409. 'playlist_mincount': 0,
  410. }, {
  411. 'url': 'https://m.twitch.tv/spamfish/videos/uploads',
  412. 'only_matching': True,
  413. }]
  414. class TwitchPastBroadcastsIE(TwitchVideosBaseIE):
  415. IE_NAME = 'twitch:videos:past-broadcasts'
  416. _VALID_URL = r'%s/past-broadcasts' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE
  417. _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'archive'
  418. _PLAYLIST_TYPE = 'past broadcasts'
  419. _TESTS = [{
  420. 'url': 'https://www.twitch.tv/spamfish/videos/past-broadcasts',
  421. 'info_dict': {
  422. 'id': 'spamfish',
  423. 'title': 'Spamfish',
  424. },
  425. 'playlist_mincount': 0,
  426. }, {
  427. 'url': 'https://m.twitch.tv/spamfish/videos/past-broadcasts',
  428. 'only_matching': True,
  429. }]
  430. class TwitchHighlightsIE(TwitchVideosBaseIE):
  431. IE_NAME = 'twitch:videos:highlights'
  432. _VALID_URL = r'%s/highlights' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE
  433. _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'highlight'
  434. _PLAYLIST_TYPE = 'highlights'
  435. _TESTS = [{
  436. 'url': 'https://www.twitch.tv/spamfish/videos/highlights',
  437. 'info_dict': {
  438. 'id': 'spamfish',
  439. 'title': 'Spamfish',
  440. },
  441. 'playlist_mincount': 805,
  442. }, {
  443. 'url': 'https://m.twitch.tv/spamfish/videos/highlights',
  444. 'only_matching': True,
  445. }]
  446. class TwitchStreamIE(TwitchBaseIE):
  447. IE_NAME = 'twitch:stream'
  448. _VALID_URL = r'''(?x)
  449. https?://
  450. (?:
  451. (?:(?:www|go|m)\.)?twitch\.tv/|
  452. player\.twitch\.tv/\?.*?\bchannel=
  453. )
  454. (?P<id>[^/#?]+)
  455. '''
  456. _TESTS = [{
  457. 'url': 'http://www.twitch.tv/shroomztv',
  458. 'info_dict': {
  459. 'id': '12772022048',
  460. 'display_id': 'shroomztv',
  461. 'ext': 'mp4',
  462. 'title': 're:^ShroomzTV [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  463. 'description': 'H1Z1 - lonewolfing with ShroomzTV | A3 Battle Royale later - @ShroomzTV',
  464. 'is_live': True,
  465. 'timestamp': 1421928037,
  466. 'upload_date': '20150122',
  467. 'uploader': 'ShroomzTV',
  468. 'uploader_id': 'shroomztv',
  469. 'view_count': int,
  470. },
  471. 'params': {
  472. # m3u8 download
  473. 'skip_download': True,
  474. },
  475. }, {
  476. 'url': 'http://www.twitch.tv/miracle_doto#profile-0',
  477. 'only_matching': True,
  478. }, {
  479. 'url': 'https://player.twitch.tv/?channel=lotsofs',
  480. 'only_matching': True,
  481. }, {
  482. 'url': 'https://go.twitch.tv/food',
  483. 'only_matching': True,
  484. }, {
  485. 'url': 'https://m.twitch.tv/food',
  486. 'only_matching': True,
  487. }]
  488. @classmethod
  489. def suitable(cls, url):
  490. return (False
  491. if any(ie.suitable(url) for ie in (
  492. TwitchVideoIE,
  493. TwitchChapterIE,
  494. TwitchVodIE,
  495. TwitchProfileIE,
  496. TwitchAllVideosIE,
  497. TwitchUploadsIE,
  498. TwitchPastBroadcastsIE,
  499. TwitchHighlightsIE,
  500. TwitchClipsIE))
  501. else super(TwitchStreamIE, cls).suitable(url))
  502. def _real_extract(self, url):
  503. channel_id = self._match_id(url)
  504. stream = self._call_api(
  505. 'kraken/streams/%s?stream_type=all' % channel_id, channel_id,
  506. 'Downloading stream JSON').get('stream')
  507. if not stream:
  508. raise ExtractorError('%s is offline' % channel_id, expected=True)
  509. # Channel name may be typed if different case than the original channel name
  510. # (e.g. http://www.twitch.tv/TWITCHPLAYSPOKEMON) that will lead to constructing
  511. # an invalid m3u8 URL. Working around by use of original channel name from stream
  512. # JSON and fallback to lowercase if it's not available.
  513. channel_id = stream.get('channel', {}).get('name') or channel_id.lower()
  514. access_token = self._call_api(
  515. 'api/channels/%s/access_token' % channel_id, channel_id,
  516. 'Downloading channel access token')
  517. query = {
  518. 'allow_source': 'true',
  519. 'allow_audio_only': 'true',
  520. 'allow_spectre': 'true',
  521. 'p': random.randint(1000000, 10000000),
  522. 'player': 'twitchweb',
  523. 'segment_preference': '4',
  524. 'sig': access_token['sig'].encode('utf-8'),
  525. 'token': access_token['token'].encode('utf-8'),
  526. }
  527. formats = self._extract_m3u8_formats(
  528. '%s/api/channel/hls/%s.m3u8?%s'
  529. % (self._USHER_BASE, channel_id, compat_urllib_parse_urlencode(query)),
  530. channel_id, 'mp4')
  531. self._prefer_source(formats)
  532. view_count = stream.get('viewers')
  533. timestamp = parse_iso8601(stream.get('created_at'))
  534. channel = stream['channel']
  535. title = self._live_title(channel.get('display_name') or channel.get('name'))
  536. description = channel.get('status')
  537. thumbnails = []
  538. for thumbnail_key, thumbnail_url in stream['preview'].items():
  539. m = re.search(r'(?P<width>\d+)x(?P<height>\d+)\.jpg$', thumbnail_key)
  540. if not m:
  541. continue
  542. thumbnails.append({
  543. 'url': thumbnail_url,
  544. 'width': int(m.group('width')),
  545. 'height': int(m.group('height')),
  546. })
  547. return {
  548. 'id': compat_str(stream['_id']),
  549. 'display_id': channel_id,
  550. 'title': title,
  551. 'description': description,
  552. 'thumbnails': thumbnails,
  553. 'uploader': channel.get('display_name'),
  554. 'uploader_id': channel.get('name'),
  555. 'timestamp': timestamp,
  556. 'view_count': view_count,
  557. 'formats': formats,
  558. 'is_live': True,
  559. }
  560. class TwitchClipsIE(TwitchBaseIE):
  561. IE_NAME = 'twitch:clips'
  562. _VALID_URL = r'https?://(?:clips\.twitch\.tv/(?:[^/]+/)*|(?:www\.)?twitch\.tv/[^/]+/clip/)(?P<id>[^/?#&]+)'
  563. _TESTS = [{
  564. 'url': 'https://clips.twitch.tv/FaintLightGullWholeWheat',
  565. 'md5': '761769e1eafce0ffebfb4089cb3847cd',
  566. 'info_dict': {
  567. 'id': '42850523',
  568. 'ext': 'mp4',
  569. 'title': 'EA Play 2016 Live from the Novo Theatre',
  570. 'thumbnail': r're:^https?://.*\.jpg',
  571. 'timestamp': 1465767393,
  572. 'upload_date': '20160612',
  573. 'creator': 'EA',
  574. 'uploader': 'stereotype_',
  575. 'uploader_id': '43566419',
  576. },
  577. }, {
  578. # multiple formats
  579. 'url': 'https://clips.twitch.tv/rflegendary/UninterestedBeeDAESuppy',
  580. 'only_matching': True,
  581. }, {
  582. 'url': 'https://www.twitch.tv/sergeynixon/clip/StormyThankfulSproutFutureMan',
  583. 'only_matching': True,
  584. }]
  585. def _real_extract(self, url):
  586. video_id = self._match_id(url)
  587. status = self._download_json(
  588. 'https://clips.twitch.tv/api/v2/clips/%s/status' % video_id,
  589. video_id)
  590. formats = []
  591. for option in status['quality_options']:
  592. if not isinstance(option, dict):
  593. continue
  594. source = url_or_none(option.get('source'))
  595. if not source:
  596. continue
  597. formats.append({
  598. 'url': source,
  599. 'format_id': option.get('quality'),
  600. 'height': int_or_none(option.get('quality')),
  601. 'fps': int_or_none(option.get('frame_rate')),
  602. })
  603. self._sort_formats(formats)
  604. info = {
  605. 'formats': formats,
  606. }
  607. clip = self._call_api(
  608. 'kraken/clips/%s' % video_id, video_id, fatal=False, headers={
  609. 'Accept': 'application/vnd.twitchtv.v5+json',
  610. })
  611. if clip:
  612. quality_key = qualities(('tiny', 'small', 'medium'))
  613. thumbnails = []
  614. thumbnails_dict = clip.get('thumbnails')
  615. if isinstance(thumbnails_dict, dict):
  616. for thumbnail_id, thumbnail_url in thumbnails_dict.items():
  617. thumbnails.append({
  618. 'id': thumbnail_id,
  619. 'url': thumbnail_url,
  620. 'preference': quality_key(thumbnail_id),
  621. })
  622. info.update({
  623. 'id': clip.get('tracking_id') or video_id,
  624. 'title': clip.get('title') or video_id,
  625. 'duration': float_or_none(clip.get('duration')),
  626. 'views': int_or_none(clip.get('views')),
  627. 'timestamp': unified_timestamp(clip.get('created_at')),
  628. 'thumbnails': thumbnails,
  629. 'creator': try_get(clip, lambda x: x['broadcaster']['display_name'], compat_str),
  630. 'uploader': try_get(clip, lambda x: x['curator']['display_name'], compat_str),
  631. 'uploader_id': try_get(clip, lambda x: x['curator']['id'], compat_str),
  632. })
  633. else:
  634. info.update({
  635. 'title': video_id,
  636. 'id': video_id,
  637. })
  638. return info