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

Code before:
            'title': 'Best Tom Hanks Movies',
            'description': 'We look at the filmography of America\'s Dad, as we count down our list of the best Tom Hanks movies.',
            'thumbnail': r're:^https?://.*\.jpg$',
            'duration': 1468.592
        },
    }, {
        'url': 'https://www.rottentomatoes.com/m/toy_story_3/trailers/VycaVoBKhGuk',
        'info_dict': {
            'id': 'VycaVoBKhGuk',
            'ext': 'mp4',
            'title': 'Toy Story 3: Trailer 2',
            'description': '',
            'thumbnail': r're:^https?://.*\.jpg$',
            'duration': 149.941
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        raw_video_info = self._html_search_regex(
            r'<script[^>]*id=["\']videos["\'][^>]*>([^<]+)</script>',
            webpage, 'video info', default=None)
        video_info = self._parse_json(raw_video_info, video_id, transform_source=js_to_json)

        return {
            'id': video_id,
            'title': traverse_obj(video_info, (0, 'title')),
            'description': traverse_obj(video_info, (0, 'description')),
            'thumbnail': traverse_obj(video_info, (0, 'image')),
            'formats': self._extract_m3u8_formats(
                traverse_obj(video_info, (0, 'file')), video_id, 'mp4', 'm3u8_native',
                m3u8_id=traverse_obj(video_info, (0, 'type')), note='Downloading m3u8 information',
                errnote='Unable to download m3u8 information'),
            'duration': parse_duration(traverse_obj(video_info, (0, 'durationInSeconds'),
                                                    default=traverse_obj(video_info, (0, 'duration'))))
        }


class RottenTomatoesPlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rottentomatoes\.com/m/(?P<id>[^/]+)(?:/trailers)?/?(?:$|[#?])'

    _TESTS = [{
        'url': 'http://www.rottentomatoes.com/m/toy_story_3',
        'info_dict': {
            'id': 'toy_story_3',
            'title': 'Toy Story 3',
        },
        'playlist_mincount': 4,
    }, {
        'url': 'http://www.rottentomatoes.com/m/toy_story_3/trailers',

Review comment (refactor):
```suggestion
```
then everything from this point and below could be replaced with:
```py
        if video_id:
            video_data = traverse_obj(data, lambda _, v: v['publicId'] == video_id)
            if not video_data:
                raise ExtractorError('Unable to extract video from webpage')
            return next(self._extract_videos(video_data, video_id))

        return self.playlist_result(
            self._extract_videos(data, playlist_id), playlist_id,
            clean_html(get_element_by_class('scoreboard__title', webpage)))
```
There's no need for `url_result`s since all of the data for every video in the playlist is already present in each webpage. We can check if a `video_id` is present in the user-provided URL, and if so, then we can filter for that id in the JSON data and only extract that video. If a playlist URL was passed, then we can extract all videos as playlist entries.

Code after:
            'id': '11028566',
            'ext': 'mp4',
            'title': 'Toy Story 3',
            'description': 'From the creators of the beloved TOY STORY films, comes a story that will reunite the gang in a whole new way.'
        },
        'skip': 'No longer available',
    }, {
        'url': 'https://www.rottentomatoes.com/m/toy_story_3/trailers/VycaVoBKhGuk',
        'info_dict': {
            'id': 'VycaVoBKhGuk',
            'ext': 'mp4',
            'title': 'Toy Story 3: Trailer 2',
            'description': '',
            'thumbnail': r're:^https?://.*\.jpg$',
            'duration': 149.941
        },
    }, {
        'url': 'http://www.rottentomatoes.com/m/toy_story_3',
        'info_dict': {
            'id': 'toy_story_3',
            'title': 'Toy Story 3',
        },
        'playlist_mincount': 4,
    }, {
        'url': 'http://www.rottentomatoes.com/m/toy_story_3/trailers',
        'info_dict': {
            'id': 'toy_story_3-trailers',
        },
        'playlist_mincount': 5,
    }]

    def _extract_videos(self, data, display_id):
        for video in traverse_obj(data, (lambda _, v: v['publicId'] and v['file'] and v['type'] == 'hls')):
            yield {
                'formats': self._extract_m3u8_formats(
                    video['file'], display_id, 'mp4', m3u8_id='hls', fatal=False),
                **traverse_obj(video, {
                    'id': 'publicId',
                    'title': 'title',
                    'description': 'description',
                    'duration': ('durationInSeconds', {float_or_none}),
                    'thumbnail': ('image', {url_or_none}),
                }),
            }

    def _real_extract(self, url):
        playlist_id, trailers, video_id = self._match_valid_url(url).group('playlist', 'tr', 'id')
        playlist_id = join_nonempty(playlist_id, trailers)
        webpage = self._download_webpage(url, playlist_id)
        data = self._search_json(
            r'<script[^>]+\bid=["\'](?:heroV|v)ideos["\'][^>]*>', webpage,