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

Code before:

        title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'Title',
                                        fatal=False) or self._og_search_title(webpage)

        entryid = self._search_regex(r'data-entry-id=["\']([^"\']+)', webpage, 'entryID')

        session_json = self._download_session_json(video_id, entryid)

        k_session = session_json.get('KSession')
        if k_session is None:

            # retry with fresh tokens
            self.token_util(video_id, {
                'grant_type': 'refresh_token',
                'refresh_token': self.REFRESH_TOKEN,
                'client_id': 'tennis-tv-web'
            })
            k_session = self._download_session_json(video_id, entryid).get('KSession')

            if k_session is None:
                raise ExtractorError('Failed to get KSession, possibly a premium video', expected=True)

        if session_json.get('ErrorMessage'):
            self.to_screen(session_json['ErrorMessage'])

        formats, subtitles = self._extract_m3u8_formats_and_subtitles(f'https://open.http.mp.streamamg.com/p/{self.PARTNER_ID}/sp/{self.PARTNER_ID}00/playManifest/entryId/{entryid}/format/applehttp/protocol/https/a.m3u8?ks={k_session}', video_id)

        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': title,
            'description': self._html_search_regex(r'<span itemprop="description" content=["\'](.+?)["\']>', webpage, 'description', fatal=False) or self._og_search_description(webpage),
            'formats': formats,
            'thumbnail': f'https://open.http.mp.streamamg.com/p/{self.PARTNER_ID}/sp/{self.PARTNER_ID}00/thumbnail/entry_id/{entryid}/version/100001/height/1920',
            'timestamp': unified_timestamp(self._html_search_regex(r'<span itemprop="description" content=["\'](.+?)["\']>', webpage, 'upload time')),
            'subtitles': subtitles,
        }


Review comment (refactor):
too long. the URL can be seperated out
```py
        formats, subtitles = self._extract_m3u8_formats_and_subtitles(
            self._FORMAT_URL.format(partner=self.PARTNER_ID, entry=entryid, session=k_session), video_id)
```

Code after:
        webpage = self._download_webpage(url, video_id)

        entryid = self._search_regex(r'data-entry-id=["\']([^"\']+)', webpage, 'entryID')
        session_json = self._download_session_json(video_id, entryid)

        k_session = session_json.get('KSession')
        if k_session is None:
            self.get_token(video_id, {
                'grant_type': 'refresh_token',
                'refresh_token': self.refresh_token,
                'client_id': 'tennis-tv-web'
            })
            k_session = self._download_session_json(video_id, entryid).get('KSession')
            if k_session is None:
                raise ExtractorError('Failed to get KSession, possibly a premium video', expected=True)

        if session_json.get('ErrorMessage'):
            self.report_warning(session_json['ErrorMessage'])

        formats, subtitles = self._extract_m3u8_formats_and_subtitles(
            self._FORMAT_URL.format(partner=self._PARTNER_ID, entry=entryid, session=k_session), video_id)

        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': self._html_extract_title(webpage) or self._og_search_title(webpage),
            'description': self._html_search_regex(
                (r'<span itemprop="description" content=["\']([^"\']+)["\']>', *self._og_regexes('description')),
                webpage, 'description', fatal=False),
            'thumbnail': f'https://open.http.mp.streamamg.com/p/{self._PARTNER_ID}/sp/{self._PARTNER_ID}00/thumbnail/entry_id/{entryid}/version/100001/height/1920',
            'timestamp': unified_timestamp(self._html_search_regex(
                r'<span itemprop="description" content=["\']([^"\']+)["\']>', webpage, 'upload time')),
            'series': self._html_search_regex(r'data-series\s*?=\s*?"(.*?)"', webpage, 'series', fatal=False) or None,
            'season': self._html_search_regex(r'data-tournament-city\s*?=\s*?"(.*?)"', webpage, 'season', fatal=False) or None,
            'episode': self._html_search_regex(r'data-round\s*?=\s*?"(.*?)"', webpage, 'round', fatal=False) or None,
            'formats': formats,
            'subtitles': subtitles,
        }