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

Code before:
                'chapter': 'Neklid #4',
                'chapter_number': 4,
            },
        }, {
            'md5': '6e729fa39b647325b868d419c76f3efa',
            'info_dict': {
                'id': '9890734',
                'ext': 'mp3',
                'title': 'Neklid #5',
                'description': '5. díl: Neklid: 5. díl',
                'duration': 908,
                'artist': 'Josef Kokta',
                'channel_id': 'radio-wave',
                'chapter': 'Neklid #5',
                'chapter_number': 5,
            },
        }]
    }]

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

        player_div = re.findall("<div class=\"mujRozhlasPlayer\" data-player='.*'>", webpage)[0]  # Use utils.get_element_text_and_html_by_tag() instead when it accepts less strict html.

        data = self._parse_json(extract_attributes(player_div).get('data-player'), video_id).get('data')

        entries = []
        for entry in data.get('playlist'):
            formats = []
            for audio_link in entry.get('audioLinks'):
                formats.append({
                    'url': audio_link.get('url'),
                    'ext': audio_link.get('variant'),
                    'format_id': audio_link.get('variant'),
                    'abr': audio_link.get('bitrate'),
                    'acodec': audio_link.get('variant'),
                    'vcodec': 'none',
                })
            entries.append({
                'id': traverse_obj(entry, ('meta', 'ga', 'contentId')),
                'title': traverse_obj(entry, ('meta', 'ga', 'contentName')),
                'description': entry.get('title'),
                'duration': entry.get('duration'),
                'formats': formats,
                'artist': traverse_obj(entry, ('meta', 'ga', 'contentAuthor')),
                'channel_id': traverse_obj(entry, ('meta', 'ga', 'contentCreator')),
                'chapter': traverse_obj(entry, ('meta', 'ga', 'contentNameShort')) if traverse_obj(entry, ('meta', 'ga', 'contentSerialPart')) is not None else None,
                'chapter_number': int(traverse_obj(entry, ('meta', 'ga', 'contentSerialPart'))) if traverse_obj(entry, ('meta', 'ga', 'contentSerialPart')) is not None else None,
            })


Review comment (refactor):
```suggestion
        # FIXME: Use get_element_text_and_html_by_tag when it accepts less strict html
        player_div = self._parse_json(extract_attributes(self._search_regex(
            '<div class="mujRozhlasPlayer" data-player=\'[^>]+\'>',
            webpage, 'player'))['data-player'], video_id)['data']
```

Code after:
                'ext': 'mp3',
                'title': 'Neklid #5',
                'description': '5. díl: Neklid: 5. díl',
                'duration': 908,
                'artist': 'Josef Kokta',
                'channel_id': 'radio-wave',
                'chapter': 'Neklid #5',
                'chapter_number': 5,
            },
        }]
    }]

    def _extract_video(self, entry):
        chapter_number = int_or_none(traverse_obj(entry, ('meta', 'ga', 'contentSerialPart')))
        return {
            'id': entry['meta']['ga']['contentId'],
            'title': traverse_obj(entry, ('meta', 'ga', 'contentName')),
            'description': entry.get('title'),
            'duration': entry.get('duration'),
            'artist': traverse_obj(entry, ('meta', 'ga', 'contentAuthor')),
            'channel_id': traverse_obj(entry, ('meta', 'ga', 'contentCreator')),
            'chapter': traverse_obj(entry, ('meta', 'ga', 'contentNameShort')) if chapter_number else None,
            'chapter_number': chapter_number,
            'formats': [{
                'url': audio_link['url'],
                'ext': audio_link.get('variant'),
                'format_id': audio_link.get('variant'),
                'abr': audio_link.get('bitrate'),
                'acodec': audio_link.get('variant'),
                'vcodec': 'none',
            } for audio_link in entry['audioLinks']],
        }

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

        # FIXME: Use get_element_text_and_html_by_tag when it accepts less strict html
        data = self._parse_json(extract_attributes(self._search_regex(
            r'(<div class="mujRozhlasPlayer" data-player=\'[^\']+\'>)',
            webpage, 'player'))['data-player'], video_id)['data']

        return {
            '_type': 'playlist',
            'id': data.get('embedId'),
            'title': traverse_obj(data, ('series', 'title')),
            'entries': map(self._extract_video, data['playlist']),
        }