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

Code before:
from .common import InfoExtractor
from .vimeo import VimeoIE
from ..utils import traverse_obj, url_or_none


class AeonCoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?aeon\.co/videos/(?P<id>[^/?]+)'
    _TESTS = [{
        'url': 'https://aeon.co/videos/raw-solar-storm-footage-is-the-punk-rock-antidote-to-sleek-james-webb-imagery',
        'md5': 'e5884d80552c9b6ea8d268a258753362',
        'info_dict': {
            'id': '1284717',
            'ext': 'mp4',
            'title': 'Brilliant Noise',
            'thumbnail': 'https://i.vimeocdn.com/video/21006315-1a1e49da8b07fd908384a982b4ba9ff0268c509a474576ebdf7b1392f4acae3b-d_960',
            'uploader': 'Semiconductor',
            'uploader_id': 'semiconductor',
            'uploader_url': 'https://vimeo.com/semiconductor',
            'duration': 348
        }
    }, {
        'url': 'https://aeon.co/videos/dazzling-timelapse-shows-how-microbes-spoil-our-food-and-sometimes-enrich-it',
        'md5': '03582d795382e49f2fd0b427b55de409',
        'info_dict': {
            'id': '759576926',
            'ext': 'mp4',
            'title': 'Wrought',
            'thumbnail': 'https://i.vimeocdn.com/video/1525599692-84614af88e446612f49ca966cf8f80eab2c73376bedd80555741c521c26f9a3e-d_1280',

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

Code after:
from .common import InfoExtractor
from .vimeo import VimeoIE
from ..utils import ExtractorError, traverse_obj, url_or_none


class AeonCoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?aeon\.co/videos/(?P<id>[^/?]+)'
    _TESTS = [{
        'url': 'https://aeon.co/videos/raw-solar-storm-footage-is-the-punk-rock-antidote-to-sleek-james-webb-imagery',
        'md5': 'e5884d80552c9b6ea8d268a258753362',
        'info_dict': {
            'id': '1284717',
            'ext': 'mp4',
            'title': 'Brilliant Noise',
            'thumbnail': 'https://i.vimeocdn.com/video/21006315-1a1e49da8b07fd908384a982b4ba9ff0268c509a474576ebdf7b1392f4acae3b-d_960',
            'uploader': 'Semiconductor',
            'uploader_id': 'semiconductor',
            'uploader_url': 'https://vimeo.com/semiconductor',
            'duration': 348
        }
    }, {
        'url': 'https://aeon.co/videos/dazzling-timelapse-shows-how-microbes-spoil-our-food-and-sometimes-enrich-it',
        'md5': '03582d795382e49f2fd0b427b55de409',
        'info_dict': {
            'id': '759576926',
            'ext': 'mp4',
            'title': 'Wrought',
            'thumbnail': 'https://i.vimeocdn.com/video/1525599692-84614af88e446612f49ca966cf8f80eab2c73376bedd80555741c521c26f9a3e-d_1280',
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/discogs.py (Python)

Code before:
from .common import InfoExtractor


class DiscogsReleasePlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>(release|master))/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        mobj = self._match_valid_url(url)
        playlist_id, playlist_type = mobj.group('id', 'type')

        api = f'https://api.discogs.com/{playlist_type}s/{playlist_id}'
        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(api, display_id)

        playlist_title = response.get('title')
        entries = [
            self.url_result(video.get('uri'), video_title=video.get('title'))
            for video in response.get('videos')
        ]

        return self.playlist_result(entries, display_id, playlist_title)


Review comment (suggestion):
```suggestion
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')
```

Code after:
from .common import InfoExtractor
from .youtube import YoutubeIE
from ..utils import traverse_obj


class DiscogsReleasePlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>release|master)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')

        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(
            f'https://api.discogs.com/{playlist_type}s/{playlist_id}', display_id)

        entries = [
            self.url_result(video['uri'], YoutubeIE, video_title=video.get('title'))
            for video in traverse_obj(response, ('videos', lambda _, v: YoutubeIE.suitable(v['uri'])))]

        return self.playlist_result(entries, display_id, response.get('title'))
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/discogs.py (Python)

Code before:

class DiscogsReleasePlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>(release|master))/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        mobj = self._match_valid_url(url)
        playlist_id, playlist_type = mobj.group('id', 'type')

        api = f'https://api.discogs.com/{playlist_type}s/{playlist_id}'
        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(api, display_id)

        playlist_title = response.get('title')
        entries = [
            self.url_result(video.get('uri'), video_title=video.get('title'))
            for video in response.get('videos')
        ]

        return self.playlist_result(entries, display_id, playlist_title)


Review comment (suggestion):
```suggestion
        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(
            f'https://api.discogs.com/{playlist_type}s/{playlist_id}', display_id)
```

Code after:
from ..utils import traverse_obj


class DiscogsReleasePlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>release|master)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')

        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(
            f'https://api.discogs.com/{playlist_type}s/{playlist_id}', display_id)

        entries = [
            self.url_result(video['uri'], YoutubeIE, video_title=video.get('title'))
            for video in traverse_obj(response, ('videos', lambda _, v: YoutubeIE.suitable(v['uri'])))]

        return self.playlist_result(entries, display_id, response.get('title'))
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/discogs.py (Python)

Code before:
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        mobj = self._match_valid_url(url)
        playlist_id, playlist_type = mobj.group('id', 'type')

        api = f'https://api.discogs.com/{playlist_type}s/{playlist_id}'
        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(api, display_id)

        playlist_title = response.get('title')
        entries = [
            self.url_result(video.get('uri'), video_title=video.get('title'))
            for video in response.get('videos')
        ]

        return self.playlist_result(entries, display_id, playlist_title)


Review comment (suggestion):
This traversal filter would ensure we only try to loop through a list, and if the list is not empty, `video` will always be a dict with a `uri` key.

(you'll need to import `traverse_obj`)
```suggestion
        entries = [
            self.url_result(video['uri'], video_title=video.get('title'))
            for video in traverse_obj(response, ('videos', lambda _, v: v['uri']))]

        return self.playlist_result(entries, display_id, response.get('title'))
```

Code after:
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')

        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(
            f'https://api.discogs.com/{playlist_type}s/{playlist_id}', display_id)

        entries = [
            self.url_result(video['uri'], YoutubeIE, video_title=video.get('title'))
            for video in traverse_obj(response, ('videos', lambda _, v: YoutubeIE.suitable(v['uri'])))]

        return self.playlist_result(entries, display_id, response.get('title'))
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/discogs.py (Python)

Code before:
from .common import InfoExtractor
from ..utils import traverse_obj


class DiscogsReleasePlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>(release|master))/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')

        display_id = f'{playlist_type}{playlist_id}'

Review comment (refactor):
```suggestion
from .common import InfoExtractor
from .youtube import YoutubeIE
```

Code after:
from .common import InfoExtractor
from .youtube import YoutubeIE
from ..utils import traverse_obj


class DiscogsReleasePlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>release|master)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/discogs.py (Python)

Code before:
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')

        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(
            f'https://api.discogs.com/{playlist_type}s/{playlist_id}', display_id)

        entries = [
            self.url_result(video['uri'], video_title=video.get('title'))
            for video in traverse_obj(response, ('videos', lambda _, v: v['uri']))]

        return self.playlist_result(entries, display_id, response.get('title'))


Review comment (suggestion):
```suggestion
        entries = [
            self.url_result(video['uri'], YoutubeIE, video_title=video.get('title'))
            for video in traverse_obj(response, ('videos', lambda _, v: YoutubeIE.suitable(v['uri'])))]
```

Code after:
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>release|master)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')

        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(
            f'https://api.discogs.com/{playlist_type}s/{playlist_id}', display_id)

        entries = [
            self.url_result(video['uri'], YoutubeIE, video_title=video.get('title'))
            for video in traverse_obj(response, ('videos', lambda _, v: YoutubeIE.suitable(v['uri'])))]

        return self.playlist_result(entries, display_id, response.get('title'))
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/discogs.py (Python)

Code before:
from .common import InfoExtractor
from .youtube import YoutubeIE
from ..utils import traverse_obj


class DiscogsReleasePlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>(release|master))/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')

        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(
            f'https://api.discogs.com/{playlist_type}s/{playlist_id}', display_id)

        entries = [
            self.url_result(video['uri'], YoutubeIE, video_title=video.get('title'))

Review comment (suggestion):
```suggestion
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>release|master)/(?P<id>\d+)'
```

Code after:
from .common import InfoExtractor
from .youtube import YoutubeIE
from ..utils import traverse_obj


class DiscogsReleasePlaylistIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?discogs\.com/(?P<type>release|master)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.discogs.com/release/1-The-Persuader-Stockholm',
        'info_dict': {
            'id': 'release1',
            'title': 'Stockholm',
        },
        'playlist_mincount': 7,
    }, {
        'url': 'https://www.discogs.com/master/113-Vince-Watson-Moments-In-Time',
        'info_dict': {
            'id': 'master113',
            'title': 'Moments In Time',
        },
        'playlist_mincount': 53,
    }]

    def _real_extract(self, url):
        playlist_id, playlist_type = self._match_valid_url(url).group('id', 'type')

        display_id = f'{playlist_type}{playlist_id}'
        response = self._download_json(
            f'https://api.discogs.com/{playlist_type}s/{playlist_id}', display_id)

        entries = [
            self.url_result(video['uri'], YoutubeIE, video_title=video.get('title'))
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/cbc.py (Python)

Code before:

        # Two types of metadata JSON
        if not video_info.get('formattedIdMedia'):
            video_info = traverse_obj(
                video_info, ('freeTv', 'items', lambda _, v: v['key'] == video_key, {dict}),
                get_all=False, default={})

        video_stream_id = video_info.get('formattedIdMedia')
        if not video_stream_id:
            raise ExtractorError('Couldn\'t find video metadata, maybe this livestream is now offline', expected=True)

        stream_data = self._download_json(
            'https://services.radio-canada.ca/media/validation/v2/', video_key, query={
                'appCode': 'mpx',
                'connectionType': 'hd',
                'deviceType': 'ipad',
                'idMedia': video_stream_id,
                'multibitrate': 'true',
                'output': 'json',
                'tech': 'hls',
                'manifestType': 'desktop',
            })

        return {
            'id': video_id,
            'formats': self._extract_m3u8_formats(stream_data['url'], video_key, 'mp4', live=True),
            'is_live': True,
            **traverse_obj(video_info, {
                'title': 'title',
                'description': 'description',
                'thumbnail': ('images', 'card', 'url'),
                'timestamp': ('airDate', {parse_iso8601}),
            })
        }


Review comment (refactor):
```suggestion
            'formats': self._extract_m3u8_formats(stream_data['url'], video_id, 'mp4', live=True),
```

Code after:
        video_info = self._search_nextjs_data(webpage, video_id)['props']['pageProps']['data']

        # Two types of metadata JSON
        if not video_info.get('formattedIdMedia'):
            video_info = traverse_obj(
                video_info, (('freeTv', ('streams', ...)), 'items', lambda _, v: v['key'] == video_id, {dict}),
                get_all=False, default={})

        video_stream_id = video_info.get('formattedIdMedia')
        if not video_stream_id:
            raise ExtractorError('Couldn\'t find video metadata, maybe this livestream is now offline', expected=True)

        stream_data = self._download_json(
            'https://services.radio-canada.ca/media/validation/v2/', video_id, query={
                'appCode': 'mpx',
                'connectionType': 'hd',
                'deviceType': 'ipad',
                'idMedia': video_stream_id,
                'multibitrate': 'true',
                'output': 'json',
                'tech': 'hls',
                'manifestType': 'desktop',
            })

        return {
            'id': video_id,
            'formats': self._extract_m3u8_formats(stream_data['url'], video_id, 'mp4', live=True),
            'is_live': True,
            **traverse_obj(video_info, {
                'title': 'title',
                'description': 'description',
                'thumbnail': ('images', 'card', 'url'),
                'timestamp': ('airDate', {parse_iso8601}),
            })
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/brilliantpala.py (Python)

Code before:
import hashlib

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    traverse_obj,
    urlencode_postdata,
)


class BrilliantpalaIE(InfoExtractor):
    _NETRC_MACHINE = 'brilliantpala'
    _DOMAIN = '{subdomain}.brilliantpala.org'

    def _initialize_pre_login(self):
        self._HOMEPAGE = f'https://{self._DOMAIN}'
        self._LOGIN_API = f'{self._HOMEPAGE}/login/'
        self._LOGOUT_DEVICES_API = f'{self._HOMEPAGE}/logout_devices/?next=/'
        self._CONTENT_API = f'{self._HOMEPAGE}/api/v2.4/contents/{{content_id}}/'
        self._HLS_AES_URI = f'{self._HOMEPAGE}/api/v2.5/video_contents/{{content_id}}/key/'

    def _get_logged_in_username(self, url, video_id):
        webpage, urlh = self._download_webpage_handle(url, video_id)
        if self._LOGIN_API == urlh.url:
            self.raise_login_required()
        return self._html_search_regex(
            r'"username"\s*:\s*"(?P<username>[^"]+)"', webpage, 'stream page info', 'username')

    def _perform_login(self, username, password):
        login_form = self._hidden_inputs(self._download_webpage(
            self._LOGIN_API, None, 'Downloading login page'))
        login_form.update({
            'username': username,
            'password': password,
        })
        self._set_cookie(self._DOMAIN, 'csrftoken', login_form['csrfmiddlewaretoken'])

Review comment (refactor):
```suggestion
class BrilliantpalaBaseIE(InfoExtractor):
```

Code after:
import hashlib

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    traverse_obj,
    urlencode_postdata,
)


class BrilliantpalaBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'brilliantpala'
    _DOMAIN = '{subdomain}.brilliantpala.org'

    def _initialize_pre_login(self):
        self._HOMEPAGE = f'https://{self._DOMAIN}'
        self._LOGIN_API = f'{self._HOMEPAGE}/login/'
        self._LOGOUT_DEVICES_API = f'{self._HOMEPAGE}/logout_devices/?next=/'
        self._CONTENT_API = f'{self._HOMEPAGE}/api/v2.4/contents/{{content_id}}/'
        self._HLS_AES_URI = f'{self._HOMEPAGE}/api/v2.5/video_contents/{{content_id}}/key/'

    def _get_logged_in_username(self, url, video_id):
        webpage, urlh = self._download_webpage_handle(url, video_id)
        if self._LOGIN_API == urlh.url:
            self.raise_login_required()
        return self._html_search_regex(
            r'"username"\s*:\s*"(?P<username>[^"]+)"', webpage, 'stream page info', 'username')

    def _perform_login(self, username, password):
        login_form = self._hidden_inputs(self._download_webpage(
            self._LOGIN_API, None, 'Downloading login page'))
        login_form.update({
            'username': username,
            'password': password,
        })
        self._set_cookie(self._DOMAIN, 'csrftoken', login_form['csrfmiddlewaretoken'])