summaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/roosterteeth.py
blob: 8883639b290b9712eb94ef05ab41ecaff7b06ee3 (plain)
    1 # coding: utf-8
    2 from __future__ import unicode_literals
    3 
    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 
   16 
   17 class RoosterTeethIE(InfoExtractor):
   18     _VALID_URL = r'https?://(?:.+?\.)?roosterteeth\.com/(?:episode|watch)/(?P<id>[^/?#&]+)'
   19     _NETRC_MACHINE = 'roosterteeth'
   20     _TESTS = [{
   21         'url': 'http://roosterteeth.com/episode/million-dollars-but-season-2-million-dollars-but-the-game-announcement',
   22         'md5': 'e2bd7764732d785ef797700a2489f212',
   23         'info_dict': {
   24             'id': '9156',
   25             'display_id': 'million-dollars-but-season-2-million-dollars-but-the-game-announcement',
   26             'ext': 'mp4',
   27             'title': 'Million Dollars, But... The Game Announcement',
   28             'description': 'md5:168a54b40e228e79f4ddb141e89fe4f5',
   29             'thumbnail': r're:^https?://.*\.png$',
   30             'series': 'Million Dollars, But...',
   31             'episode': 'Million Dollars, But... The Game Announcement',
   32         },
   33     }, {
   34         'url': 'http://achievementhunter.roosterteeth.com/episode/off-topic-the-achievement-hunter-podcast-2016-i-didn-t-think-it-would-pass-31',
   35         'only_matching': True,
   36     }, {
   37         'url': 'http://funhaus.roosterteeth.com/episode/funhaus-shorts-2016-austin-sucks-funhaus-shorts',
   38         'only_matching': True,
   39     }, {
   40         'url': 'http://screwattack.roosterteeth.com/episode/death-battle-season-3-mewtwo-vs-shadow',
   41         'only_matching': True,
   42     }, {
   43         'url': 'http://theknow.roosterteeth.com/episode/the-know-game-news-season-1-boring-steam-sales-are-better',
   44         'only_matching': True,
   45     }, {
   46         # only available for FIRST members
   47         'url': 'http://roosterteeth.com/episode/rt-docs-the-world-s-greatest-head-massage-the-world-s-greatest-head-massage-an-asmr-journey-part-one',
   48         'only_matching': True,
   49     }, {
   50         'url': 'https://roosterteeth.com/watch/million-dollars-but-season-2-million-dollars-but-the-game-announcement',
   51         'only_matching': True,
   52     }]
   53     _EPISODE_BASE_URL = 'https://svod-be.roosterteeth.com/api/v1/episodes/'
   54 
   55     def _login(self):
   56         username, password = self._get_login_info()
   57         if username is None:
   58             return
   59 
   60         try:
   61             self._download_json(
   62                 'https://auth.roosterteeth.com/oauth/token',
   63                 None, 'Logging in', data=urlencode_postdata({
   64                     'client_id': '4338d2b4bdc8db1239360f28e72f0d9ddb1fd01e7a38fbb07b4b1f4ba4564cc5',
   65                     'grant_type': 'password',
   66                     'username': username,
   67                     'password': password,
   68                 }))
   69         except ExtractorError as e:
   70             msg = 'Unable to login'
   71             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
   72                 resp = self._parse_json(e.cause.read().decode(), None, fatal=False)
   73                 if resp:
   74                     error = resp.get('extra_info') or resp.get('error_description') or resp.get('error')
   75                     if error:
   76                         msg += ': ' + error
   77             self.report_warning(msg)
   78 
   79     def _real_initialize(self):
   80         if self._get_cookies(self._EPISODE_BASE_URL).get('rt_access_token'):
   81             return
   82         self._login()
   83 
   84     def _real_extract(self, url):
   85         display_id = self._match_id(url)
   86         api_episode_url = self._EPISODE_BASE_URL + display_id
   87 
   88         try:
   89             m3u8_url = self._download_json(
   90                 api_episode_url + '/videos', display_id,
   91                 'Downloading video JSON metadata')['data'][0]['attributes']['url']
   92         except ExtractorError as e:
   93             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
   94                 if self._parse_json(e.cause.read().decode(), display_id).get('access') is False:
   95                     self.raise_login_required(
   96                         '%s is only available for FIRST members' % display_id)
   97             raise
   98 
   99         formats = self._extract_m3u8_formats(
  100             m3u8_url, display_id, 'mp4', 'm3u8_native', m3u8_id='hls')
  101         self._sort_formats(formats)
  102 
  103         episode = self._download_json(
  104             api_episode_url, display_id,
  105             'Downloading episode JSON metadata')['data'][0]
  106         attributes = episode['attributes']
  107         title = attributes.get('title') or attributes['display_title']
  108         video_id = compat_str(episode['id'])
  109 
  110         thumbnails = []
  111         for image in episode.get('included', {}).get('images', []):
  112             if image.get('type') == 'episode_image':
  113                 img_attributes = image.get('attributes') or {}
  114                 for k in ('thumb', 'small', 'medium', 'large'):
  115                     img_url = img_attributes.get(k)
  116                     if img_url:
  117                         thumbnails.append({
  118                             'id': k,
  119                             'url': img_url,
  120                         })
  121 
  122         return {
  123             'id': video_id,
  124             'display_id': display_id,
  125             'title': title,
  126             'description': attributes.get('description') or attributes.get('caption'),
  127             'thumbnails': thumbnails,
  128             'series': attributes.get('show_title'),
  129             'season_number': int_or_none(attributes.get('season_number')),
  130             'season_id': attributes.get('season_id'),
  131             'episode': title,
  132             'episode_number': int_or_none(attributes.get('number')),
  133             'episode_id': str_or_none(episode.get('uuid')),
  134             'formats': formats,
  135             'channel_id': attributes.get('channel_id'),
  136             'duration': int_or_none(attributes.get('length')),
  137         }

Generated by cgit