]> jfr.im git - yt-dlp.git/commitdiff
[nebula] Authentication via tokens from cookie jar (#537)
authorHenrik Heimbuerger <redacted>
Wed, 21 Jul 2021 12:42:43 +0000 (14:42 +0200)
committerGitHub <redacted>
Wed, 21 Jul 2021 12:42:43 +0000 (18:12 +0530)
Closes #496
Co-authored-by: hheimbuerger, TpmKranz
yt_dlp/extractor/nebula.py

index 1a0a394f1ad6488864240dd3c9b6f3369280f100..4426a8fdc932bc0e4b046a698b827e740bff1507 100644 (file)
@@ -2,9 +2,11 @@
 from __future__ import unicode_literals\r
 \r
 import json\r
+import time\r
 \r
+from urllib.error import HTTPError\r
 from .common import InfoExtractor\r
-from ..compat import compat_str\r
+from ..compat import compat_str, compat_urllib_parse_unquote, compat_urllib_parse_quote\r
 from ..utils import (\r
     ExtractorError,\r
     parse_iso8601,\r
@@ -78,7 +80,9 @@ class NebulaIE(InfoExtractor):
     ]\r
     _NETRC_MACHINE = 'watchnebula'\r
 \r
-    def _retrieve_nebula_auth(self, video_id):\r
+    _nebula_token = None\r
+\r
+    def _retrieve_nebula_auth(self):\r
         """\r
         Log in to Nebula, and returns a Nebula API token\r
         """\r
@@ -91,7 +95,7 @@ def _retrieve_nebula_auth(self, video_id):
         data = json.dumps({'email': username, 'password': password}).encode('utf8')\r
         response = self._download_json(\r
             'https://api.watchnebula.com/api/v1/auth/login/',\r
-            data=data, fatal=False, video_id=video_id,\r
+            data=data, fatal=False, video_id=None,\r
             headers={\r
                 'content-type': 'application/json',\r
                 # Submitting the 'sessionid' cookie always causes a 403 on auth endpoint\r
@@ -101,6 +105,19 @@ def _retrieve_nebula_auth(self, video_id):
             errnote='Authentication failed or rejected')\r
         if not response or not response.get('key'):\r
             self.raise_login_required()\r
+\r
+        # save nebula token as cookie\r
+        self._set_cookie(\r
+            'nebula.app', 'nebula-auth',\r
+            compat_urllib_parse_quote(\r
+                json.dumps({\r
+                    "apiToken": response["key"],\r
+                    "isLoggingIn": False,\r
+                    "isLoggingOut": False,\r
+                }, separators=(",", ":"))),\r
+            expire_time=int(time.time()) + 86400 * 365,\r
+        )\r
+\r
         return response['key']\r
 \r
     def _retrieve_zype_api_key(self, page_url, display_id):\r
@@ -139,8 +156,17 @@ def _call_nebula_api(self, path, video_id, access_token, note):
             'Authorization': 'Token {access_token}'.format(access_token=access_token)\r
         }, note=note)\r
 \r
-    def _fetch_zype_access_token(self, video_id, nebula_token):\r
-        user_object = self._call_nebula_api('/auth/user/', video_id, nebula_token, note='Retrieving Zype access token')\r
+    def _fetch_zype_access_token(self, video_id):\r
+        try:\r
+            user_object = self._call_nebula_api('/auth/user/', video_id, self._nebula_token, note='Retrieving Zype access token')\r
+        except ExtractorError as exc:\r
+            # if 401, attempt credential auth and retry\r
+            if exc.cause and isinstance(exc.cause, HTTPError) and exc.cause.code == 401:\r
+                self._nebula_token = self._retrieve_nebula_auth()\r
+                user_object = self._call_nebula_api('/auth/user/', video_id, self._nebula_token, note='Retrieving Zype access token')\r
+            else:\r
+                raise\r
+\r
         access_token = try_get(user_object, lambda x: x['zype_auth_info']['access_token'], compat_str)\r
         if not access_token:\r
             if try_get(user_object, lambda x: x['is_subscribed'], bool):\r
@@ -162,9 +188,21 @@ def _extract_channel_title(self, video_meta):
             if category.get('value'):\r
                 return category['value'][0]\r
 \r
+    def _real_initialize(self):\r
+        # check cookie jar for valid token\r
+        nebula_cookies = self._get_cookies('https://nebula.app')\r
+        nebula_cookie = nebula_cookies.get('nebula-auth')\r
+        if nebula_cookie:\r
+            self.to_screen('Authenticating to Nebula with token from cookie jar')\r
+            nebula_cookie_value = compat_urllib_parse_unquote(nebula_cookie.value)\r
+            self._nebula_token = self._parse_json(nebula_cookie_value, None).get('apiToken')\r
+\r
+        # try to authenticate using credentials if no valid token has been found\r
+        if not self._nebula_token:\r
+            self._nebula_token = self._retrieve_nebula_auth()\r
+\r
     def _real_extract(self, url):\r
         display_id = self._match_id(url)\r
-        nebula_token = self._retrieve_nebula_auth(display_id)\r
         api_key = self._retrieve_zype_api_key(url, display_id)\r
 \r
         response = self._call_zype_api('/videos', {'friendly_title': display_id},\r
@@ -174,7 +212,7 @@ def _real_extract(self, url):
         video_meta = response['response'][0]\r
 \r
         video_id = video_meta['_id']\r
-        zype_access_token = self._fetch_zype_access_token(display_id, nebula_token=nebula_token)\r
+        zype_access_token = self._fetch_zype_access_token(display_id)\r
 \r
         channel_title = self._extract_channel_title(video_meta)\r
 \r
@@ -187,13 +225,12 @@ def _real_extract(self, url):
             'title': video_meta.get('title'),\r
             'description': video_meta.get('description'),\r
             'timestamp': parse_iso8601(video_meta.get('published_at')),\r
-            'thumbnails': [\r
-                {\r
-                    'id': tn.get('name'),  # this appears to be null\r
-                    'url': tn['url'],\r
-                    'width': tn.get('width'),\r
-                    'height': tn.get('height'),\r
-                } for tn in video_meta.get('thumbnails', [])],\r
+            'thumbnails': [{\r
+                'id': tn.get('name'),  # this appears to be null\r
+                'url': tn['url'],\r
+                'width': tn.get('width'),\r
+                'height': tn.get('height'),\r
+            } for tn in video_meta.get('thumbnails', [])],\r
             'duration': video_meta.get('duration'),\r
             'channel': channel_title,\r
             'uploader': channel_title,  # we chose uploader = channel name\r