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

Code before:
            'platform': self._PLATFORM,
            # For VQQ
            'guid': guid,
            'flowid': ''.join(random.choices(string.digits + string.ascii_lowercase, k=32)),
        }

        return self._search_json(r'QZOutputJson=', self._download_webpage(
            self._API_URL, video_id, query=query), 'api_response', video_id)

    def _extract_video_formats_and_subtitles(self, api_response, video_id):
        video_response = api_response['vl']['vi'][0]
        video_width, video_height = video_response.get('vw'), video_response.get('vh')

        formats, subtitles = [], {}
        for video_format in video_response['ul']['ui']:
            if video_format.get('hls') or determine_ext(video_format['url']) == 'm3u8':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(
                    video_format['url'] + traverse_obj(video_format, ('hls', 'pt'), default=''),
                    video_id, 'mp4', fatal=False)
                for f in fmts:
                    f.update({'width': video_width, 'height': video_height})

                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
            else:
                formats.append({
                    'url': f'{video_format["url"]}{video_response["fn"]}?vkey={video_response["fvkey"]}',
                    'width': video_width,
                    'height': video_height,
                    'ext': 'mp4',
                })

        return formats, subtitles

    def _extract_video_native_subtitles(self, api_response, subtitles_format):
        subtitles = {}
        for subtitle in traverse_obj(api_response, ('sfl', 'fi')) or ():
            subtitles.setdefault(subtitle['lang'].lower(), []).append({
                'url': subtitle['url'],
                'ext': subtitles_format,
                'protocol': 'm3u8_native' if determine_ext(subtitle['url']) == 'm3u8' else 'http',
            })

        return subtitles

    def _extract_all_video_formats_and_subtitles(self, url, video_id, series_id):
        formats, subtitles = [], {}
        for video_format, subtitle_format, video_quality in (
                # '': 480p, 'shd': 720p, 'fhd': 1080p
                ('mp4', 'srt', ''), ('hls', 'vtt', 'shd'), ('hls', 'vtt', 'fhd')):
            api_response = self._get_video_api_response(

Review comment (refactor):
How about something like this for this function?
```py
    def _extract_video_formats_and_subtitles(self, api_response, video_id):
        video_response = api_response['vl']['vi'][0]

        formats, subtitles = [], {}
        for video_format in video_response['ul']['ui']:
            if video_format.get('hls') or determine_ext(video_format['url']) == 'm3u8':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(
                    video_format['url'] + traverse_obj(video_format, ('hls', 'pt'), default=''),
                    video_id, 'mp4', fatal=False)

                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
            else:
                formats.append({
                    'url': f'{video_format["url"]}{video_response["fn"]}?vkey={video_response["fvkey"]}',
                    'ext': 'mp4',
                })

        identifier = video_response.get('br')
        format_response = traverse_obj(
            api_response, ('fl', 'fi', lambda _, v: v['br'] == identifier),
            expected_type=dict, get_all=False) or {}
        has_drm = video_response.get('drm') == 1
        is_hdr = format_response.get('name') == 'hdr10'
        format_name = f'{format_response.get("sname")} ({format_response.get("resolution")})'
        common_info = {
            'width': video_response.get('vw'),
            'height': video_response.get('vh'),
            'abr': float_or_none(format_response.get('audiobandwidth'), scale=1000),
            'vbr': float_or_none(format_response.get('bandwidth'), scale=1000),
            'fps': format_response.get('vfps'),
            'format': format_name,
            'format_id ': format_response.get('formatdefn'),
            'format_note': f'{format_name} HDR' if is_hdr else None,
            'dynamic_range': 'hdr10' if is_hdr else None,
            'has_drm': has_drm,
        }
        for f in formats:
            f.update(common_info)

        return formats, subtitles
```
It seems to pass the tests. I believe this to ba a lot more readable though.

As a side note, why was the property called `has_drm ` with a space? I have removed the space. The tests would need to be updated accordingly.

Code after:
            'dtype': '3' if video_format == 'hls' else '0',
            'defn': video_quality,
            'spsrt': '2',  # Enable subtitles
            'sphttps': '1',  # Enable HTTPS
            'otype': 'json',
            'spwm': '1',
            'hevclv': '28',  # Enable HEVC
            'drm': '40',  # Enable DRM
            # For HDR
            'spvideo': '4',
            'spsfrhdr': '100',
            # For SHD
            'host': self._HOST,
            'referer': self._REFERER,
            'ehost': video_url,
            'appVer': self._APP_VERSION,
            'platform': self._PLATFORM,
            # For VQQ
            'guid': guid,
            'flowid': ''.join(random.choices(string.digits + string.ascii_lowercase, k=32)),
        }

        return self._search_json(r'QZOutputJson=', self._download_webpage(
            self._API_URL, video_id, query=query), 'api_response', video_id)

    def _extract_video_formats_and_subtitles(self, api_response, video_id):
        video_response = api_response['vl']['vi'][0]

        formats, subtitles = [], {}
        for video_format in video_response['ul']['ui']:
            if video_format.get('hls') or determine_ext(video_format['url']) == 'm3u8':
                fmts, subs = self._extract_m3u8_formats_and_subtitles(
                    video_format['url'] + traverse_obj(video_format, ('hls', 'pt'), default=''),
                    video_id, 'mp4', fatal=False)

                formats.extend(fmts)
                self._merge_subtitles(subs, target=subtitles)
            else:
                formats.append({
                    'url': f'{video_format["url"]}{video_response["fn"]}?vkey={video_response["fvkey"]}',
                    'ext': 'mp4',
                })

        identifier = video_response.get('br')
        format_response = traverse_obj(
            api_response, ('fl', 'fi', lambda _, v: v['br'] == identifier),
            expected_type=dict, get_all=False) or {}
        common_info = {
            'width': video_response.get('vw'),
            'height': video_response.get('vh'),
            'abr': float_or_none(format_response.get('audiobandwidth'), scale=1000),