171 lines
6.2 KiB
Python
Raw Normal View History

2016-06-22 02:58:42 -05:00
# coding: utf-8
from __future__ import unicode_literals
import time
2016-07-10 01:28:28 +07:00
2016-06-22 02:58:42 -05:00
from .common import InfoExtractor
from ..utils import (
ExtractorError,
str_or_none,
unified_timestamp,
2016-06-22 02:58:42 -05:00
urlencode_postdata,
)
class RoosterTeethIE(InfoExtractor):
_VALID_URL = r'https?://(?:.+?\.)?roosterteeth\.com/episode/(?P<id>[^/?#&]+)'
_LOGIN_URL = 'https://auth.roosterteeth.com/oauth/token'
2018-04-08 17:48:10 -04:00
_API_URL = 'https://svod-be.roosterteeth.com/api/v1/episodes/'
_ACCESS_TOKEN = None
2016-06-22 02:58:42 -05:00
_NETRC_MACHINE = 'roosterteeth'
_TESTS = [{
'url': 'http://roosterteeth.com/episode/million-dollars-but-season-2-million-dollars-but-the-game-announcement',
2016-07-10 01:28:28 +07:00
'md5': 'e2bd7764732d785ef797700a2489f212',
2016-06-22 02:58:42 -05:00
'info_dict': {
'id': '9156',
2016-07-10 01:28:28 +07:00
'display_id': 'million-dollars-but-season-2-million-dollars-but-the-game-announcement',
2016-06-22 02:58:42 -05:00
'ext': 'mp4',
'title': 'Million Dollars, But... The Game Announcement',
2016-07-10 01:28:28 +07:00
'description': 'md5:0cc3b21986d54ed815f5faeccd9a9ca5',
'thumbnail': r're:^https?://.*\.png$',
2016-06-22 02:58:42 -05:00
'series': 'Million Dollars, But...',
'episode': 'S2:E10 - Million Dollars, But... The Game Announcement',
2016-06-22 02:58:42 -05:00
},
}, {
'url': 'http://achievementhunter.roosterteeth.com/episode/off-topic-the-achievement-hunter-podcast-2016-i-didn-t-think-it-would-pass-31',
'only_matching': True,
}, {
'url': 'http://funhaus.roosterteeth.com/episode/funhaus-shorts-2016-austin-sucks-funhaus-shorts',
'only_matching': True,
}, {
'url': 'http://screwattack.roosterteeth.com/episode/death-battle-season-3-mewtwo-vs-shadow',
'only_matching': True,
}, {
'url': 'http://theknow.roosterteeth.com/episode/the-know-game-news-season-1-boring-steam-sales-are-better',
'only_matching': True,
2016-07-10 01:28:28 +07:00
}, {
# only available for FIRST members
'url': 'http://roosterteeth.com/episode/rt-docs-the-world-s-greatest-head-massage-the-world-s-greatest-head-massage-an-asmr-journey-part-one',
'only_matching': True,
2016-06-22 02:58:42 -05:00
}]
def _login(self):
(username, password) = self._get_login_info()
2016-07-10 01:28:28 +07:00
if username is None:
return
2016-06-22 02:58:42 -05:00
response = self._download_json(
2016-06-22 02:58:42 -05:00
self._LOGIN_URL, None,
note='Logging in',
errnote='Unable to log in',
data=urlencode_postdata({
'username': username,
'password': password,
'client_id': '4338d2b4bdc8db1239360f28e72f0d9ddb1fd01e7a38fbb07b4b1f4ba4564cc5',
'grant_type': 'password',
2016-07-10 01:28:28 +07:00
})
)
2016-07-10 01:28:28 +07:00
self._ACCESS_TOKEN = response.get('access_token')
if not self._ACCESS_TOKEN:
2016-07-10 01:28:28 +07:00
raise ExtractorError('Unable to log in')
2016-06-22 02:58:42 -05:00
def _real_initialize(self):
self._login()
def _real_extract(self, url):
2016-07-10 01:28:28 +07:00
display_id = self._match_id(url)
headers = {}
if self._ACCESS_TOKEN:
2018-04-08 17:48:10 -04:00
headers['Authorization'] = 'Bearer {}'.format(self._ACCESS_TOKEN)
api_response = self._call_api(
display_id,
note='Downloading video information (1/2)',
errnote='Unable to download video information (1/2)',
headers=headers,
)
if len(api_response.get('data', [])) == 0:
raise ExtractorError('Unable to download video information')
data = api_response.get('data')[0]
attributes = data.get('attributes', {})
episode = attributes.get('display_title')
title = attributes.get('title')
description = attributes.get('caption')
series = attributes.get('show_title')
images = data.get('included', {}).get('images')
if images and len(images) > 0:
images = images[0]
thumbnail = images.get('attributes', {}).get('thumb')
video_response = self._call_api(
display_id,
path='/videos',
note='Downloading video information (2/2)',
errnote='Unable to download video information (2/2)',
headers=headers,
)
if video_response.get('access') is not None:
now = time.time()
sponsor_golive = unified_timestamp(attributes.get('sponsor_golive_at'))
member_golive = unified_timestamp(attributes.get('member_golive_at'))
public_golive = unified_timestamp(attributes.get('public_golive_at'))
if attributes.get('is_sponsors_only', False):
if now < sponsor_golive:
self._golive_error(display_id, 'FIRST members')
else:
self.raise_login_required('{0} is only available for FIRST members'.format(display_id))
else:
if now < member_golive:
self._golive_error(display_id, 'site members')
elif now < public_golive:
self._golive_error(display_id, 'the public')
else:
raise ExtractorError('Video is not available')
if len(video_response.get('data', [])) == 0:
raise ExtractorError('Unable to download video information')
video_attributes = video_response.get('data')[0].get('attributes')
m3u8_url = video_attributes.get('url')
2016-07-10 01:28:28 +07:00
if not m3u8_url:
raise ExtractorError('Unable to extract m3u8 URL')
formats = self._extract_m3u8_formats(
m3u8_url, display_id, ext='mp4',
entry_protocol='m3u8_native', m3u8_id='hls')
2016-06-22 02:58:42 -05:00
self._sort_formats(formats)
video_id = str_or_none(video_attributes.get('content_id'))
2016-07-10 01:28:28 +07:00
2016-06-22 02:58:42 -05:00
return {
2016-07-10 01:28:28 +07:00
'id': video_id,
'display_id': display_id,
2016-06-22 02:58:42 -05:00
'title': title,
'description': description,
2016-07-10 01:28:28 +07:00
'thumbnail': thumbnail,
2016-06-22 02:58:42 -05:00
'series': series,
'episode': episode,
2016-07-10 01:28:28 +07:00
'formats': formats,
2016-06-22 02:58:42 -05:00
}
def _golive_error(self, video_id, member_level):
raise ExtractorError('{0} is not yet live for {1}'.format(video_id, member_level))
def _call_api(self, video_id, path=None, **kwargs):
url = self._API_URL + video_id
if path:
url = url + path
return self._download_json(
url,
video_id,
**kwargs
)