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

Code before:
        for season in seasons:
            season_id = season.get('id')
            if not season_id:
                continue
            page = 1
            per = 100
            has_more = True
            while has_more is True:
                season = self._download_json(
                    f'https://appservice.puhutv.com/api/seasons/{season_id}/episodes?v=2',
                    season_id, f'Downloading episode {(page - 1) * per}-{page * per} metadata', query={
                        'page': page,
                        'per': per,
                    })['data']

                episodes = season['episodes']
                for episode in episodes:
                    video_id = episode['id']
                    video_title = episode['name']
                    slug = episode.get('slug') or episode['assets'][0]['slug']
                    if not slug:
                        continue

                    yield self.url_result(f'https://puhutv.com/{slug}',
                                          ie=PuhuTVIE.ie_key(), video_id=video_id,
                                          video_title=video_title)
                page += 1
                has_more = season['has_more']

    def _real_extract(self, url):
        playlist_id = self._match_id(url)

        info = self._download_json(
            urljoin(url, f'/api/slug/{playlist_id}-detay'),
            playlist_id)['data']

        seasons = info.get('seasons')
        if seasons:
            return self.playlist_result(
                self._extract_entries(seasons), playlist_id, info.get('name'))

        # For films, these are using same url with series
        video_id = info.get('slug') or info['assets'][0]['slug']
        return self.url_result(
            f'https://puhutv.com/{video_id}-izle',
            PuhuTVIE.ie_key(), video_id)


Review comment (refactor):
let's make the metadata extraction non-fatal
```suggestion
                for episode in traverse_obj(season, ('episodes', lambda _, v: v.get('slug') or v['assets'][0]['slug'])):
                    slug = episode.get('slug') or episode['assets'][0]['slug']
                    yield self.url_result(
                        f'https://puhutv.com/{slug}', PuhuTVIE, episode.get('id'), episode.get('name'))
```

Code after:
    def _extract_entries(self, seasons):
        for season in seasons:
            season_id = season.get('id')
            if not season_id:
                continue
            page = 1
            has_more = True
            while has_more is True:
                season = self._download_json(
                    f'https://appservice.puhutv.com/api/seasons/{season_id}/episodes?v=2',
                    season_id, f'Downloading page {page}', query={
                        'page': page,
                        'per': 100,
                    })['data']

                for episode in traverse_obj(season, ('episodes', lambda _, v: v.get('slug') or v['assets'][0]['slug'])):
                    slug = episode.get('slug') or episode['assets'][0]['slug']
                    yield self.url_result(
                        f'https://puhutv.com/{slug}', PuhuTVIE, episode.get('id'), episode.get('name'))

                page += 1
                has_more = traverse_obj(season, 'has_more')

    def _real_extract(self, url):
        playlist_id = self._match_id(url)

        info = self._download_json(
            urljoin(url, f'/api/slug/{playlist_id}-detay'),
            playlist_id)['data']

        seasons = info.get('seasons')
        if seasons:
            return self.playlist_result(
                self._extract_entries(seasons), playlist_id, info.get('name'))

        # For films, these are using same url with series
        video_id = info.get('slug')
        if video_id:
            video_id = video_id.removesuffix('-detay')
        else:
            video_id = info['assets'][0]['slug'].removesuffix('-izle')
        return self.url_result(
            f'https://puhutv.com/{video_id}-izle', PuhuTVIE, video_id)