ronantakizawa/codereview-bench huggingface dataset:
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/tunein.py (Python)

Code before:
            'display_id': '236404354',
            'duration': 8330,
            'series': 'Lex Fridman Podcast',
            'series_id': 'p1153019',
            'thumbnail': r're:https?://.+',
            'timestamp': 1673458571,
            'upload_date': '20230111',
        },
        'params': {'skip_download': 'Livestream'},
    }, {
        'url': 'http://tun.in/pei6i',
        'info_dict': {
            'id': 'p14',
            'title': 'BBC News',
        },
        'playlist_mincount': 35,
    }]

    def _real_extract(self, url):
        redirect_id = self._match_id(url)
        # The server doesn't support HEAD requests
        urlh = self._request_webpage(url, redirect_id, 'Downloading redirect page')
        parsed = urllib.parse.urlparse(urlh.url)

        return self.url_result(
            urllib.parse.urlunparse(parsed._replace(netloc=parsed.hostname)))


Review comment (bug):
```suggestion
        new_url = urllib.parse.urlunparse(parsed._replace(netloc=parsed.hostname))
        if self.suitable(new_url):  # Prevent infinite loop in case redirect fails
            raise UnsupportedError(new_url)
        return self.url_result(new_url)
```

Code after:
            'display_id': '236404354',
            'duration': 8330,
            'series': 'Lex Fridman Podcast',
            'series_id': 'p1153019',
            'thumbnail': r're:https?://.+',
            'timestamp': 1673458571,
            'upload_date': '20230111',
        },
        'params': {'skip_download': 'Livestream'},
    }, {
        'url': 'http://tun.in/pei6i',
        'info_dict': {
            'id': 'p14',
            'title': 'BBC News',
        },
        'playlist_mincount': 35,
    }]

    def _real_extract(self, url):
        redirect_id = self._match_id(url)
        # The server doesn't support HEAD requests
        urlh = self._request_webpage(url, redirect_id, 'Downloading redirect page')
        # Need to strip port from URL
        parsed = urllib.parse.urlparse(urlh.url)
        new_url = parsed._replace(netloc=parsed.hostname).geturl()
        # Prevent infinite loop in case redirect fails
        if self.suitable(new_url):
            raise UnsupportedError(new_url)
        return self.url_result(new_url)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/medialaan.py (Python)

Code before:
            'ext': 'mp4',
            'title': 'Tom Waes blaastest',
            'channel': 'De Morgen',
            'channel_id': '352',
            'description': 'Tom Waes werkt mee aan een alcoholcampagne op Werchter',
            'duration': 62,
            'genres': ['News'],
            'release_date': '20250705',
            'release_timestamp': 1751730795,
            'series': 'Nieuwsvideo\'s',
            'series_id': '1683',
            'tags': 'count:1',
            'thumbnail': r're:https?://video-images\.persgroep\.be/aws_generated.+\.jpg',
            'uploader': 'De Morgen',
            'uploader_id': '17',
        },
        'params': {'extractor_args': {'generic': {'impersonate': ['chrome']}}},
    }]

    @classmethod
    def _extract_embed_urls(cls, url, webpage):
        return traverse_obj(webpage, (
            {find_elements(tag='div', attr='data-mychannels-type', value='video', html=True)},
            ..., {extract_attributes}, 'data-mychannels-id', {str},
            {lambda x: f'https://mychannels.video/embed/{x}'}, {url_or_none}, filter, all, filter,
        ))

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

        return self._extract_from_mychannels_api(mychannels_id)


Review comment (refactor):
we can `yield from` here

and more importantly, we need to `filter` before the f-string transformation or else we could end up with `/embed/None` URLs

and since we're filtering beforehand (and not using `all`) that means everything after the transformation is unnecessary
```suggestion
        yield from traverse_obj(webpage, (
            {find_elements(tag='div', attr='data-mychannels-type', value='video', html=True)},
            ..., {extract_attributes}, 'data-mychannels-id', {str}, filter,
            {lambda x: f'https://mychannels.video/embed/{x}'}))
```

Code after:
            'ext': 'mp4',
            'title': 'Tom Waes blaastest',
            'channel': 'De Morgen',
            'channel_id': '352',
            'description': 'Tom Waes werkt mee aan een alcoholcampagne op Werchter',
            'duration': 62,
            'genres': ['News'],
            'release_date': '20250705',
            'release_timestamp': 1751730795,
            'series': 'Nieuwsvideo\'s',
            'series_id': '1683',
            'tags': 'count:1',
            'thumbnail': r're:https?://video-images\.persgroep\.be/aws_generated.+\.jpg',
            'uploader': 'De Morgen',
            'uploader_id': '17',
        },
        'params': {'extractor_args': {'generic': {'impersonate': ['chrome']}}},
    }]

    @classmethod
    def _extract_embed_urls(cls, url, webpage):
        yield from traverse_obj(webpage, (
            {find_elements(tag='div', attr='data-mychannels-type', value='video', html=True)},
            ..., {extract_attributes}, 'data-mychannels-id', {str}, filter,
            {lambda x: f'https://mychannels.video/embed/{x}'}))

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

        return self._extract_from_mychannels_api(mychannels_id)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/vtm.py (Python)

Code before:
from .medialaan import MedialaanBaseIE
from ..utils import str_or_none
from ..utils.traversal import require, traverse_obj


class VTMIE(MedialaanBaseIE):
    _VALID_URL = r'https?://(?:www\.)?vtm\.be/[^/?#]+~v(?P<id>[\da-f-]+)'
    _TESTS = [{
        'url': 'https://vtm.be/gast-vernielt-genkse-hotelkamer~ve7534523-279f-4b4d-a5c9-a33ffdbe23e1',
        'info_dict': {
            'id': '192445',
            'ext': 'mp4',
            'title': 'Gast vernielt Genkse hotelkamer',
            'channel': 'VTM',
            'channel_id': '867',
            'description': 'md5:75fce957d219646ff1b65ba449ab97b5',
            'duration': 74,
            'genres': ['Documentaries'],
            'release_date': '20210119',
            'release_timestamp': 1611060180,
            'series': 'Op Interventie',
            'series_id': '2658',
            'tags': 'count:2',
            'thumbnail': r're:https?://images\.mychannels\.video/imgix/.+\.(?:jpe?g|png)',
            'uploader': 'VTM',
            'uploader_id': '74',
        },
    }]

    def _real_initialize(self):
        if not self._get_cookies('https://vtm.be/').get('authId'):
            self.raise_login_required()

Review comment (suggestion):
I like having the actual UUID pattern in here
```suggestion
    _VALID_URL = r'https?://(?:www\.)?vtm\.be/[^/?#]+~v(?P<id>[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12})'
```

Code after:
from .medialaan import MedialaanBaseIE
from ..utils import str_or_none
from ..utils.traversal import require, traverse_obj


class VTMIE(MedialaanBaseIE):
    _VALID_URL = r'https?://(?:www\.)?vtm\.be/[^/?#]+~v(?P<id>[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12})'
    _TESTS = [{
        'url': 'https://vtm.be/gast-vernielt-genkse-hotelkamer~ve7534523-279f-4b4d-a5c9-a33ffdbe23e1',
        'info_dict': {
            'id': '192445',
            'ext': 'mp4',
            'title': 'Gast vernielt Genkse hotelkamer',
            'channel': 'VTM',
            'channel_id': '867',
            'description': 'md5:75fce957d219646ff1b65ba449ab97b5',
            'duration': 74,
            'genres': ['Documentaries'],
            'release_date': '20210119',
            'release_timestamp': 1611060180,
            'series': 'Op Interventie',
            'series_id': '2658',
            'tags': 'count:2',
            'thumbnail': r're:https?://images\.mychannels\.video/imgix/.+\.(?:jpe?g|png)',
            'uploader': 'VTM',
            'uploader_id': '74',
        },
    }]

    def _real_initialize(self):
        if not self._get_cookies('https://vtm.be/').get('authId'):
            self.raise_login_required()
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/vtm.py (Python)

Code before:
        'info_dict': {
            'id': '192445',
            'ext': 'mp4',
            'title': 'Gast vernielt Genkse hotelkamer',
            'channel': 'VTM',
            'channel_id': '867',
            'description': 'md5:75fce957d219646ff1b65ba449ab97b5',
            'duration': 74,
            'genres': ['Documentaries'],
            'release_date': '20210119',
            'release_timestamp': 1611060180,
            'series': 'Op Interventie',
            'series_id': '2658',
            'tags': 'count:2',
            'thumbnail': r're:https?://images\.mychannels\.video/imgix/.+\.(?:jpe?g|png)',
            'uploader': 'VTM',
            'uploader_id': '74',
        },
    }]

    def _real_initialize(self):
        if not self._get_cookies('https://vtm.be/').get('authId'):
            self.raise_login_required()

    def _real_extract(self, url):
        uuid = self._match_id(url)
        webpage = self._download_webpage(url, uuid)
        apollo_state = self._search_json(
            r'window\.__APOLLO_STATE__\s*=', webpage, 'apollo state', uuid)
        mychannels_id = traverse_obj(apollo_state, (
            f'Video:{{"uuid":"{uuid}"}}', 'myChannelsVideo', {str_or_none}, {require('mychannels ID')}))

        return self._extract_from_mychannels_api(mychannels_id)


Review comment (suggestion):
let's avoid shadowing a stdlib module name
```suggestion
        video_id = self._match_id(url)
```

Code after:
        'info_dict': {
            'id': '192445',
            'ext': 'mp4',
            'title': 'Gast vernielt Genkse hotelkamer',
            'channel': 'VTM',
            'channel_id': '867',
            'description': 'md5:75fce957d219646ff1b65ba449ab97b5',
            'duration': 74,
            'genres': ['Documentaries'],
            'release_date': '20210119',
            'release_timestamp': 1611060180,
            'series': 'Op Interventie',
            'series_id': '2658',
            'tags': 'count:2',
            'thumbnail': r're:https?://images\.mychannels\.video/imgix/.+\.(?:jpe?g|png)',
            'uploader': 'VTM',
            'uploader_id': '74',
        },
    }]

    def _real_initialize(self):
        if not self._get_cookies('https://vtm.be/').get('authId'):
            self.raise_login_required()

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        apollo_state = self._search_json(
            r'window\.__APOLLO_STATE__\s*=', webpage, 'apollo state', video_id)
        mychannels_id = traverse_obj(apollo_state, (
            f'Video:{{"uuid":"{video_id}"}}', 'myChannelsVideo', {str_or_none}, {require('mychannels ID')}))

        return self._extract_from_mychannels_api(mychannels_id)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/vtm.py (Python)

Code before:
            'channel_id': '867',
            'description': 'md5:75fce957d219646ff1b65ba449ab97b5',
            'duration': 74,
            'genres': ['Documentaries'],
            'release_date': '20210119',
            'release_timestamp': 1611060180,
            'series': 'Op Interventie',
            'series_id': '2658',
            'tags': 'count:2',
            'thumbnail': r're:https?://images\.mychannels\.video/imgix/.+\.(?:jpe?g|png)',
            'uploader': 'VTM',
            'uploader_id': '74',
        },
    }]

    def _real_initialize(self):
        if not self._get_cookies('https://vtm.be/').get('authId'):
            self.raise_login_required()

    def _real_extract(self, url):
        uuid = self._match_id(url)
        webpage = self._download_webpage(url, uuid)
        apollo_state = self._search_json(
            r'window\.__APOLLO_STATE__\s*=', webpage, 'apollo state', uuid)
        mychannels_id = traverse_obj(apollo_state, (
            f'Video:{{"uuid":"{uuid}"}}', 'myChannelsVideo', {str_or_none}, {require('mychannels ID')}))

        return self._extract_from_mychannels_api(mychannels_id)


Review comment (suggestion):
```suggestion
        webpage = self._download_webpage(url, video_id)
        apollo_state = self._search_json(
            r'window\.__APOLLO_STATE__\s*=', webpage, 'apollo state', video_id)
        mychannels_id = traverse_obj(apollo_state, (
            f'Video:{{"uuid":"{video_id}"}}', 'myChannelsVideo', {str_or_none}, {require('mychannels ID')}))
```

Code after:
            'channel_id': '867',
            'description': 'md5:75fce957d219646ff1b65ba449ab97b5',
            'duration': 74,
            'genres': ['Documentaries'],
            'release_date': '20210119',
            'release_timestamp': 1611060180,
            'series': 'Op Interventie',
            'series_id': '2658',
            'tags': 'count:2',
            'thumbnail': r're:https?://images\.mychannels\.video/imgix/.+\.(?:jpe?g|png)',
            'uploader': 'VTM',
            'uploader_id': '74',
        },
    }]

    def _real_initialize(self):
        if not self._get_cookies('https://vtm.be/').get('authId'):
            self.raise_login_required()

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        apollo_state = self._search_json(
            r'window\.__APOLLO_STATE__\s*=', webpage, 'apollo state', video_id)
        mychannels_id = traverse_obj(apollo_state, (
            f'Video:{{"uuid":"{video_id}"}}', 'myChannelsVideo', {str_or_none}, {require('mychannels ID')}))

        return self._extract_from_mychannels_api(mychannels_id)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/tenplay.py (Python)

Code before:
import itertools

from .common import InfoExtractor
from ..networking import HEADRequest
from ..utils import ExtractorError, int_or_none, traverse_obj, update_url_query, url_or_none, urljoin


class TenPlayIE(InfoExtractor):
    IE_NAME = '10play'
    _VALID_URL = r'https?://(?:www\.)?10(?:play)?\.com\.au/(?:[^/?#]+/)+(?P<id>tpv\d{6}[a-z]{5})'
    _NETRC_MACHINE = '10play'
    _TESTS = [{
        # Geo-restricted to Australia
        'url': 'https://10.com.au/australian-survivor/web-extras/season-10-brains-v-brawn-ii/myless-journey/tpv250414jdmtf',
        'info_dict': {
            'id': '7440980000013868',
            'ext': 'mp4',
            'title': 'Myles\'s Journey',
            'alt_title': 'Myles\'s Journey',
            'description': 'Relive Myles\'s epic Brains V Brawn II journey to reach the game\'s final two',
            'uploader': 'Channel 10',
            'uploader_id': '2199827728001',
            'age_limit': 15,
            'duration': 249,
            'thumbnail': r're:https://.+/.+\.jpg',
            'series': 'Australian Survivor',
            'season': 'Season 10',
            'season_number': 10,
            'timestamp': 1744629420,
            'upload_date': '20250414',

Review comment (refactor):
```suggestion
from ..utils import (
    ExtractorError,
    int_or_none,
    update_url_query,
    url_or_none,
    urljoin,
)
from ..utils.traversal import traverse_obj
```

Code after:
import itertools

from .common import InfoExtractor
from ..networking import HEADRequest
from ..utils import (
    ExtractorError,
    int_or_none,
    update_url_query,
    url_or_none,
    urljoin,
)
from ..utils.traversal import traverse_obj


class TenPlayIE(InfoExtractor):
    IE_NAME = '10play'
    _VALID_URL = r'https?://(?:www\.)?10(?:play)?\.com\.au/(?:[^/?#]+/)+(?P<id>tpv\d{6}[a-z]{5})'
    _NETRC_MACHINE = '10play'
    _TESTS = [{
        # Geo-restricted to Australia
        'url': 'https://10.com.au/australian-survivor/web-extras/season-10-brains-v-brawn-ii/myless-journey/tpv250414jdmtf',
        'info_dict': {
            'id': '7440980000013868',
            'ext': 'mp4',
            'title': 'Myles\'s Journey',
            'alt_title': 'Myles\'s Journey',
            'description': 'Relive Myles\'s epic Brains V Brawn II journey to reach the game\'s final two',
            'uploader': 'Channel 10',
            'uploader_id': '2199827728001',
            'age_limit': 15,
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/firsttv.py (Python)

Code before:
    _GEO_COUNTRIES = ['RU']

    _TESTS = [
        {
            'url': 'https://www.1tv.ru/live',
            'info_dict': {
                'id': 'live',
                'ext': 'mp4',
                'title': 'ПЕРВЫЙ КАНАЛ ПРЯМОЙ ЭФИР СМОТРЕТЬ ОНЛАЙН',
                'is_live': True,
            },
        },
    ]

    def _real_extract(self, url):
        display_id = 'live'
        webpage = self._download_webpage(url, display_id)

        streams_list = self._download_json('https://stream.1tv.ru/api/playlist/1tvch-v1_as_array.json', 'live')
        mpd_list = traverse_obj(streams_list, ('mpd', ..., {url_or_none}))

        if not mpd_list:
            raise ExtractorError('Can\'t download json with mpd streams')
        mpd_formats, mpd_subtitles = self._extract_mpd_formats_and_subtitles(
            mpd_url=mpd_list[0], video_id='live_mpd',
        )

        # It is mandatory to use the '-re' option for ffmpeg,
        # otherwise the recording of fragments will stop after
        # a while due to the speed n-times faster than real time.
        for f in mpd_formats:
            f.update({'downloader_options': {'ffmpeg_args': ['-re'], 'ffmpeg_args_out': ['-c', 'copy', '-f', 'mp4']}})

        return {
            'id': display_id,
            'title': self._html_extract_title(webpage),
            'ext': 'mp4',
            'formats': mpd_formats,
            'subtitles': mpd_subtitles,
            'is_live': True,
        }


Review comment (refactor):
```suggestion
        mpd_url = traverse_obj(streams_list, ('mpd', ..., {url_or_none}, any, {require('mpd url')}))

        formats, subtitles = self._extract_mpd_formats_and_subtitles(
            mpd_url, display_id, mpd_id='dash', fatal=False)
```
import `require` from `..utils.traversal`

Code after:
        'url': 'https://www.1tv.ru/live',
        'info_dict': {
            'id': 'live',
            'ext': 'mp4',
            'title': r're:ПЕРВЫЙ КАНАЛ ПРЯМОЙ ЭФИР СМОТРЕТЬ ОНЛАЙН \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {'skip_download': 'livestream'},
    }]

    def _real_extract(self, url):
        display_id = 'live'
        webpage = self._download_webpage(url, display_id, fatal=False)

        streams_list = self._download_json('https://stream.1tv.ru/api/playlist/1tvch-v1_as_array.json', display_id)
        mpd_url = traverse_obj(streams_list, ('mpd', ..., {url_or_none}, any, {require('mpd url')}))
        # FFmpeg needs to be passed -re to not seek past live window. This is handled by core
        formats, _ = self._extract_mpd_formats_and_subtitles(mpd_url, display_id, mpd_id='dash')

        return {
            'id': display_id,
            'title': self._html_extract_title(webpage),
            'formats': formats,
            'is_live': True,
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/firsttv.py (Python)

Code before:
            self._entries(items), display_id, self._og_search_title(webpage, default=None),
            thumbnail=self._og_search_thumbnail(webpage, default=None))


class FirstTVLiveIE(InfoExtractor):
    IE_NAME = '1tv-live'
    IE_DESC = 'Первый канал (прямой эфир)'
    _VALID_URL = r'https?://(?:www\.)?1tv\.ru/live'
    _GEO_BYPASS = False
    _GEO_COUNTRIES = ['RU']

    _TESTS = [
        {
            'url': 'https://www.1tv.ru/live',
            'info_dict': {
                'id': 'live',
                'ext': 'mp4',
                'title': 'ПЕРВЫЙ КАНАЛ ПРЯМОЙ ЭФИР СМОТРЕТЬ ОНЛАЙН',
                'is_live': True,
            },
        },
    ]

    def _real_extract(self, url):
        display_id = 'live'
        webpage = self._download_webpage(url, display_id)

        streams_list = self._download_json('https://stream.1tv.ru/api/playlist/1tvch-v1_as_array.json', 'live')
        mpd_url = traverse_obj(streams_list, ('mpd', ..., {url_or_none}, any))
        formats = self._extract_mpd_formats(mpd_url, display_id, mpd_id='dash', fatal=False)
        hls_url = traverse_obj(streams_list, ('hls', ..., {url_or_none}, any))
        formats.extend(self._extract_m3u8_formats(hls_url, display_id, 'mp4', m3u8_id='hls', live=True, fatal=False))
        if not formats:
            raise ExtractorError('Unable to extract any stream')

        return {
            'id': display_id,
            'title': self._html_extract_title(webpage),
            'formats': formats,
            'is_live': True,
        }


Review comment (suggestion):
Probably doesn't make a huge difference, but
```suggestion
        webpage = self._download_webpage(url, display_id, fatal=False)
```

Code after:
            thumbnail=self._og_search_thumbnail(webpage, default=None))


class FirstTVLiveIE(InfoExtractor):
    IE_NAME = '1tv:live'
    IE_DESC = 'Первый канал (прямой эфир)'
    _VALID_URL = r'https?://(?:www\.)?1tv\.ru/live'

    _TESTS = [{
        'url': 'https://www.1tv.ru/live',
        'info_dict': {
            'id': 'live',
            'ext': 'mp4',
            'title': r're:ПЕРВЫЙ КАНАЛ ПРЯМОЙ ЭФИР СМОТРЕТЬ ОНЛАЙН \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {'skip_download': 'livestream'},
    }]

    def _real_extract(self, url):
        display_id = 'live'
        webpage = self._download_webpage(url, display_id, fatal=False)

        streams_list = self._download_json('https://stream.1tv.ru/api/playlist/1tvch-v1_as_array.json', display_id)
        mpd_url = traverse_obj(streams_list, ('mpd', ..., {url_or_none}, any, {require('mpd url')}))
        # FFmpeg needs to be passed -re to not seek past live window. This is handled by core
        formats, _ = self._extract_mpd_formats_and_subtitles(mpd_url, display_id, mpd_id='dash')

        return {
            'id': display_id,
            'title': self._html_extract_title(webpage),
            'formats': formats,
            'is_live': True,
        }