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

Code before:
        video_id = self._match_id(url)

        data = self._download_json(
            f'https://odkmedia.io/odx/api/v3/playback/{video_id}/', video_id, fatal=False,
            headers={'service-name': 'odk'}, query={'did': str(uuid.uuid4())}, expected_status=(403, 404))
        if not traverse_obj(data, ('result', {dict})):
            msg = traverse_obj(data, ('messages', '__default'), 'title', expected_type=str)
            raise ExtractorError(msg or 'Got empty response from playback API', expected=True)

        data = data['result']

        def try_geo_bypass(url):
            return traverse_obj(url, ({parse_qs}, 'stream_url', 0, {url_or_none})) or url

        def try_upgrade_quality(url):
            mod_url = re.sub(r'_720(p?)\.m3u8', r'_1080\1.m3u8', url)
            return mod_url if mod_url != url and self._request_webpage(
                HEADRequest(mod_url), video_id, note='Checking for higher quality format',
                errnote='No higher quality format found', fatal=False) else url

        potential_urls = traverse_obj(
            data, (('sources', 'manifest'), ..., 'url', {url_or_none}, {try_geo_bypass}, {try_upgrade_quality}))

        formats = []
        for url in potential_urls:
            formats.extend(self._extract_m3u8_formats(url, video_id, fatal=False))

        subtitles = {}
        for track in traverse_obj(data, ('text_tracks', lambda _, v: url_or_none(v['url']))):
            subtitles.setdefault(track.get('language', 'und'), []).append({
                'url': track['url'],
                'ext': track.get('codec'),
                'name': track.get('label'),
            })

        def if_series(key=None):
            return lambda obj: obj[key] if key and obj['kind'] == 'series' else None

        return {
            'id': video_id,
            'title': join_nonempty(
                ('episode', 'program', 'title'),
                ('episode', 'title'), from_dict=data, delim=': '),
            **traverse_obj(data, {
                'thumbnail': ('episode', 'images', 'thumbnail', {url_or_none}),
                'release_date': ('episode', 'release_date', {lambda x: x.replace('-', '')}, {unified_strdate}),
                'duration': ('duration', {functools.partial(float_or_none, scale=1000)}),
                'age_limit': ('age_rating', 'name', {lambda x: x.replace('R', '')}, {parse_age_limit}),
                'series': ('episode', {if_series(key='program')}, 'title'),
                'series_id': ('episode', {if_series(key='program')}, 'id'),
                'episode': ('episode', {if_series(key='title')}),

Review comment (refactor):
On second look, I think it'd be better to keep the network requests out of the traversal:
```suggestion
        formats = []
        for m3u8_url in traverse_obj(data, (('sources', 'manifest'), ..., 'url', {url_or_none}, {try_geo_bypass}):
            formats.extend(self._extract_m3u8_formats(try_upgrade_quality(m3u8_url), video_id, fatal=False))
```

Code after:
        video_id = self._match_id(url)

        data = self._download_json(
            f'https://odkmedia.io/odx/api/v3/playback/{video_id}/', video_id, fatal=False,
            headers={'service-name': 'odk'}, query={'did': str(uuid.uuid4())}, expected_status=(403, 404))
        if not traverse_obj(data, ('result', {dict})):
            msg = traverse_obj(data, ('messages', '__default'), 'title', expected_type=str)
            raise ExtractorError(msg or 'Got empty response from playback API', expected=True)

        data = data['result']

        def try_geo_bypass(url):
            return traverse_obj(url, ({parse_qs}, 'stream_url', 0, {url_or_none})) or url

        def try_upgrade_quality(url):
            mod_url = re.sub(r'_720(p?)\.m3u8', r'_1080\1.m3u8', url)
            return mod_url if mod_url != url and self._request_webpage(
                HEADRequest(mod_url), video_id, note='Checking for higher quality format',
                errnote='No higher quality format found', fatal=False) else url

        formats = []
        for m3u8_url in traverse_obj(data, (('sources', 'manifest'), ..., 'url', {url_or_none}, {try_geo_bypass})):
            formats.extend(self._extract_m3u8_formats(try_upgrade_quality(m3u8_url), video_id, fatal=False))

        subtitles = {}
        for track in traverse_obj(data, ('text_tracks', lambda _, v: url_or_none(v['url']))):
            subtitles.setdefault(track.get('language', 'und'), []).append({
                'url': track['url'],
                'ext': track.get('codec'),
                'name': track.get('label'),
            })

        def if_series(key=None):
            return lambda obj: obj[key] if key and obj['kind'] == 'series' else None

        return {
            'id': video_id,
            'title': join_nonempty(
                ('episode', 'program', 'title'),
                ('episode', 'title'), from_dict=data, delim=': '),
            **traverse_obj(data, {
                'thumbnail': ('episode', 'images', 'thumbnail', {url_or_none}),
                'release_date': ('episode', 'release_date', {lambda x: x.replace('-', '')}, {unified_strdate}),
                'duration': ('duration', {functools.partial(float_or_none, scale=1000)}),
                'age_limit': ('age_rating', 'name', {lambda x: x.replace('R', '')}, {parse_age_limit}),
                'series': ('episode', {if_series(key='program')}, 'title'),
                'series_id': ('episode', {if_series(key='program')}, 'id'),
                'episode': ('episode', {if_series(key='title')}),
                'episode_number': ('episode', {if_series(key='number')}, {int_or_none}),
            }, get_all=False),
            'formats': formats,