2 from __future__
import unicode_literals
14 from .common
import InfoExtractor
17 class NebulaBaseIE(InfoExtractor
):
18 _NETRC_MACHINE
= 'watchnebula'
20 _nebula_api_token
= None
21 _nebula_bearer_token
= None
22 _zype_access_token
= None
24 def _perform_nebula_auth(self
):
25 username
, password
= self
._get
_login
_info
()
26 if not (username
and password
):
27 self
.raise_login_required()
29 data
= json
.dumps({'email': username, 'password': password}
).encode('utf8')
30 response
= self
._download
_json
(
31 'https://api.watchnebula.com/api/v1/auth/login/',
32 data
=data
, fatal
=False, video_id
=None,
34 'content-type': 'application/json',
35 # Submitting the 'sessionid' cookie always causes a 403 on auth endpoint
38 note
='Logging in to Nebula with supplied credentials',
39 errnote
='Authentication failed or rejected')
40 if not response
or not response
.get('key'):
41 self
.raise_login_required()
43 # save nebula token as cookie
45 'nebula.app', 'nebula-auth',
48 "apiToken": response
["key"],
50 "isLoggingOut": False,
51 }, separators
=(",", ":"))),
52 expire_time
=int(time
.time()) + 86400 * 365,
55 return response
['key']
57 def _retrieve_nebula_api_token(self
):
59 Check cookie jar for valid token. Try to authenticate using credentials if no valid token
60 can be found in the cookie jar.
62 nebula_cookies
= self
._get
_cookies
('https://nebula.app')
63 nebula_cookie
= nebula_cookies
.get('nebula-auth')
65 self
.to_screen('Authenticating to Nebula with token from cookie jar')
66 nebula_cookie_value
= urllib
.parse
.unquote(nebula_cookie
.value
)
67 nebula_api_token
= self
._parse
_json
(nebula_cookie_value
, None).get('apiToken')
69 return nebula_api_token
71 return self
._perform
_nebula
_auth
()
73 def _call_nebula_api(self
, url
, video_id
=None, method
='GET', auth_type
='api', note
=''):
74 assert method
in ('GET', 'POST',)
75 assert auth_type
in ('api', 'bearer',)
78 authorization
= f
'Token {self._nebula_api_token}' if auth_type
== 'api' else f
'Bearer {self._nebula_bearer_token}'
79 return self
._download
_json
(
80 url
, video_id
, note
=note
, headers
={'Authorization': authorization}
,
81 data
=b
'' if method
== 'POST' else None)
85 except ExtractorError
as exc
:
86 # if 401 or 403, attempt credential re-auth and retry
87 if exc
.cause
and isinstance(exc
.cause
, urllib
.error
.HTTPError
) and exc
.cause
.code
in (401, 403):
88 self
.to_screen(f
'Reauthenticating to Nebula and retrying, because last {auth_type} call resulted in error {exc.cause.code}')
94 def _fetch_nebula_bearer_token(self
):
96 Get a Bearer token for the Nebula API. This will be required to fetch video meta data.
98 response
= self
._call
_nebula
_api
('https://api.watchnebula.com/api/v1/authorization/',
100 note
='Authorizing to Nebula')
101 return response
['token']
103 def _fetch_zype_access_token(self
):
105 Get a Zype access token, which is required to access video streams -- in our case: to
108 user_object
= self
._call
_nebula
_api
('https://api.watchnebula.com/api/v1/auth/user/', note
='Retrieving Zype access token')
110 access_token
= try_get(user_object
, lambda x
: x
['zype_auth_info']['access_token'], str)
112 if try_get(user_object
, lambda x
: x
['is_subscribed'], bool):
113 # TODO: Reimplement the same Zype token polling the Nebula frontend implements
114 # see https://github.com/ytdl-org/youtube-dl/pull/24805#issuecomment-749231532
115 raise ExtractorError(
116 'Unable to extract Zype access token from Nebula API authentication endpoint. '
117 'Open an arbitrary video in a browser with this account to generate a token',
119 raise ExtractorError('Unable to extract Zype access token from Nebula API authentication endpoint')
122 def _build_video_info(self
, episode
):
123 zype_id
= episode
['zype_id']
124 zype_video_url
= f
'https://player.zype.com/embed/{zype_id}.html?access_token={self._zype_access_token}'
125 channel_slug
= episode
['channel_slug']
127 'id': episode
['zype_id'],
128 'display_id': episode
['slug'],
129 '_type': 'url_transparent',
131 'url': zype_video_url
,
132 'title': episode
['title'],
133 'description': episode
['description'],
134 'timestamp': parse_iso8601(episode
['published_at']),
136 # 'id': tn.get('name'), # this appears to be null
137 'url': tn
['original'],
139 } for key
, tn
in episode
['assets']['thumbnail'].items()],
140 'duration': episode
['duration'],
141 'channel': episode
['channel_title'],
142 'channel_id': channel_slug
,
143 'channel_url': f
'https://nebula.app/{channel_slug}',
144 'uploader': episode
['channel_title'],
145 'uploader_id': channel_slug
,
146 'uploader_url': f
'https://nebula.app/{channel_slug}',
147 'series': episode
['channel_title'],
148 'creator': episode
['channel_title'],
152 self
._nebula
_api
_token
= self
._retrieve
_nebula
_api
_token
()
153 self
._nebula
_bearer
_token
= self
._fetch
_nebula
_bearer
_token
()
154 self
._zype
_access
_token
= self
._fetch
_zype
_access
_token
()
156 def _real_initialize(self
):
160 class NebulaIE(NebulaBaseIE
):
161 _VALID_URL
= r
'https?://(?:www\.)?(?:watchnebula\.com|nebula\.app)/videos/(?P<id>[-\w]+)'
164 'url': 'https://nebula.app/videos/that-time-disney-remade-beauty-and-the-beast',
165 'md5': 'fe79c4df8b3aa2fea98a93d027465c7e',
167 'id': '5c271b40b13fd613090034fd',
169 'title': 'That Time Disney Remade Beauty and the Beast',
170 '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.',
171 'upload_date': '20180731',
172 'timestamp': 1533009600,
173 'channel': 'Lindsay Ellis',
174 'channel_id': 'lindsayellis',
175 'uploader': 'Lindsay Ellis',
176 'uploader_id': 'lindsayellis',
183 'url': 'https://nebula.app/videos/the-logistics-of-d-day-landing-craft-how-the-allies-got-ashore',
184 'md5': '6d4edd14ce65720fa63aba5c583fb328',
186 'id': '5e7e78171aaf320001fbd6be',
188 'title': 'Landing Craft - How The Allies Got Ashore',
189 'description': r
're:^In this episode we explore the unsung heroes of D-Day, the landing craft.',
190 'upload_date': '20200327',
191 'timestamp': 1585348140,
192 'channel': 'Real Engineering',
193 'channel_id': 'realengineering',
194 'uploader': 'Real Engineering',
195 'uploader_id': 'realengineering',
202 'url': 'https://nebula.app/videos/money-episode-1-the-draw',
203 'md5': '8c7d272910eea320f6f8e6d3084eecf5',
205 'id': '5e779ebdd157bc0001d1c75a',
207 'title': 'Episode 1: The Draw',
208 'description': r
'contains:There’s free money on offer… if the players can all work together.',
209 'upload_date': '20200323',
210 'timestamp': 1584980400,
211 'channel': 'Tom Scott Presents: Money',
212 'channel_id': 'tom-scott-presents-money',
213 'uploader': 'Tom Scott Presents: Money',
214 'uploader_id': 'tom-scott-presents-money',
221 'url': 'https://watchnebula.com/videos/money-episode-1-the-draw',
222 'only_matching': True,
226 def _fetch_video_metadata(self
, slug
):
227 return self
._call
_nebula
_api
(f
'https://content.watchnebula.com/video/{slug}/',
230 note
='Fetching video meta data')
232 def _real_extract(self
, url
):
233 slug
= self
._match
_id
(url
)
234 video
= self
._fetch
_video
_metadata
(slug
)
235 return self
._build
_video
_info
(video
)
238 class NebulaCollectionIE(NebulaBaseIE
):
239 IE_NAME
= 'nebula:collection'
240 _VALID_URL
= r
'https?://(?:www\.)?(?:watchnebula\.com|nebula\.app)/(?!videos/)(?P<id>[-\w]+)'
243 'url': 'https://nebula.app/tom-scott-presents-money',
245 'id': 'tom-scott-presents-money',
246 'title': 'Tom Scott Presents: Money',
247 'description': 'Tom Scott hosts a series all about trust, negotiation and money.',
254 'url': 'https://nebula.app/lindsayellis',
256 'id': 'lindsayellis',
257 'title': 'Lindsay Ellis',
258 'description': 'Enjoy these hottest of takes on Disney, Transformers, and Musicals.',
260 'playlist_mincount': 100,
267 def _generate_playlist_entries(self
, collection_id
, channel
):
268 episodes
= channel
['episodes']['results']
269 for page_num
in itertools
.count(2):
270 for episode
in episodes
:
271 yield self
._build
_video
_info
(episode
)
272 next_url
= channel
['episodes']['next']
275 channel
= self
._call
_nebula
_api
(next_url
, collection_id
, auth_type
='bearer',
276 note
=f
'Retrieving channel page {page_num}')
277 episodes
= channel
['episodes']['results']
279 def _real_extract(self
, url
):
280 collection_id
= self
._match
_id
(url
)
281 channel_url
= f
'https://content.watchnebula.com/video/channels/{collection_id}/'
282 channel
= self
._call
_nebula
_api
(channel_url
, collection_id
, auth_type
='bearer', note
='Retrieving channel')
283 channel_details
= channel
['details']
285 return self
.playlist_result(
286 entries
=self
._generate
_playlist
_entries
(collection_id
, channel
),
287 playlist_id
=collection_id
,
288 playlist_title
=channel_details
['title'],
289 playlist_description
=channel_details
['description']