Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/viu.py (Python)

Code before:

        duration_limit = False
        query = {
            'ccs_product_id': video_data['ccs_product_id'],
            'language_flag_id': self._LANGUAGE_FLAG.get(lang_code.lower()) or '3',
        }

        def download_playback():
            stream_data = self._download_json(
                'https://api-gateway-global.viu.com/api/playback/distribute',
                video_id=video_id,
                note='Downloading stream info',
                query=query,
                headers={
                    'Authorization': f'Bearer {self.auth_codes[country_code]}',
                    'Referer': url,
                    'Origin': url
                })
            return self._detect_error(stream_data).get('stream')

        try:
            stream_data = download_playback()
        except (ExtractorError, KeyError):
            stream_data = None
            if video_data.get('user_level', 0) > 0:
                user = self._login(self._country_code, video_id)
                if user:
                    query['identity'] = user['identity']
                    stream_data = download_playback()
                else:
                    # preview is limited to 3min for non-members
                    # try to bypass the duration limit
                    duration_limit = True
                    query['duration'] = '180'
                    try:
                        stream_data = download_playback()
                    except (ExtractorError, KeyError):  # if still not working, give up
                        self._raise_login_required()

        if not stream_data:
            raise ExtractorError('Cannot get stream info', expected=True)

        stream_sizes = stream_data.get('size', {})
        formats = []
        for vid_format, stream_url in stream_data.get('url', {}).items():
            height = int_or_none(self._search_regex(
                r's(\d+)p', vid_format, 'height', default=None))

            # bypass preview duration limit
            if duration_limit:
                stream_url = compat_urlparse.urlparse(stream_url)

Review comment (suggestion):
mistake?
```suggestion
                user = self._login(country_code, video_id)
```

Code after:
                'https://api-gateway-global.viu.com/api/playback/distribute',
                video_id=video_id, query=query, fatal=False, note='Downloading stream info',
                headers={
                    'Authorization': f'Bearer {self._auth_codes[country_code]}',
                    'Referer': url,
                    'Origin': url
                })
            return self._detect_error(stream_data).get('stream')

        if not self._auth_codes.get(country_code):
            self._auth_codes[country_code] = self._get_token(country_code, video_id)

        stream_data = None
        try:
            stream_data = download_playback()
        except (ExtractorError, KeyError):
            token = self._login(country_code, video_id)
            if token is not None:
                query['identity'] = token
            else:
                # preview is limited to 3min for non-members. But we can try to bypass it
                duration_limit, query['duration'] = True, '180'
            try:
                stream_data = download_playback()
            except (ExtractorError, KeyError):
                if token is not None:
                    raise
                self.raise_login_required(method='password')
        if not stream_data:
            raise ExtractorError('Cannot get stream info', expected=True)

        formats = []
        for vid_format, stream_url in (stream_data.get('url') or {}).items():
            height = int(self._search_regex(r's(\d+)p', vid_format, 'height', default=None))

            # bypass preview duration limit
            if duration_limit:
                stream_url = urllib.parse.urlparse(stream_url)
                query.update({
                    'duration': video_data.get('time_duration') or '9999999',
                    'duration_start': '0',
                })
                stream_url = stream_url._replace(query=urllib.parse.urlencode(dict(
                    urllib.parse.parse_qsl(stream_url.query, keep_blank_values=True)))).geturl()

            formats.append({
                'format_id': vid_format,
                'url': stream_url,
                'height': height,
                'ext': 'mp4',
                'filesize': try_get(stream_data, lambda x: x['size'][vid_format], int)