Browse Source

[extractor] Add `_perform_login` function (#2943)

* Adds new functions `_initialize_pre_login` and `_perform_login` as part of the extractor API
* Adds `ie.supports_login` to the public API
pukkandan 3 years ago
parent
commit
52efa4b312

+ 2 - 7
test/test_iqiyi_sdk_interpreter.py

@@ -12,11 +12,6 @@ from test.helper import FakeYDL, is_download_test
 from yt_dlp.extractor import IqiyiIE
 
 
-class IqiyiIEWithCredentials(IqiyiIE):
-    def _get_login_info(self):
-        return 'foo', 'bar'
-
-
 class WarningLogger(object):
     def __init__(self):
         self.messages = []
@@ -40,8 +35,8 @@ class TestIqiyiSDKInterpreter(unittest.TestCase):
         If `sign` is incorrect, /validate call throws an HTTP 556 error
         '''
         logger = WarningLogger()
-        ie = IqiyiIEWithCredentials(FakeYDL({'logger': logger}))
-        ie._login()
+        ie = IqiyiIE(FakeYDL({'logger': logger}))
+        ie._perform_login('foo', 'bar')
         self.assertTrue('unable to log in:' in logger.messages[0])
 
 

+ 7 - 6
test/test_netrc.py

@@ -7,18 +7,19 @@ import unittest
 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 
 
-from yt_dlp.extractor import (
-    gen_extractors,
-)
+from yt_dlp.extractor import gen_extractor_classes
+from yt_dlp.extractor.common import InfoExtractor
+
+NO_LOGIN = InfoExtractor._perform_login
 
 
 class TestNetRc(unittest.TestCase):
     def test_netrc_present(self):
-        for ie in gen_extractors():
-            if not hasattr(ie, '_login'):
+        for ie in gen_extractor_classes():
+            if ie._perform_login is NO_LOGIN:
                 continue
             self.assertTrue(
-                hasattr(ie, '_NETRC_MACHINE'),
+                ie._NETRC_MACHINE,
                 'Extractor %s supports login, but is missing a _NETRC_MACHINE property' % ie.IE_NAME)
 
 

+ 1 - 9
yt_dlp/extractor/abematv.py

@@ -291,15 +291,7 @@ class AbemaTVIE(AbemaTVBaseIE):
 
         return self._MEDIATOKEN
 
-    def _real_initialize(self):
-        self._login()
-
-    def _login(self):
-        username, password = self._get_login_info()
-        # No authentication to be performed
-        if not username:
-            return True
-
+    def _perform_login(self, username, password):
         if '@' in username:  # don't strictly check if it's email address or not
             ep, method = 'user/email', 'email'
         else:

+ 1 - 4
yt_dlp/extractor/adn.py

@@ -126,10 +126,7 @@ Format: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text'''
             }])
         return subtitles
 
-    def _real_initialize(self):
-        username, password = self._get_login_info()
-        if not username:
-            return
+    def _perform_login(self, username, password):
         try:
             access_token = (self._download_json(
                 self._API_BASE_URL + 'authentication/login', None,

+ 1 - 8
yt_dlp/extractor/afreecatv.py

@@ -184,14 +184,7 @@ class AfreecaTVIE(InfoExtractor):
             video_key['part'] = int(m.group('part'))
         return video_key
 
-    def _real_initialize(self):
-        self._login()
-
-    def _login(self):
-        username, password = self._get_login_info()
-        if username is None:
-            return
-
+    def _perform_login(self, username, password):
         login_form = {
             'szWork': 'login',
             'szType': 'json',

+ 1 - 8
yt_dlp/extractor/alura.py

@@ -74,14 +74,7 @@ class AluraIE(InfoExtractor):
                 "formats": formats
             }
 
-    def _real_initialize(self):
-        self._login()
-
-    def _login(self):
-        username, password = self._get_login_info()
-        if username is None:
-            return
-        pass
+    def _perform_login(self, username, password):
 
         login_page = self._download_webpage(
             self._LOGIN_URL, None, 'Downloading login popup')

+ 14 - 21
yt_dlp/extractor/animelab.py

@@ -15,25 +15,21 @@ from ..compat import compat_HTTPError
 
 
 class AnimeLabBaseIE(InfoExtractor):
-    _LOGIN_REQUIRED = True
     _LOGIN_URL = 'https://www.animelab.com/login'
     _NETRC_MACHINE = 'animelab'
+    _LOGGED_IN = False
 
-    def _login(self):
-        def is_logged_in(login_webpage):
-            return 'Sign In' not in login_webpage
+    def _is_logged_in(self, login_page=None):
+        if not self._LOGGED_IN:
+            if not login_page:
+                login_page = self._download_webpage(self._LOGIN_URL, None, 'Downloading login page')
+            AnimeLabBaseIE._LOGGED_IN = 'Sign In' not in login_page
+        return self._LOGGED_IN
 
-        login_page = self._download_webpage(
-            self._LOGIN_URL, None, 'Downloading login page')
-
-        # Check if already logged in
-        if is_logged_in(login_page):
+    def _perform_login(self, username, password):
+        if self._is_logged_in():
             return
 
-        (username, password) = self._get_login_info()
-        if username is None and self._LOGIN_REQUIRED:
-            self.raise_login_required('Login is required to access any AnimeLab content')
-
         login_form = {
             'email': username,
             'password': password,
@@ -47,17 +43,14 @@ class AnimeLabBaseIE(InfoExtractor):
         except ExtractorError as e:
             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
                 raise ExtractorError('Unable to log in (wrong credentials?)', expected=True)
-            else:
-                raise
+            raise
 
-        # if login was successful
-        if is_logged_in(response):
-            return
-
-        raise ExtractorError('Unable to login (cannot verify if logged in)')
+        if not self._is_logged_in(response):
+            raise ExtractorError('Unable to login (cannot verify if logged in)')
 
     def _real_initialize(self):
-        self._login()
+        if not self._is_logged_in():
+            self.raise_login_required('Login is required to access any AnimeLab content')
 
 
 class AnimeLabIE(AnimeLabBaseIE):

+ 1 - 8
yt_dlp/extractor/animeondemand.py

@@ -53,11 +53,7 @@ class AnimeOnDemandIE(InfoExtractor):
         'only_matching': True,
     }]
 
-    def _login(self):
-        username, password = self._get_login_info()
-        if username is None:
-            return
-
+    def _perform_login(self, username, password):
         login_page = self._download_webpage(
             self._LOGIN_URL, None, 'Downloading login page')
 
@@ -93,9 +89,6 @@ class AnimeOnDemandIE(InfoExtractor):
                 raise ExtractorError('Unable to login: %s' % error, expected=True)
             raise ExtractorError('Unable to log in')
 
-    def _real_initialize(self):
-        self._login()
-
     def _real_extract(self, url):
         anime_id = self._match_id(url)
 

+ 1 - 8
yt_dlp/extractor/atresplayer.py

@@ -37,9 +37,6 @@ class AtresPlayerIE(InfoExtractor):
     ]
     _API_BASE = 'https://api.atresplayer.com/'
 
-    def _real_initialize(self):
-        self._login()
-
     def _handle_error(self, e, code):
         if isinstance(e.cause, compat_HTTPError) and e.cause.code == code:
             error = self._parse_json(e.cause.read(), None)
@@ -48,11 +45,7 @@ class AtresPlayerIE(InfoExtractor):
             raise ExtractorError(error['error_description'], expected=True)
         raise
 
-    def _login(self):
-        username, password = self._get_login_info()
-        if username is None:
-            return
-
+    def _perform_login(self, username, password):
         self._request_webpage(
             self._API_BASE + 'login', None, 'Downloading login page')
 

+ 1 - 8
yt_dlp/extractor/bbc.py

@@ -264,11 +264,7 @@ class BBCCoUkIE(InfoExtractor):
             'only_matching': True,
         }]
 
-    def _login(self):
-        username, password = self._get_login_info()
-        if username is None:
-            return
-
+    def _perform_login(self, username, password):
         login_page = self._download_webpage(
             self._LOGIN_URL, None, 'Downloading signin page')
 
@@ -294,9 +290,6 @@ class BBCCoUkIE(InfoExtractor):
                     'Unable to login: %s' % error, expected=True)
             raise ExtractorError('Unable to log in')
 
-    def _real_initialize(self):
-        self._login()
-
     class MediaSelectionError(Exception):
         def __init__(self, id):
             self.id = id

Some files were not shown because too many files changed in this diff