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

Code before:
            'id': '184f9970e7ba48e4915f1b252c55015e',
        }
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        page = self._download_webpage(url, video_id)
        meta = self._search_json('window.app=', page, 'playlist info', video_id)

        playlist = traverse_obj(meta, ('falcorCache', 'playList', video_id))

        entries = []
        for entry in traverse_obj(playlist, ('catalogs', 'value')):
            # it's likely possible to get most/all of the metadata from the playlist page json,
            # but just going to go with the lazy solution for now
            content_id = entry.get('content_id')
            content_type = entry.get('content_type')
            if content_type == 'tv_show':
                url = f'https://cu.tbs.co.jp/program/{content_id}'
            elif content_type == 'tv_episode':
                url = f'https://cu.tbs.co.jp/episode/{content_id}'
            else:
                self.report_warning(f'{content_id}: Unexpected content_type: {content_type}. Skipping.')

            if url:
                entries.append(self.url_result(url))

        return {
            '_type': 'playlist',
            **traverse_obj(playlist, {
                'id': ('id', 'value'),
                'title': ('display_name', 'value'),
                'playlist_count': ('total_count', 'value'),
            }),
            'entries': entries,
        }


Review comment (refactor):
`if url:` is problematic because `url` is already the input URL, so if the first entry's  `content_type` is unrecognized, the extractor will yield a recursively nested playlist. And `url` will never be falsy if `content_type` is unrecognized, because it will still have its value from the last iteration.

Instead we can append to `entries` immediately and avoid reused/unbound vars

(and in the traversal we can filter for dicts with a truthy `content_id` value)
```suggestion
        entries = []
        for entry in traverse_obj(playlist, ('catalogs', 'value', lambda _, v: v['content_id'])):
            # it's likely possible to get most/all of the metadata from the playlist page json instead
            content_id = entry['content_id']
            content_type = entry.get('content_type')
            if content_type == 'tv_show':
                entries.append(self.url_result(
                    f'https://cu.tbs.co.jp/program/{content_id}', TBSJPProgramIE, content_id))
            elif content_type == 'tv_episode':
                entries.append(self.url_result(
                    f'https://cu.tbs.co.jp/episode/{content_id}', TBSJPEpisodeIE, content_id))
            else:
                self.report_warning(f'Skipping "{content_id}" with unsupported content_type "{content_type}"')
```

Code after:
        'info_dict': {
            'title': 'まもなく配信終了',
            'id': '184f9970e7ba48e4915f1b252c55015e',
        }
    }]

    def _real_extract(self, url):
        playlist_id = self._match_id(url)
        page = self._download_webpage(url, playlist_id)
        meta = self._search_json(r'window\.app\s*=', page, 'playlist info', playlist_id)
        playlist = traverse_obj(meta, ('falcorCache', 'playList', playlist_id))

        def entries():
            for entry in traverse_obj(playlist, ('catalogs', 'value', lambda _, v: v['content_id'])):
                # TODO: it's likely possible to get all metadata from the playlist page json instead
                content_id = entry['content_id']
                content_type = entry.get('content_type')
                if content_type == 'tv_show':
                    yield self.url_result(
                        f'https://cu.tbs.co.jp/program/{content_id}', TBSJPProgramIE, content_id)
                elif content_type == 'tv_episode':
                    yield self.url_result(
                        f'https://cu.tbs.co.jp/episode/{content_id}', TBSJPEpisodeIE, content_id)
                else:
                    self.report_warning(f'Skipping "{content_id}" with unsupported content_type "{content_type}"')

        return self.playlist_result(entries(), playlist_id, traverse_obj(playlist, ('display_name', 'value')))