From 2290860e1f2d66f8c2ff373c1f7148c65c017077 Mon Sep 17 00:00:00 2001 From: lkho Date: Fri, 23 Oct 2020 23:05:11 +0800 Subject: [PATCH] [viu:ott] optimize try and error logic --- youtube_dl/extractor/viu.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/youtube_dl/extractor/viu.py b/youtube_dl/extractor/viu.py index 5e70ff128..21f7973cb 100644 --- a/youtube_dl/extractor/viu.py +++ b/youtube_dl/extractor/viu.py @@ -243,8 +243,7 @@ class ViuOTTIE(InfoExtractor): 'password': password, 'platform_flag_label': 'web', }).encode()) - data = self._detect_error(data) - self._user_info = data['user'] + self._user_info = self._detect_error(data)['user'] return self._user_info @@ -278,37 +277,34 @@ class ViuOTTIE(InfoExtractor): 'Referer': re.search(r'https?://[^/]+', url).group(0), 'Origin': re.search(r'https?://[^/]+', url).group(0), } - try: - stream_data = self._download_json( + + def _try_download_stream_data(): + temp1 = self._download_json( 'https://d1k2us671qcoau.cloudfront.net/distribute_web_%s.php' % country_code, video_id, 'Downloading stream info', query=query, headers=headers) - stream_data = self._detect_error(stream_data)['stream'] + return self._detect_error(temp1).get('stream') + + stream_data = None + try: + stream_data = _try_download_stream_data() except (ExtractorError, KeyError): - stream_data = None if video_data.get('user_level', 0) > 0: user = self._login(country_code, video_id) if user: query['identity'] = user['identity'] - stream_data = self._download_json( - 'https://d1k2us671qcoau.cloudfront.net/distribute_web_%s.php' % country_code, - video_id, 'Downloading stream info', query=query, headers=headers) - stream_data = self._detect_error(stream_data).get('stream') else: # preview is limited to 3min for non-members # try to bypass the duration limit duration_limit = True query['duration'] = '180' - stream_data = self._download_json( - 'https://d1k2us671qcoau.cloudfront.net/distribute_web_%s.php' % country_code, - video_id, 'Downloading stream info', query=query, headers=headers) - try: - stream_data = self._detect_error(stream_data)['stream'] - except (ExtractorError, KeyError): - # if still not working, give up - self._raise_login_required() + stream_data = _try_download_stream_data() if not stream_data: - raise ExtractorError('Cannot get stream info', expected=True) + if duration_limit: + # raise login required message if we have tried with the duration hack + self._raise_login_required() + else: + raise ExtractorError('Cannot get stream info', expected=True) stream_sizes = stream_data.get('size', {}) formats = []