[roosterteeth] Add extractor
[youtube-dl] / youtube_dl / extractor / roosterteeth.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     urlencode_postdata,
8 )
9
10
11 class RoosterTeethIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:.+?\.)?roosterteeth\.com/episode/(?P<id>[^/?#&]+)'
13     _LOGIN_URL = 'https://roosterteeth.com/login'
14     _NETRC_MACHINE = 'roosterteeth'
15     _TESTS = [{
16         'url': 'http://roosterteeth.com/episode/million-dollars-but-season-2-million-dollars-but-the-game-announcement',
17         'info_dict': {
18             'id': '26576',
19             'ext': 'mp4',
20             'title': 'Million Dollars, But... The Game Announcement',
21             'thumbnail': 're:^https?://.*\.png$',
22             'description': 'Introducing Million Dollars, But... The Game! Available for pre-order now at www.MDBGame.com ',
23             'creator': 'Rooster Teeth',
24             'series': 'Million Dollars, But...',
25             'episode': 'Million Dollars, But... The Game Announcement',
26             'episode_id': '26576',
27         },
28         'params': {
29             'skip_download': True,  # m3u8 downloads
30         },
31     }, {
32         'url': 'http://achievementhunter.roosterteeth.com/episode/off-topic-the-achievement-hunter-podcast-2016-i-didn-t-think-it-would-pass-31',
33         'only_matching': True,
34     }, {
35         'url': 'http://funhaus.roosterteeth.com/episode/funhaus-shorts-2016-austin-sucks-funhaus-shorts',
36         'only_matching': True,
37     }, {
38         'url': 'http://screwattack.roosterteeth.com/episode/death-battle-season-3-mewtwo-vs-shadow',
39         'only_matching': True,
40     }, {
41         'url': 'http://theknow.roosterteeth.com/episode/the-know-game-news-season-1-boring-steam-sales-are-better',
42         'only_matching': True,
43     }]
44
45     def _login(self):
46         (username, password) = self._get_login_info()
47         if username is None or password is None:
48             return False
49
50         # token is required to authenticate request
51         login_page = self._download_webpage(self._LOGIN_URL, None, 'Getting login token', 'Unable to get login token')
52
53         login_form = self._hidden_inputs(login_page)
54         login_form.update({
55             'username': username,
56             'password': password,
57         })
58         login_payload = urlencode_postdata(login_form)
59
60         # required for proper responses
61         login_headers = {
62             'Referer': self._LOGIN_URL,
63         }
64
65         login_request = self._download_webpage(
66             self._LOGIN_URL, None,
67             note='Logging in as %s' % username,
68             data=login_payload,
69             headers=login_headers)
70
71         if 'Authentication failed' in login_request:
72             raise ExtractorError(
73                 'Login failed (invalid username/password)', expected=True)
74
75     def _real_initialize(self):
76         self._login()
77
78     def _real_extract(self, url):
79         match_id = self._match_id(url)
80         webpage = self._download_webpage(url, match_id)
81
82         episode_id = self._html_search_regex(r"commentControls\('#comment-([0-9]+)'\)", webpage, 'episode id', match_id, False)
83
84         self.report_extraction(episode_id)
85
86         title = self._html_search_regex(r'<title>([^<]+)</title>', webpage, 'episode title', self._og_search_title(webpage), False)
87         thumbnail = self._og_search_thumbnail(webpage)
88         description = self._og_search_description(webpage)
89         creator = self._html_search_regex(r'<h3>Latest (.+) Gear</h3>', webpage, 'site', 'Rooster Teeth', False)
90         series = self._html_search_regex(r'<h2>More ([^<]+)</h2>', webpage, 'series', fatal=False)
91         episode = self._html_search_regex(r'<title>([^<]+)</title>', webpage, 'episode title', fatal=False)
92
93         if '<div class="non-sponsor">' in webpage:
94             self.raise_login_required('%s is only available for FIRST members' % title)
95
96         if '<div class="golive-gate">' in webpage:
97             self.raise_login_required('%s is not available yet' % title)
98
99         formats = self._extract_m3u8_formats(self._html_search_regex(r"file: '(.+?)m3u8'", webpage, 'm3u8 url') + 'm3u8', episode_id, ext='mp4')
100         self._sort_formats(formats)
101
102         return {
103             'id': episode_id,
104             'title': title,
105             'formats': formats,
106             'thumbnail': thumbnail,
107             'description': description,
108             'creator': creator,
109             'series': series,
110             'episode': episode,
111             'episode_id': episode_id,
112         }