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

Code before:
        response = self._download_json(
            'https://www.freetv.com/wordpress/wp-admin/admin-ajax.php',
            contentId,
            'Downloading %s video JSON' % contentId,
            data=urlencode_postdata(request_body),
            fatal=False
        )

        if response is False:
            return False

        if response.get('success') is False:
            return False

        response_data = response.get('data')

        if response_data is False:
            return False

        video_metadata = response_data.get('displayMeta')

        video_id = video_metadata.get('contentID')
        title = video_metadata.get('title')
        full_title = video_metadata.get('fullTitle')
        description = video_metadata.get('desc')
        video_url = video_metadata.get('streamURLVideo')

        formats = []
        subtitles = []

        ext = determine_ext(video_url)

        if ext == "m3u8":
            fmts, subs = self._extract_m3u8_formats_and_subtitles(
                video_url, video_id, 'mp4',
                entry_protocol='m3u8_native',
                fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)
        else:
            if self._is_valid_url(video_url, video_id):
                formats.append({
                    'url': video_url,
                })

        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': title,
            'full_title': full_title,

Review comment (refactor):
1. Get rid of variables that are only used once
2. Lack of video id/URL should throw error. So make their extraction fatal

Code after:
            'ext': 'mp4',
        }
    }]

    def _extract_video(self, content_id, action='olyott_video_play'):
        api_response = self._get_api_response(content_id, 'video', {
            'action': action,
            'contentID': content_id,
        })

        video_id, video_url = api_response['displayMeta']['contentID'], api_response['displayMeta']['streamURLVideo']
        formats, subtitles = self._extract_m3u8_formats_and_subtitles(video_url, video_id, 'mp4')
        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': traverse_obj(api_response, ('displayMeta', 'title')),
            'description': traverse_obj(api_response, ('displayMeta', 'desc')),
            'formats': formats,
            'subtitles': subtitles,
        }

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)

        return self._extract_video(
            self._search_regex((
                r'class=["\'][^>]+postid-(?P<video_id>\d+)',
                r'<link[^>]+freetv.com/\?p=(?P<video_id>\d+)',
                r'<div[^>]+data-params=["\'][^>]+post_id=(?P<video_id>\d+)',
            ), webpage, 'video id', group='video_id'))


class FreeTvIE(FreeTvBaseIE):
    IE_NAME = 'freetv:series'
    _VALID_URL = r'https?://(?:www\.)?freetv\.com/series/(?P<id>[^/]+)'
    _TESTS = [{
        'url': 'https://www.freetv.com/series/el-detective-l/',
        'info_dict': {
            'id': 'el-detective-l',
            'title': 'El Detective L',
            'description': 'md5:f9f1143bc33e9856ecbfcbfb97a759be'
        },
        'playlist_count': 24,
    }, {
        'url': 'https://www.freetv.com/series/esmeraldas/',
        'info_dict': {
            'id': 'esmeraldas',
            'title': 'Esmeraldas',
            'description': 'md5:43d7ec45bd931d8268a4f5afaf4c77bf'