2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
7 from ..compat
import compat_str
16 class NebulaIE(InfoExtractor
):
18 _VALID_URL
= r
'https?://(?:www\.)?(?:watchnebula\.com|nebula\.app)/videos/(?P<id>[-\w]+)'
21 'url': 'https://nebula.app/videos/that-time-disney-remade-beauty-and-the-beast',
22 'md5': 'fe79c4df8b3aa2fea98a93d027465c7e',
24 'id': '5c271b40b13fd613090034fd',
26 'title': 'That Time Disney Remade Beauty and the Beast',
27 'description': 'Note: this video was originally posted on YouTube with the sponsor read included. We weren’t able to remove it without reducing video quality, so it’s presented here in its original context.',
28 'upload_date': '20180731',
29 'timestamp': 1533009600,
30 'channel': 'Lindsay Ellis',
31 'uploader': 'Lindsay Ellis',
36 'skip': 'All Nebula content requires authentication',
39 'url': 'https://nebula.app/videos/the-logistics-of-d-day-landing-craft-how-the-allies-got-ashore',
40 'md5': '6d4edd14ce65720fa63aba5c583fb328',
42 'id': '5e7e78171aaf320001fbd6be',
44 'title': 'Landing Craft - How The Allies Got Ashore',
45 'description': r
're:^In this episode we explore the unsung heroes of D-Day, the landing craft.',
46 'upload_date': '20200327',
47 'timestamp': 1585348140,
48 'channel': 'The Logistics of D-Day',
49 'uploader': 'The Logistics of D-Day',
54 'skip': 'All Nebula content requires authentication',
57 'url': 'https://nebula.app/videos/money-episode-1-the-draw',
58 'md5': '8c7d272910eea320f6f8e6d3084eecf5',
60 'id': '5e779ebdd157bc0001d1c75a',
62 'title': 'Episode 1: The Draw',
63 'description': r
'contains:There’s free money on offer… if the players can all work together.',
64 'upload_date': '20200323',
65 'timestamp': 1584980400,
66 'channel': 'Tom Scott Presents: Money',
67 'uploader': 'Tom Scott Presents: Money',
72 'skip': 'All Nebula content requires authentication',
75 'url': 'https://watchnebula.com/videos/money-episode-1-the-draw',
76 'only_matching': True,
79 _NETRC_MACHINE
= 'watchnebula'
81 def _retrieve_nebula_auth(self
, video_id
):
83 Log in to Nebula, and returns a Nebula API token
86 username
, password
= self
._get
_login
_info
()
87 if not (username
and password
):
88 self
.raise_login_required()
91 data
= json
.dumps({'email': username, 'password': password}
).encode('utf8')
92 response
= self
._download
_json
(
93 'https://api.watchnebula.com/api/v1/auth/login/',
94 data
=data
, fatal
=False, video_id
=video_id
,
96 'content-type': 'application/json',
97 # Submitting the 'sessionid' cookie always causes a 403 on auth endpoint
100 note
='Authenticating to Nebula with supplied credentials',
101 errnote
='Authentication failed or rejected')
102 if not response
or not response
.get('key'):
103 self
.raise_login_required()
104 return response
['key']
106 def _retrieve_zype_api_key(self
, page_url
, display_id
):
108 Retrieves the Zype API key
111 # Find the js that has the API key from the webpage and download it
112 webpage
= self
._download
_webpage
(page_url
, video_id
=display_id
)
113 main_script_relpath
= self
._search
_regex
(
114 r
'<script[^>]*src="(?P<script_relpath>[^"]*main.[0-9a-f]*.chunk.js)"[^>]*>', webpage
,
115 group
='script_relpath', name
='script relative path', fatal
=True)
116 main_script_abspath
= urljoin(page_url
, main_script_relpath
)
117 main_script
= self
._download
_webpage
(main_script_abspath
, video_id
=display_id
,
118 note
='Retrieving Zype API key')
120 api_key
= self
._search
_regex
(
121 r
'REACT_APP_ZYPE_API_KEY\s*:\s*"(?P<api_key>[\w-]*)"', main_script
,
122 group
='api_key', name
='API key', fatal
=True)
126 def _call_zype_api(self
, path
, params
, video_id
, api_key
, note
):
128 A helper for making calls to the Zype API.
130 query
= {'api_key': api_key, 'per_page': 1}
132 return self
._download
_json
('https://api.zype.com' + path
, video_id
, query
=query
, note
=note
)
134 def _call_nebula_api(self
, path
, video_id
, access_token
, note
):
136 A helper for making calls to the Nebula API.
138 return self
._download
_json
('https://api.watchnebula.com/api/v1' + path
, video_id
, headers
={
139 'Authorization': 'Token {access_token}'.format(access_token
=access_token
)
142 def _fetch_zype_access_token(self
, video_id
, nebula_token
):
143 user_object
= self
._call
_nebula
_api
('/auth/user/', video_id
, nebula_token
, note
='Retrieving Zype access token')
144 access_token
= try_get(user_object
, lambda x
: x
['zype_auth_info']['access_token'], compat_str
)
146 if try_get(user_object
, lambda x
: x
['is_subscribed'], bool):
147 # TODO: Reimplement the same Zype token polling the Nebula frontend implements
148 # see https://github.com/ytdl-org/youtube-dl/pull/24805#issuecomment-749231532
149 raise ExtractorError(
150 'Unable to extract Zype access token from Nebula API authentication endpoint. '
151 'Open an arbitrary video in a browser with this account to generate a token',
153 raise ExtractorError('Unable to extract Zype access token from Nebula API authentication endpoint')
156 def _extract_channel_title(self
, video_meta
):
157 # TODO: Implement the API calls giving us the channel list,
158 # so that we can do the title lookup and then figure out the channel URL
159 categories
= video_meta
.get('categories', []) if video_meta
else []
160 # the channel name is the value of the first category
161 for category
in categories
:
162 if category
.get('value'):
163 return category
['value'][0]
165 def _real_extract(self
, url
):
166 display_id
= self
._match
_id
(url
)
167 nebula_token
= self
._retrieve
_nebula
_auth
(display_id
)
168 api_key
= self
._retrieve
_zype
_api
_key
(url
, display_id
)
170 response
= self
._call
_zype
_api
('/videos', {'friendly_title': display_id}
,
171 display_id
, api_key
, note
='Retrieving metadata from Zype')
172 if len(response
.get('response') or []) != 1:
173 raise ExtractorError('Unable to find video on Zype API')
174 video_meta
= response
['response'][0]
176 video_id
= video_meta
['_id']
177 zype_access_token
= self
._fetch
_zype
_access
_token
(display_id
, nebula_token
=nebula_token
)
179 channel_title
= self
._extract
_channel
_title
(video_meta
)
183 'display_id': display_id
,
184 '_type': 'url_transparent',
186 'url': 'https://player.zype.com/embed/%s.html?access_token=%s' % (video_id
, zype_access_token
),
187 'title': video_meta
.get('title'),
188 'description': video_meta
.get('description'),
189 'timestamp': parse_iso8601(video_meta
.get('published_at')),
192 'id': tn
.get('name'), # this appears to be null
194 'width': tn
.get('width'),
195 'height': tn
.get('height'),
196 } for tn
in video_meta
.get('thumbnails', [])],
197 'duration': video_meta
.get('duration'),
198 'channel': channel_title
,
199 'uploader': channel_title
, # we chose uploader = channel name
200 # TODO: uploader_url, channel_id, channel_url