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

Code before:
                subtitles.setdefault(lang, []).append({
                    'ext': mimetype2ext(mime),
                    'url': src,
                })

        duration = info.get('duration')
        tp_chapters = info.get('chapters', [])
        chapters = []
        if tp_chapters:
            def _add_chapter(start_time, end_time):
                start_time = float_or_none(start_time, 1000)
                end_time = float_or_none(end_time, 1000)
                if start_time is None or end_time is None:
                    return
                chapters.append({
                    'start_time': start_time,
                    'end_time': end_time,
                })

            for chapter in tp_chapters[:-1]:
                _add_chapter(chapter.get('startTime'), chapter.get('endTime'))
            _add_chapter(tp_chapters[-1].get('startTime'), tp_chapters[-1].get('endTime') or duration)

        def extract_site_specific_field(field):
            # A number of sites have custom-prefixed keys, e.g. 'cbc$seasonNumber'
            return next((info[k] for k in info if k.endswith(f'${field}') and info[k] != ''), None)

        return {
            'title': info['title'],
            'subtitles': subtitles,
            'description': info['description'],
            'thumbnail': info['defaultThumbnailUrl'],
            'duration': float_or_none(duration, 1000),
            'timestamp': int_or_none(info.get('pubDate'), 1000) or None,
            'uploader': info.get('billingCode'),
            'chapters': chapters,
            'creator': traverse_obj(info, ('author', {str})) or None,
            'categories': traverse_obj(info, (
                'categories', lambda _, v: v.get('label') in ('category', None), 'name', {str})) or None,
            'tags': traverse_obj(info, ('keywords', {lambda x: re.split(r'[;,]\s?', x) if x else None})),
            'location': extract_site_specific_field('region'),
            'series': extract_site_specific_field('show'),
            'season_number': int_or_none(extract_site_specific_field('seasonNumber')),
            'media_type': extract_site_specific_field('programmingType') or extract_site_specific_field('type'),
        }

    def _extract_theplatform_metadata(self, path, video_id):
        info = self._download_theplatform_metadata(path, video_id)
        return self._parse_theplatform_metadata(info)



Review comment (suggestion):
```suggestion
            return traverse_obj(info, lambda k, v: v and k.endswith(f'${field}'), get_all=False)
```

Code after:
                subtitles.setdefault(lang, []).append({
                    'ext': mimetype2ext(mime),
                    'url': src,
                })

        duration = info.get('duration')
        tp_chapters = info.get('chapters', [])
        chapters = []
        if tp_chapters:
            def _add_chapter(start_time, end_time):
                start_time = float_or_none(start_time, 1000)
                end_time = float_or_none(end_time, 1000)
                if start_time is None or end_time is None:
                    return
                chapters.append({
                    'start_time': start_time,
                    'end_time': end_time,
                })

            for chapter in tp_chapters[:-1]:
                _add_chapter(chapter.get('startTime'), chapter.get('endTime'))
            _add_chapter(tp_chapters[-1].get('startTime'), tp_chapters[-1].get('endTime') or duration)

        def extract_site_specific_field(field):
            # A number of sites have custom-prefixed keys, e.g. 'cbc$seasonNumber'
            return traverse_obj(info, lambda k, v: v and k.endswith(f'${field}'), get_all=False)

        return {
            'title': info['title'],
            'subtitles': subtitles,
            'description': info['description'],
            'thumbnail': info['defaultThumbnailUrl'],
            'duration': float_or_none(duration, 1000),
            'timestamp': int_or_none(info.get('pubDate'), 1000) or None,
            'uploader': info.get('billingCode'),
            'chapters': chapters,
            'creator': traverse_obj(info, ('author', {str})) or None,
            'categories': traverse_obj(info, (
                'categories', lambda _, v: v.get('label') in ('category', None), 'name', {str})) or None,
            'tags': traverse_obj(info, ('keywords', {lambda x: re.split(r'[;,]\s?', x) if x else None})),
            'location': extract_site_specific_field('region'),
            'series': extract_site_specific_field('show'),
            'season_number': int_or_none(extract_site_specific_field('seasonNumber')),
            'media_type': extract_site_specific_field('programmingType') or extract_site_specific_field('type'),
        }

    def _extract_theplatform_metadata(self, path, video_id):
        info = self._download_theplatform_metadata(path, video_id)
        return self._parse_theplatform_metadata(info)