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

Code before:
        'info_dict': {
            'id': 'convenca-me-num-minuto',
            'title': 'Convença-me num Minuto - Temporada 3',
        },
        'playlist_mincount': 32
    }, {
        'url': 'https://radiocomercial.pt/podcasts/o-homem-que-mordeu-o-cao',
        'info_dict': {
            'id': 'o-homem-que-mordeu-o-cao',
            'title': 'O Homem Que Mordeu o Cão',
        },
        '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
    }]

    def _fetch_page(self, podcast, season, page):
        page += 1
        url = urljoin('https://radiocomercial.pt/podcasts/', podcast + (f'/t{season}' if season else '') + f'/{page}')
        playlist_id = join_nonempty(podcast, season, delim='_')
        webpage = self._download_webpage(url, playlist_id, note=f'Downloading page: {page}')

        episodes = set(traverse_obj(get_elements_html_by_class('tm-ouvir-podcast', webpage),
                                    (..., {extract_attributes}, 'href')))
        for entry in episodes:
            yield self.url_result(urljoin('https://radiocomercial.pt', entry), RadioComercialIE)

    def _real_extract(self, url):
        podcast, season = self._match_valid_url(url).group('id', 'season')
        webpage = self._download_webpage(url, podcast)

        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_result(OnDemandPagedList(functools.partial(self._fetch_page, podcast, season),
                                                      self._PAGE_SIZE), podcast, title)


Review comment (refactor):
IMO it's simpler to just pass the url and id to the page func (sorry, I know this is the opposite of what sepro suggested)

Also fyi `urljoin` didn't really need to be used where you had used it; it's particularly useful when:
- the 2nd argument's value is uncertain, and you want to return `None` if it's falsy
- the 2nd argument's value is uncertain, and you want to return *only* the 2nd argument when it is a complete URL instead of just a path
- you want the 1st argument to be stripped of any query/fragment
- you want a leading slash in the 2nd argument to have it be interpreted as a root directory

And it won't work in my suggestion below, because if the input URL lacks a trailing slash, then `urljoin` would omit the last subdir in the URL path when joining. So we will f-string it:
```suggestion
    def _fetch_page(self, url, playlist_id, page):
        page += 1
        webpage = self._download_webpage(
            f'{url}/{page}', playlist_id, f'Downloading page {page}', expected_status=404)
```
We should also add `expected_status=404`. When we request a page number that doesn't exist, the website currently redirects to a `404.aspx` page without actually returning a 404 http error, but that behavior may change in the future

(see the accompanying suggestions in `_real_extract`)

Code after:
        },
        'playlist_mincount': 32
    }, {
        'url': 'https://radiocomercial.pt/podcasts/o-homem-que-mordeu-o-cao',
        'info_dict': {
            'id': 'o-homem-que-mordeu-o-cao',
            'title': 'O Homem Que Mordeu o Cão',
        },
        '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 ')