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

Code before:
        if not next_page.path or not next_page.page:
            return None
        number_section = f'{next_page.page if not next_page.add_one else next_page.page + 1}'
        next_page = f'https://radiocomercial.pt{next_page.path}{number_section}'
        video_id = self._match_id(next_page)
        return self._download_webpage(next_page, video_id, headers={'X-Requested-With': 'XMLHttpRequest'})

    def _collect_hrefs(self, webpage):
        regex = re.compile(r'rounded-site-bottom"><a class="tm-ouvir-podcast" href="([^"]+)"')
        matches = regex.finditer(webpage)
        for match in matches:
            yield f'https://radiocomercial.pt{match.group(1)}'

    def _generate_sorted_entries(self, list_of_podcasts):
        entries = [self._extract_page_content(item) for item in list_of_podcasts]
        sorted_entries = sorted(entries, key=lambda x: x['date'], reverse=True)
        for entry in sorted_entries:
            yield entry

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

        podcast_name = self._html_search_regex(r'<h1>(.+?)</h1>', webpage, 'name')
        podcast_season = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'season')
        podcast_title = podcast_name if podcast_name == podcast_season else f'{podcast_name} - {podcast_season}'

        list_of_podcasts = set()
        while True:
            get_entries = self._collect_hrefs(webpage)
            if get_entries:
                list_of_podcasts.update(get_entries)
            webpage = self._get_next_page(webpage)
            if not webpage:
                break

        return self.playlist_result(self._generate_sorted_entries(list_of_podcasts), podcast, podcast_title)


Review comment (refactor):
```suggestion
        name = try_call(lambda: get_element_text_and_html_by_tag('h1', webpage)[0])
        season = self._html_extract_title(webpage)
        title = name if name == season else join_nonempty(name, season, delim=' - ')
```

Code after:
        'playlist_mincount': 19
    }, {
        'url': 'https://radiocomercial.pt/podcasts/as-minhas-coisas-favoritas',
        'info_dict': {
            'id': 'as-minhas-coisas-favoritas',
            'title': 'As Minhas Coisas Favoritas',
        },
        'playlist_mincount': 131
    }, {
        'url': 'https://radiocomercial.pt/podcasts/tnt-todos-no-top/t2023',
        'info_dict': {
            'id': 'tnt-todos-no-top_t2023',
            'title': 'TNT - Todos No Top - Temporada 2023',
        },
        'playlist_mincount': 39
    }]

    def _entries(self, url, playlist_id):
        for page in itertools.count(1):
            try:
                webpage = self._download_webpage(
                    f'{url}/{page}', playlist_id, f'Downloading page {page}')
            except ExtractorError as e:
                if isinstance(e.cause, HTTPError) and e.cause.status == 404:
                    break
                raise

            episodes = get_elements_html_by_class('tm-ouvir-podcast', webpage)
            if not episodes:
                break
            for url_path in traverse_obj(episodes, (..., {extract_attributes}, 'href')):
                episode_url = urljoin(url, url_path)
                if RadioComercialIE.suitable(episode_url):
                    yield episode_url

    def _real_extract(self, url):
        podcast, season = self._match_valid_url(url).group('id', 'season')
        playlist_id = join_nonempty(podcast, season, delim='_t')
        url = update_url(url, query=None, fragment=None)
        webpage = self._download_webpage(url, playlist_id)

        name = try_call(lambda: get_element_text_and_html_by_tag('h1', webpage)[0])
        title = name if name == season else join_nonempty(name, season, delim=' - Temporada ')

        return self.playlist_from_matches(
            self._entries(url, playlist_id), playlist_id, title, ie=RadioComercialIE)