1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-11-25 19:52:11 +00:00

[rutube] Better login and error handling, code style fixes

This commit is contained in:
Alexander Nartov 2020-09-21 03:17:02 +10:00
parent 90e16aa42c
commit f91625cd12

View file

@ -18,7 +18,7 @@ from ..utils import (
try_get, try_get,
unified_timestamp, unified_timestamp,
url_or_none, url_or_none,
) ExtractorError)
class RutubeBaseIE(InfoExtractor): class RutubeBaseIE(InfoExtractor):
@ -61,6 +61,39 @@ class RutubeBaseIE(InfoExtractor):
'is_club': bool_or_none(video.get('is_club')), 'is_club': bool_or_none(video.get('is_club')),
} }
def _real_initialize(self):
self._login()
def _login(self):
username, password = self._get_login_info()
if username is None:
return
login = self._download_json(
'https://pass.rutube.ru/api/accounts/phone/login/', None,
'Logging in', 'Unable to log in',
data=json.dumps({
'phone': username,
'password': password,
}).encode(),
headers={
'Content-Type': 'application/json',
'Accept': 'application/json'
},
expected_status=400
)
if not login.get('success'):
msg = login.get('message')
raise ExtractorError(
'Unable to log in. %s said: %s' % (self.IE_NAME, msg),
expected=True
)
self._download_webpage(
'https://rutube.ru/social/auth/rupass/?callback_path=/social/login/rupass/',
None, False
)
def _download_and_extract_info(self, video_id, query=None): def _download_and_extract_info(self, video_id, query=None):
return self._extract_info( return self._extract_info(
self._download_api_info(video_id, query=query), video_id) self._download_api_info(video_id, query=query), video_id)
@ -148,33 +181,26 @@ class RutubeIE(RutubeBaseIE):
query = {} query = {}
if info['is_club']: if info['is_club']:
username, password = self._get_login_info() visitor_json, urlh = self._download_webpage_handle(
if username is None or password is None: 'https://rutube.ru/api/accounts/visitor/', video_id,
'Downloading visitor JSON', 'Unable to download visitor JSON'
)
if not visitor_json:
self.raise_login_required() self.raise_login_required()
login = self._download_json( visitor = self._parse_json(visitor_json, video_id)
'https://pass.rutube.ru/api/accounts/phone/login/', video_id, data=json.dumps({ club_params = visitor['club_params_encrypted']
'phone': username,
'password': password,
}).encode(),
headers={'Content-Type': 'application/json', 'Accept': 'application/json'}
)
if not login['success']:
self.raise_login_required('Invalid login or password')
self._download_webpage(
'https://rutube.ru/social/auth/rupass/?callback_path=/social/login/rupass/', video_id)
visitor = self._download_json(
'https://rutube.ru/api/accounts/visitor/', video_id, 'Downloading visitor JSON',
'Unable to download visitor JSON')
clubParams = visitor['club_params_encrypted']
ad = self._download_json( ad = self._download_json(
'https://mtr.rutube.ru/api/v3/interactive?' + clubParams + '&video_id=' + video_id, video_id, 'https://mtr.rutube.ru/api/v3/interactive?%s&video_id=%s' %
'Downloading AD JSON', 'Unable to download AD JSON') (club_params, video_id),
clubToken = ad['award'] video_id, 'Downloading AD JSON', 'Unable to download AD JSON'
query['club_token'] = clubToken )
query['no_404'] = 'true' club_token = ad['award']
query.update({
'club_token': club_token,
'no_404': 'true',
})
info['formats'] = self._download_and_extract_formats(video_id, query) info['formats'] = self._download_and_extract_formats(video_id, query)
return info return info