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

Code before:

        # US accessed article with single embedded video (e.g.
        # https://www.bbc.com/news/uk-68546268)
        next_data = traverse_obj(self._search_nextjs_data(webpage, playlist_id), (
            'props', 'pageProps', 'page'), get_all=False)
        video_data = traverse_obj(next_data, (
            ..., 'contents', lambda _, v: v['type'] == 'video'), get_all=False)
        if video_data:
            model = traverse_obj(video_data, (
                'model', 'blocks', lambda _, v: v['type'] == 'media',
                'model', 'blocks', lambda _, v: v['type'] == 'mediaMetadata',
                'model'), get_all=False)
            if model:
                timestamp = traverse_obj(next_data, (
                    ..., 'contents', lambda _, v: v['type'] == 'timestamp',
                    'model', 'timestamp', {int_or_none}, any))
                item_id = traverse_obj(model, ('versions', 0, 'versionId', {str}))
                formats, subtitles = self._download_media_selector(item_id)
                entries.append({
                    'id': item_id,
                    'formats': formats,
                    'subtitles': subtitles,
                    'timestamp': timestamp,
                    **traverse_obj(model, {
                        'title': ('title', {str}),
                        'thumbnail': ('imageUrl', {url_or_none}),
                        'description': (
                            'synopses', ('long', 'medium', 'short'), {str}, any),
                    })
                })
            return self.playlist_result(
                entries, playlist_id, playlist_title, playlist_description)

        # Multiple video article (e.g.
        # http://www.bbc.co.uk/blogs/adamcurtis/entries/3662a707-0af9-3149-963f-47bea720b460)
        EMBED_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:[^/]+/)+%s(?:\b[^"]+)?' % self._ID_REGEX
        entries = []
        for match in extract_all(r'new\s+SMP\(({.+?})\)'):
            embed_url = match.get('playerSettings', {}).get('externalEmbedUrl')
            if embed_url and re.match(EMBED_URL, embed_url):
                entries.append(embed_url)
        entries.extend(re.findall(
            r'setPlaylist\("(%s)"\)' % EMBED_URL, webpage))
        if entries:
            return self.playlist_result(
                [self.url_result(entry_, 'BBCCoUk') for entry_ in entries],
                playlist_id, playlist_title, playlist_description)

        # Multiple video article (e.g. http://www.bbc.com/news/world-europe-32668511)
        medias = extract_all(r"data-media-meta='({[^']+})'")


Review comment (bug):
As in several places elsewhere in the existing code (eg l.1100): 
```suggestion
                        'thumbnail': ('imageUrl', {lambda u: url_or_none(u.replace('$recipe', 'raw'))}),
```
I can't tell from l.802 if this applies to the US page but it probably does. However the equivalent `imageUrl` in the UK page has no scheme which makes it fail `url_or_none`; `lambda u: urljoin(url, u)` is needed instead.    

Code after:
                        'formats': formats,
                        'subtitles': subtitles,
                    })
                return self.playlist_result(
                    entries, playlist_id, playlist_title, playlist_description)

        def parse_model(model):
            """Extract single video from model structure"""
            item_id = traverse_obj(model, ('versions', 0, 'versionId', {str}))
            if not item_id:
                return
            formats, subtitles = self._download_media_selector(item_id)
            return {
                'id': item_id,
                'formats': formats,
                'subtitles': subtitles,
                **traverse_obj(model, {
                    'title': ('title', {str}),
                    'thumbnail': ('imageUrl', {lambda u: urljoin(url, u.replace('$recipe', 'raw'))}),
                    'description': ('synopses', ('long', 'medium', 'short'), {str}, {lambda x: x or None}, any),
                    'duration': ('versions', 0, 'duration', {int}),
                    'timestamp': ('versions', 0, 'availableFrom', {functools.partial(int_or_none, scale=1000)}),
                })
            }

        def is_type(*types):
            return lambda _, v: v['type'] in types

        initial_data = self._search_regex(
            r'window\.__INITIAL_DATA__\s*=\s*("{.+?}")\s*;', webpage,
            'quoted preload state', default=None)
        if initial_data is None:
            initial_data = self._search_regex(
                r'window\.__INITIAL_DATA__\s*=\s*({.+?})\s*;', webpage,
                'preload state', default='{}')
        else:
            initial_data = self._parse_json(initial_data or '"{}"', playlist_id, fatal=False)
        initial_data = self._parse_json(initial_data, playlist_id, fatal=False)
        if initial_data:
            for video_data in traverse_obj(initial_data, (
                    'stores', 'article', 'articleBodyContent', is_type('video'))):
                model = traverse_obj(video_data, (
                    'model', 'blocks', is_type('aresMedia'),
                    'model', 'blocks', is_type('aresMediaMetadata'),
                    'model', {dict}, any))
                entry = parse_model(model)
                if entry:
                    entries.append(entry)
            if entries:
                return self.playlist_result(
                    entries, playlist_id, playlist_title, playlist_description)