From c447fddb171533f84b9761f0c6eebe6444f91952 Mon Sep 17 00:00:00 2001 From: bato3 Date: Sun, 29 Jul 2018 03:22:11 +0200 Subject: [PATCH 1/3] ADN login + response error detect FIXES #17084 --- youtube_dl/extractor/adn.py | 61 ++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/youtube_dl/extractor/adn.py b/youtube_dl/extractor/adn.py index 1eb99c39a..5e1fc9a9c 100644 --- a/youtube_dl/extractor/adn.py +++ b/youtube_dl/extractor/adn.py @@ -23,6 +23,7 @@ from ..utils import ( pkcs1pad, srt_subtitles_timecode, strip_or_none, + urlencode_postdata, urljoin, ) @@ -40,9 +41,59 @@ class ADNIE(InfoExtractor): 'description': 'md5:2f7b5aa76edbc1a7a92cedcda8a528d5', } } - _BASE_URL = 'http://animedigitalnetwork.fr' + _BASE_URL = 'https://animedigitalnetwork.fr' + _LOGIN_URL = 'https://animedigitalnetwork.fr/connexion' + _NETRC_MACHINE = 'ADN' _RSA_KEY = (0xc35ae1e4356b65a73b551493da94b8cb443491c0aa092a357a5aee57ffc14dda85326f42d716e539a34542a0d3f363adf16c5ec222d713d5997194030ee2e4f0d1fb328c01a81cf6868c090d50de8e169c6b13d1675b9eeed1cbc51e1fffca9b38af07f37abd790924cd3bee59d0257cfda4fe5f3f0534877e21ce5821447d1b, 65537) + def _login(self): + username, password = self._get_login_info() + if username is None: + return + login_page = self._download_webpage( + self._LOGIN_URL, None, 'Downloading login page') + + def is_logged(webpage): + return 'task=user.logout' in webpage + + # Already logged in + if is_logged(login_page): + return + + _token = self._search_regex( + r']+type=["\']hidden["\'][^>]+name=["\'](?P[\da-f]+)["\'][^>]+value=["\']1["\']', + login_page, + 'action token', + group='token' + ) + if not _token: + raise ExtractorError('%s Can\'t extract login token' % self.IE_NAME) + + response = self._download_webpage( + self._LOGIN_URL + '?task=user.login', + None, 'Logging in', 'Wrong login info', + data=urlencode_postdata({ + 'username': username, + 'password': password, + 'return': 'aHR0cHM6Ly9hbmltZWRpZ2l0YWxuZXR3b3JrLmZyL2Nvbm5leGlvbg==', # base64 LOGIN URL + _token: '1', + })) + + # Successful login + if is_logged(response): + return + + error = self._html_search_regex( + r'(?s)]+class=["\']error message["\'][^>]*>(.+?)', + response, 'error message', default=None) + if error: + raise ExtractorError('Unable to login: %s' % error, expected=True) + + raise ExtractorError('Unable to log in') + + def _real_initialize(self): + self._login() + def _get_subtitles(self, sub_path, video_id): if not sub_path: return None @@ -132,6 +183,14 @@ class ADNIE(InfoExtractor): urljoin(self._BASE_URL, links_url), video_id, headers={ 'Authorization': 'Bearer ' + authorization, }) + if links_data.get('status'): + if links_data.get('code') == 1: + self.raise_login_required(links_data.get('error')) + if links_data.get('code') == 0: + if links_data.get('error').find('Pour des raisons l') != -1: + self.raise_geo_restricted(links_data.get('error')) + else: + raise ExtractorError('%s said: %s' % (self.IE_NAME, links_data.get('error')), expected=True) links = links_data.get('links') or {} metas = metas or links_data.get('meta') or {} sub_path = (sub_path or links_data.get('subtitles')) + '&token=' + token From b50478675de2ef52fab925e3ca8768ecf6cfbc7e Mon Sep 17 00:00:00 2001 From: bato3 Date: Mon, 30 Jul 2018 11:44:18 +0200 Subject: [PATCH 2/3] Change token extraction to `_form_hidden_inputs` --- youtube_dl/extractor/adn.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/youtube_dl/extractor/adn.py b/youtube_dl/extractor/adn.py index 5e1fc9a9c..568e1d128 100644 --- a/youtube_dl/extractor/adn.py +++ b/youtube_dl/extractor/adn.py @@ -54,30 +54,26 @@ class ADNIE(InfoExtractor): self._LOGIN_URL, None, 'Downloading login page') def is_logged(webpage): - return 'task=user.logout' in webpage + return 'task=user.logout' in webpage or 'view=logout' in webpage # Already logged in if is_logged(login_page): return - _token = self._search_regex( - r']+type=["\']hidden["\'][^>]+name=["\'](?P[\da-f]+)["\'][^>]+value=["\']1["\']', - login_page, - 'action token', - group='token' - ) - if not _token: + form_data = self._form_hidden_inputs('login-form', login_page) + if not form_data.get('return'): raise ExtractorError('%s Can\'t extract login token' % self.IE_NAME) + form_data.update({ + 'username': username, + 'password': password, + # base64 LOGIN URL + # 'return': 'aHR0cHM6Ly9hbmltZWRpZ2l0YWxuZXR3b3JrLmZyL2Nvbm5leGlvbg==', + }) response = self._download_webpage( self._LOGIN_URL + '?task=user.login', None, 'Logging in', 'Wrong login info', - data=urlencode_postdata({ - 'username': username, - 'password': password, - 'return': 'aHR0cHM6Ly9hbmltZWRpZ2l0YWxuZXR3b3JrLmZyL2Nvbm5leGlvbg==', # base64 LOGIN URL - _token: '1', - })) + data=urlencode_postdata(form_data)) # Successful login if is_logged(response): From 7475f05442463ef830b86215da8181b61af2e8c9 Mon Sep 17 00:00:00 2001 From: bato3 Date: Mon, 30 Jul 2018 17:44:45 +0200 Subject: [PATCH 3/3] after 2nd review --- youtube_dl/extractor/adn.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/youtube_dl/extractor/adn.py b/youtube_dl/extractor/adn.py index 568e1d128..c3e3c7294 100644 --- a/youtube_dl/extractor/adn.py +++ b/youtube_dl/extractor/adn.py @@ -61,13 +61,9 @@ class ADNIE(InfoExtractor): return form_data = self._form_hidden_inputs('login-form', login_page) - if not form_data.get('return'): - raise ExtractorError('%s Can\'t extract login token' % self.IE_NAME) form_data.update({ 'username': username, 'password': password, - # base64 LOGIN URL - # 'return': 'aHR0cHM6Ly9hbmltZWRpZ2l0YWxuZXR3b3JrLmZyL2Nvbm5leGlvbg==', }) response = self._download_webpage( @@ -179,14 +175,15 @@ class ADNIE(InfoExtractor): urljoin(self._BASE_URL, links_url), video_id, headers={ 'Authorization': 'Bearer ' + authorization, }) - if links_data.get('status'): + error = links_data.get('error') + if error: if links_data.get('code') == 1: - self.raise_login_required(links_data.get('error')) + self.raise_login_required(error) if links_data.get('code') == 0: - if links_data.get('error').find('Pour des raisons l') != -1: - self.raise_geo_restricted(links_data.get('error')) + if 'Pour des raisons l' in error: + self.raise_geo_restricted(error) else: - raise ExtractorError('%s said: %s' % (self.IE_NAME, links_data.get('error')), expected=True) + raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True) links = links_data.get('links') or {} metas = metas or links_data.get('meta') or {} sub_path = (sub_path or links_data.get('subtitles')) + '&token=' + token