1
0

roosterteeth.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_HTTPError,
  7. compat_str,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. str_or_none,
  13. urlencode_postdata,
  14. )
  15. class RoosterTeethIE(InfoExtractor):
  16. _VALID_URL = r'https?://(?:.+?\.)?roosterteeth\.com/episode/(?P<id>[^/?#&]+)'
  17. _LOGIN_URL = 'https://roosterteeth.com/login'
  18. _NETRC_MACHINE = 'roosterteeth'
  19. _TESTS = [{
  20. 'url': 'http://roosterteeth.com/episode/million-dollars-but-season-2-million-dollars-but-the-game-announcement',
  21. 'md5': 'e2bd7764732d785ef797700a2489f212',
  22. 'info_dict': {
  23. 'id': '9156',
  24. 'display_id': 'million-dollars-but-season-2-million-dollars-but-the-game-announcement',
  25. 'ext': 'mp4',
  26. 'title': 'Million Dollars, But... The Game Announcement',
  27. 'description': 'md5:168a54b40e228e79f4ddb141e89fe4f5',
  28. 'thumbnail': r're:^https?://.*\.png$',
  29. 'series': 'Million Dollars, But...',
  30. 'episode': 'Million Dollars, But... The Game Announcement',
  31. },
  32. }, {
  33. 'url': 'http://achievementhunter.roosterteeth.com/episode/off-topic-the-achievement-hunter-podcast-2016-i-didn-t-think-it-would-pass-31',
  34. 'only_matching': True,
  35. }, {
  36. 'url': 'http://funhaus.roosterteeth.com/episode/funhaus-shorts-2016-austin-sucks-funhaus-shorts',
  37. 'only_matching': True,
  38. }, {
  39. 'url': 'http://screwattack.roosterteeth.com/episode/death-battle-season-3-mewtwo-vs-shadow',
  40. 'only_matching': True,
  41. }, {
  42. 'url': 'http://theknow.roosterteeth.com/episode/the-know-game-news-season-1-boring-steam-sales-are-better',
  43. 'only_matching': True,
  44. }, {
  45. # only available for FIRST members
  46. 'url': 'http://roosterteeth.com/episode/rt-docs-the-world-s-greatest-head-massage-the-world-s-greatest-head-massage-an-asmr-journey-part-one',
  47. 'only_matching': True,
  48. }]
  49. def _login(self):
  50. username, password = self._get_login_info()
  51. if username is None:
  52. return
  53. login_page = self._download_webpage(
  54. self._LOGIN_URL, None,
  55. note='Downloading login page',
  56. errnote='Unable to download login page')
  57. login_form = self._hidden_inputs(login_page)
  58. login_form.update({
  59. 'username': username,
  60. 'password': password,
  61. })
  62. login_request = self._download_webpage(
  63. self._LOGIN_URL, None,
  64. note='Logging in',
  65. data=urlencode_postdata(login_form),
  66. headers={
  67. 'Referer': self._LOGIN_URL,
  68. })
  69. if not any(re.search(p, login_request) for p in (
  70. r'href=["\']https?://(?:www\.)?roosterteeth\.com/logout"',
  71. r'>Sign Out<')):
  72. error = self._html_search_regex(
  73. r'(?s)<div[^>]+class=(["\']).*?\balert-danger\b.*?\1[^>]*>(?:\s*<button[^>]*>.*?</button>)?(?P<error>.+?)</div>',
  74. login_request, 'alert', default=None, group='error')
  75. if error:
  76. raise ExtractorError('Unable to login: %s' % error, expected=True)
  77. raise ExtractorError('Unable to log in')
  78. def _real_initialize(self):
  79. self._login()
  80. def _real_extract(self, url):
  81. display_id = self._match_id(url)
  82. api_episode_url = 'https://svod-be.roosterteeth.com/api/v1/episodes/%s' % display_id
  83. try:
  84. m3u8_url = self._download_json(
  85. api_episode_url + '/videos', display_id,
  86. 'Downloading video JSON metadata')['data'][0]['attributes']['url']
  87. except ExtractorError as e:
  88. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
  89. if self._parse_json(e.cause.read().decode(), display_id).get('access') is False:
  90. self.raise_login_required(
  91. '%s is only available for FIRST members' % display_id)
  92. raise
  93. formats = self._extract_m3u8_formats(
  94. m3u8_url, display_id, 'mp4', 'm3u8_native', m3u8_id='hls')
  95. self._sort_formats(formats)
  96. episode = self._download_json(
  97. api_episode_url, display_id,
  98. 'Downloading episode JSON metadata')['data'][0]
  99. attributes = episode['attributes']
  100. title = attributes.get('title') or attributes['display_title']
  101. video_id = compat_str(episode['id'])
  102. thumbnails = []
  103. for image in episode.get('included', {}).get('images', []):
  104. if image.get('type') == 'episode_image':
  105. img_attributes = image.get('attributes') or {}
  106. for k in ('thumb', 'small', 'medium', 'large'):
  107. img_url = img_attributes.get(k)
  108. if img_url:
  109. thumbnails.append({
  110. 'id': k,
  111. 'url': img_url,
  112. })
  113. return {
  114. 'id': video_id,
  115. 'display_id': display_id,
  116. 'title': title,
  117. 'description': attributes.get('description') or attributes.get('caption'),
  118. 'thumbnails': thumbnails,
  119. 'series': attributes.get('show_title'),
  120. 'season_number': int_or_none(attributes.get('season_number')),
  121. 'season_id': attributes.get('season_id'),
  122. 'episode': title,
  123. 'episode_number': int_or_none(attributes.get('number')),
  124. 'episode_id': str_or_none(episode.get('uuid')),
  125. 'formats': formats,
  126. 'channel_id': attributes.get('channel_id'),
  127. 'duration': int_or_none(attributes.get('length')),
  128. }