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