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

Code before:
    BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
    _PATH_MAP = {
        'episodes': 'episode',
    }

    def _real_extract(self, url):
        path, video_id = self._match_valid_url(url).groups()
        if path == 'lp':
            webpage = self._download_webpage(url, video_id)
            redirect_path = self._search_regex(r'to_href="([^"]+)', webpage, 'redirect path')
            path, video_id = self._match_valid_url(f'https://tver.jp{redirect_path}').groups()
        api_response = self._download_json(
            f'https://statics.tver.jp/content/{self._PATH_MAP.get(path, path)}/{video_id}.json', video_id,
            query={'v': '5'}, headers={
                'Origin': 'https://tver.jp',
                'Referer': 'https://tver.jp/',
            })
        p_id = traverse_obj(api_response, ('video', 'accountID'))
        r_id = 'ref:' + traverse_obj(api_response, ('video', 'videoRefID'))
        bc_url = smuggle_url(
            self.BRIGHTCOVE_URL_TEMPLATE % (p_id, r_id),
            {'geo_countries': ['JP']})

        return {
            '_type': 'url_transparent',
            'description': traverse_obj(api_response, 'description', expected_type=compat_str),
            'episode_number': int_or_none(traverse_obj(api_response, ('main', 'ext', 'episode_number'), expected_type=compat_str)),
            'url': bc_url,
            'ie_key': 'BrightcoveNew',
        }


Review comment (bug):
As new l.40 didn't crash (`p_id`, `r_id` are both strings), `api_response` is a valid `dict` and this could be just `str_or_none(api_response.get('description'))`.

Code after:
        },
        'add_ie': ['BrightcoveNew'],
    }, {
        'skip': 'videos are only available for 7 days',
        'url': 'https://tver.jp/lp/episodes/ep6f16g26p',
        'info_dict': {
            # sorry but this is "correct"
            'title': '4月11日(月)23時06分 ~ 放送予定',
            'description': 'md5:4029cc5f4b1e8090dfc5b7bd2bc5cd0b',
        },
        'add_ie': ['BrightcoveNew'],
    }, {
        'url': 'https://tver.jp/corner/f0103888',
        'only_matching': True,
    }, {
        'url': 'https://tver.jp/lp/f0033031',
        'only_matching': True,
    }]
    BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
    _PLATFORM_UID = None
    _PLATFORM_TOKEN = None

    def _real_initialize(self):
        create_response = self._download_json(
            'https://platform-api.tver.jp/v2/api/platform_users/browser/create', None,
            note='Creating session', data=b'device_type=pc', headers={
                'Origin': 'https://s.tver.jp',
                'Referer': 'https://s.tver.jp/',
                'Content-Type': 'application/x-www-form-urlencoded',
            })
        self._PLATFORM_UID = traverse_obj(create_response, ('result', 'platform_uid'))
        self._PLATFORM_TOKEN = traverse_obj(create_response, ('result', 'platform_token'))

    def _real_extract(self, url):
        video_id, video_type = self._match_valid_url(url).group('id', 'type')
        if video_type not in {'series', 'episodes'}:
            webpage = self._download_webpage(url, video_id, note='Resolving to new URL')
            video_id = self._match_id(self._search_regex(
                (r'canonical"\s*href="(https?://tver\.jp/[^"]+)"', r'&link=(https?://tver\.jp/[^?&]+)[?&]'),
                webpage, 'url regex'))
        video_info = self._download_json(
            f'https://statics.tver.jp/content/episode/{video_id}.json', video_id,
            query={'v': '5'}, headers={
                'Origin': 'https://tver.jp',
                'Referer': 'https://tver.jp/',
            })
        p_id = video_info['video']['accountID']
        r_id = traverse_obj(video_info, ('video', ('videoRefID', 'videoID')), get_all=False)
        if not r_id:
            raise ExtractorError('Failed to extract reference ID for Brightcove')
        if not r_id.isdigit():