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

Code before:
import math
import urllib.parse

from .common import InfoExtractor
from ..utils import (
    InAdvancePagedList,
    determine_ext,
    format_field,
    int_or_none,
    traverse_obj,
    unified_timestamp,
    url_or_none,
)


class BanByeBaseIE(InfoExtractor):
    _API_BASE = 'https://api.banbye.com'
    _CDN_BASE = 'https://cdn.banbye.com'
    _VIDEO_BASE = 'https://banbye.com/watch'

    @staticmethod
    def _extract_playlist_id(url, param='playlist'):
        return urllib.parse.parse_qs(
            urllib.parse.urlparse(url).query).get(param, [None])[0]

    def _extract_playlist(self, playlist_id):
        data = self._download_json(f'{self._API_BASE}/playlists/{playlist_id}', playlist_id)
        return self.playlist_result([
            self.url_result(f'{self._VIDEO_BASE}/{video_id}', BanByeIE)
            for video_id in data['videoIds']], playlist_id, data.get('name'))


class BanByeIE(BanByeBaseIE):
    _VALID_URL = r'https?://(?:www\.)?banbye\.com/(?:en/)?watch/(?P<id>[\w-]+)'
    _TESTS = [{

Review comment (suggestion):
if applying my formats suggestion, we'll need `join_nonempty`:
```suggestion
    format_field,
    int_or_none,
    join_nonempty,
    traverse_obj,
```

Code after:
import math
import urllib.parse

from .common import InfoExtractor
from ..utils import (
    InAdvancePagedList,
    determine_ext,
    format_field,
    int_or_none,
    join_nonempty,
    traverse_obj,
    unified_timestamp,
    url_or_none,
)


class BanByeBaseIE(InfoExtractor):
    _API_BASE = 'https://api.banbye.com'
    _CDN_BASE = 'https://cdn.banbye.com'
    _VIDEO_BASE = 'https://banbye.com/watch'

    @staticmethod
    def _extract_playlist_id(url, param='playlist'):
        return urllib.parse.parse_qs(
            urllib.parse.urlparse(url).query).get(param, [None])[0]

    def _extract_playlist(self, playlist_id):
        data = self._download_json(f'{self._API_BASE}/playlists/{playlist_id}', playlist_id)
        return self.playlist_result([
            self.url_result(f'{self._VIDEO_BASE}/{video_id}', BanByeIE)
            for video_id in data['videoIds']], playlist_id, data.get('name'))


class BanByeIE(BanByeBaseIE):
    _VALID_URL = r'https?://(?:www\.)?banbye\.com/(?:en/)?watch/(?P<id>[\w-]+)'
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/rtve.py (Python)

Code before:
import base64
import io
import struct

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    determine_ext,
    float_or_none,
    qualities,
    try_get,
)


class RTVEALaCartaIE(InfoExtractor):
    IE_NAME = 'rtve.es:alacarta'
    IE_DESC = 'RTVE a la carta and Play'
    _VALID_URL = [
        r'https?://(?:www\.)?rtve\.es/(m/)?(alacarta/videos|filmoteca)/[^/]+/[^/]+/(?P<id>\d+)',
        r'https?://(?:www\.)?rtve\.es/(m/)?play/videos/[^/]+/[^/]+/(?P<id>\d+)',
    ]

    _TESTS = [{
        'url': 'http://www.rtve.es/alacarta/videos/la-aventura-del-saber/aventuraentornosilla/3088905/',
        'md5': 'a964547824359a5753aef09d79fe984b',
        'info_dict': {
            'id': '3088905',
            'ext': 'mp4',
            'title': 'En torno a la silla',
            'duration': 1216.981,
            'series': 'La aventura del Saber',
            'thumbnail': 'https://img2.rtve.es/v/aventuraentornosilla_3088905.png',
        },
    }, {
        'note': 'Live stream',
        'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
        'info_dict': {
            'id': '1694255',
            'ext': 'mp4',
            'title': 're:^24H LIVE [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
            'is_live': True,
            'live_status': 'is_live',
            'thumbnail': r're:https://img2\.rtve\.es/v/.*\.png',
        },
        'params': {
            'skip_download': 'live stream',

Review comment (suggestion):
Not sure why we need a separate regex
```suggestion
    _VALID_URL = r'https?://(?:www\.)?rtve\.es/(m/)?(?:(?:alacarta|play)/videos|filmoteca)/[^/?#]+/[^/?#]+/(?P<id>\d+)'
```

Code after:
import base64
import io
import struct
import urllib.parse

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    clean_html,
    determine_ext,
    float_or_none,
    make_archive_id,
    parse_iso8601,
    qualities,
    url_or_none,
)
from ..utils.traversal import subs_list_to_dict, traverse_obj


class RTVEBaseIE(InfoExtractor):
    # Reimplementation of https://js2.rtve.es/pages/app-player/3.5.1/js/pf_video.js
    @staticmethod
    def _decrypt_url(png):
        encrypted_data = io.BytesIO(base64.b64decode(png)[8:])
        while True:
            length_data = encrypted_data.read(4)
            length = struct.unpack('!I', length_data)[0]
            chunk_type = encrypted_data.read(4)
            if chunk_type == b'IEND':
                break
            data = encrypted_data.read(length)
            if chunk_type == b'tEXt':
                data = bytes(filter(None, data))
                alphabet_data, _, url_data = data.partition(b'#')
                quality_str, _, url_data = url_data.rpartition(b'%%')
                quality_str = quality_str.decode() or ''
                alphabet = RTVEBaseIE._get_alphabet(alphabet_data)
                url = RTVEBaseIE._get_url(alphabet, url_data)
                yield quality_str, url
            encrypted_data.read(4)  # CRC

    @staticmethod
    def _get_url(alphabet, url_data):
        url = ''
        f = 0
        e = 3
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/picarto.py (Python)

Code before:
        'only_matching': True,
    }]

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

        data = self._download_json(
            'https://ptvintern.picarto.tv/ptvapi', video_id, query={
                'query': f'''{{
  video(id: "{video_id}") {{
    id
    title
    adult
    file_name
    video_recording_image_url
    channel {{
      name
    }}
  }}
}}''',
            },
            headers={
                'Accept': '*/*',
                'Content-Type': 'application/json',
            },
        )['data']['video']

        file_name = data['file_name']
        netloc = urllib.parse.urlparse(data['video_recording_image_url']).netloc

        formats = self._extract_m3u8_formats(
            f'https://{netloc}/stream/hls/{file_name}/index.m3u8', video_id, 'mp4', m3u8_id='hls')

        return {
            'id': video_id,
            **traverse_obj(data, {
                'id': ('id', {str_or_none}),
                'title': ('title', {str}),
                'thumbnail': 'video_recording_image_url',
                'channel': ('channel', 'name', {str}),
                'age_limit': ('adult', {lambda x: 18 if x else 0}),
            }),
            'formats': formats,
        }


Review comment (suggestion):
```suggestion
            }, headers={'Accept': '*/*', 'Content-Type': 'application/json'})['data']['video']
```

Code after:
        data = self._download_json(
            'https://ptvintern.picarto.tv/ptvapi', video_id, query={
                'query': f'''{{
  video(id: "{video_id}") {{
    id
    title
    adult
    file_name
    video_recording_image_url
    channel {{
      name
    }}
  }}
}}''',
            }, headers={'Accept': '*/*', 'Content-Type': 'application/json'})['data']['video']

        file_name = data['file_name']
        netloc = urllib.parse.urlparse(data['video_recording_image_url']).netloc

        formats = self._extract_m3u8_formats(
            f'https://{netloc}/stream/hls/{file_name}/index.m3u8', video_id, 'mp4', m3u8_id='hls')

        return {
            'id': video_id,
            **traverse_obj(data, {
                'id': ('id', {str_or_none}),
                'title': ('title', {str}),
                'thumbnail': 'video_recording_image_url',
                'channel': ('channel', 'name', {str}),
                'age_limit': ('adult', {lambda x: 18 if x else 0}),
            }),
            'formats': formats,
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/epidemicsound.py (Python)

Code before:
import re

from .common import InfoExtractor
from ..utils import (
    float_or_none,
    int_or_none,
    orderedSet,
    parse_iso8601,
    parse_qs,
    parse_resolution,
    str_or_none,
    traverse_obj,
    url_or_none,
)


class EpidemicSoundIE(InfoExtractor):
    # set an attribute to indicate if it's a sound effect or a track based on this regex (r'https?://(?:www\.)?epidemicsound\.com/sound-effects/tracks/(?P<id>[0-9a-zA-Z-]+)')
    _IS_SFX = False
    _SFX_REGEX = re.compile(r'https?://(?:www\.)?epidemicsound\.com/sound-effects/tracks/(?P<id>[0-9a-zA-Z-]+)')
    _VALID_URL = r'https?://(?:www\.)?epidemicsound\.com/(?:sound-effects/tracks|track)/(?P<id>[0-9a-zA-Z-]+)'
    _TESTS = [{
        'url': 'https://www.epidemicsound.com/track/yFfQVRpSPz/',
        'md5': 'd98ff2ddb49e8acab9716541cbc9dfac',
        'info_dict': {
            'id': '45014',
            'display_id': 'yFfQVRpSPz',
            'ext': 'mp3',

Review comment (refactor):
```suggestion
from .common import InfoExtractor
```

&nbsp;

and also import `join_nonempty` from utils below

Code after:
from .common import InfoExtractor
from ..utils import (
    float_or_none,
    int_or_none,
    join_nonempty,
    orderedSet,
    parse_iso8601,
    parse_qs,
    parse_resolution,
    str_or_none,
    traverse_obj,
    url_or_none,
)


class EpidemicSoundIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?epidemicsound\.com/(?:(?P<sfx>sound-effects/tracks)|track)/(?P<id>[0-9a-zA-Z-]+)'
    _TESTS = [{
        'url': 'https://www.epidemicsound.com/track/yFfQVRpSPz/',
        'md5': 'd98ff2ddb49e8acab9716541cbc9dfac',
        'info_dict': {
            'id': '45014',
            'display_id': 'yFfQVRpSPz',
            'ext': 'mp3',
            'title': 'Door Knock Door 1',
            'alt_title': 'Door Knock Door 1',
            'tags': ['foley', 'door', 'knock', 'glass', 'window', 'glass door knock'],
            'categories': ['Misc. Door'],
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/nzz.py (Python)

Code before:
import re

from .common import InfoExtractor


class NZZIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?nzz\.ch/(?:[^/]+/)*[^/?#]+-ld\.(?P<id>\d+)'
    _TESTS = [{
        'url': 'http://www.nzz.ch/zuerich/gymizyte/gymizyte-schreiben-schueler-heute-noch-diktate-ld.9153',
        'info_dict': {
            'id': '9153',
        },
        'playlist_mincount': 6,
    }, {
        'url': 'https://www.nzz.ch/video/nzz-standpunkte/cvp-auf-der-suche-nach-dem-mass-der-mitte-ld.1368112',
        'info_dict': {
            'id': '1368112',
        },
        'playlist_count': 1,
    }]

    def _entries(self, webpage, page_id):
        for script in re.findall(r'(?s)<script[^>]+data-hid="jw-video-jw[^>]+>(.*?)</script>', webpage):
            settings = self._search_json(r'var\s+settings\s*=[^{]*', script, 'settings', page_id, fatal=False)
            if entry := self._parse_jwplayer_data(settings, page_id):
                yield entry

    def _real_extract(self, url):
        page_id = self._match_id(url)
        webpage = self._download_webpage(url, page_id)

        return self.playlist_result(
            self._entries(webpage, page_id), page_id)


Review comment (suggestion):
```suggestion
        for script in re.findall(r'(?s)<script[^>]* data-hid="jw-video-jw[^>]+>(.+?)</script>', webpage):
```

Code after:
import re

from .common import InfoExtractor


class NZZIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?nzz\.ch/(?:[^/]+/)*[^/?#]+-ld\.(?P<id>\d+)'
    _TESTS = [{
        'url': 'http://www.nzz.ch/zuerich/gymizyte/gymizyte-schreiben-schueler-heute-noch-diktate-ld.9153',
        'info_dict': {
            'id': '9153',
        },
        'playlist_mincount': 6,
    }, {
        'url': 'https://www.nzz.ch/video/nzz-standpunkte/cvp-auf-der-suche-nach-dem-mass-der-mitte-ld.1368112',
        'info_dict': {
            'id': '1368112',
        },
        'playlist_count': 1,
    }]

    def _entries(self, webpage, page_id):
        for script in re.findall(r'(?s)<script[^>]* data-hid="jw-video-jw[^>]+>(.+?)</script>', webpage):
            settings = self._search_json(r'var\s+settings\s*=[^{]*', script, 'settings', page_id, fatal=False)
            if entry := self._parse_jwplayer_data(settings, page_id):
                yield entry

    def _real_extract(self, url):
        page_id = self._match_id(url)
        webpage = self._download_webpage(url, page_id)

        return self.playlist_result(self._entries(webpage, page_id), page_id)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/nzz.py (Python)

Code before:
    _TESTS = [{
        'url': 'http://www.nzz.ch/zuerich/gymizyte/gymizyte-schreiben-schueler-heute-noch-diktate-ld.9153',
        'info_dict': {
            'id': '9153',
        },
        'playlist_mincount': 6,
    }, {
        'url': 'https://www.nzz.ch/video/nzz-standpunkte/cvp-auf-der-suche-nach-dem-mass-der-mitte-ld.1368112',
        'info_dict': {
            'id': '1368112',
        },
        'playlist_count': 1,
    }]

    def _entries(self, webpage, page_id):
        for script in re.findall(r'(?s)<script[^>]+data-hid="jw-video-jw[^>]+>(.*?)</script>', webpage):
            settings = self._search_json(r'var\s+settings\s*=[^{]*', script, 'settings', page_id, fatal=False)
            if entry := self._parse_jwplayer_data(settings, page_id):
                yield entry

    def _real_extract(self, url):
        page_id = self._match_id(url)
        webpage = self._download_webpage(url, page_id)

        return self.playlist_result(
            self._entries(webpage, page_id), page_id)


Review comment (suggestion):
```suggestion
        return self.playlist_result(self._entries(webpage, page_id), page_id)
```

Code after:
    _TESTS = [{
        'url': 'http://www.nzz.ch/zuerich/gymizyte/gymizyte-schreiben-schueler-heute-noch-diktate-ld.9153',
        'info_dict': {
            'id': '9153',
        },
        'playlist_mincount': 6,
    }, {
        'url': 'https://www.nzz.ch/video/nzz-standpunkte/cvp-auf-der-suche-nach-dem-mass-der-mitte-ld.1368112',
        'info_dict': {
            'id': '1368112',
        },
        'playlist_count': 1,
    }]

    def _entries(self, webpage, page_id):
        for script in re.findall(r'(?s)<script[^>]* data-hid="jw-video-jw[^>]+>(.+?)</script>', webpage):
            settings = self._search_json(r'var\s+settings\s*=[^{]*', script, 'settings', page_id, fatal=False)
            if entry := self._parse_jwplayer_data(settings, page_id):
                yield entry

    def _real_extract(self, url):
        page_id = self._match_id(url)
        webpage = self._download_webpage(url, page_id)

        return self.playlist_result(self._entries(webpage, page_id), page_id)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/mlb.py (Python)

Code before:
        url
      }
    }
    id
    timestamp
    title
  }
}''' % display_id,  # noqa: UP031
            })['data']['mediaPlayback'][0]


class MLBTVIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?mlb\.com/tv/g(?P<id>\d{6})'
    _NETRC_MACHINE = 'mlb'
    _TESTS = [{
        'url': 'https://www.mlb.com/tv/g661581/vee2eff5f-a7df-4c20-bdb4-7b926fa12638',
        'info_dict': {
            'id': '661581',
            'ext': 'mp4',
            'title': '2022-07-02 - St. Louis Cardinals @ Philadelphia Phillies',
        },
        'params': {
            'skip_download': True,
        },
    }]
    _GRAPHQL_INIT_QUERY = '''mutation initSession($device: InitSessionInput!, $clientType: ClientType!, $experience: ExperienceTypeInput) {
    initSession(device: $device, clientType: $clientType, experience: $experience) {
        deviceId
        sessionId
        entitlements {
            code
        }
        location {
            countryCode
            regionName
            zipCode
            latitude
            longitude
        }
        clientExperience
        features
    }
  }'''
    _GRAPHQL_PLAYBACK_QUERY = '''mutation initPlaybackSession(
        $adCapabilities: [AdExperienceType]
        $mediaId: String!
        $deviceId: String!
        $sessionId: String!
        $quality: PlaybackQuality
    ) {
        initPlaybackSession(

Review comment (suggestion):
i think this is easier to read with 
```suggestion
    _GRAPHQL_INIT_QUERY = '''\
mutation initSession($device: InitSessionInput!, $clientType: ClientType!, $experience: ExperienceTypeInput) {
```

Code after:
      }
      playbacks {
        name
        url
      }
    }
    id
    timestamp
    title
  }
}''' % display_id,  # noqa: UP031
            })['data']['mediaPlayback'][0]


class MLBTVIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?mlb\.com/tv/g(?P<id>\d{6})'
    _NETRC_MACHINE = 'mlb'
    _TESTS = [{
        'url': 'https://www.mlb.com/tv/g661581/vee2eff5f-a7df-4c20-bdb4-7b926fa12638',
        'info_dict': {
            'id': '661581',
            'ext': 'mp4',
            'title': '2022-07-02 - St. Louis Cardinals @ Philadelphia Phillies',
            'release_date': '20220702',
            'release_timestamp': 1656792300,
        },
        'params': {
            'skip_download': True,
        },
    }]
    _GRAPHQL_INIT_QUERY = '''\
mutation initSession($device: InitSessionInput!, $clientType: ClientType!, $experience: ExperienceTypeInput) {
    initSession(device: $device, clientType: $clientType, experience: $experience) {
        deviceId
        sessionId
        entitlements {
            code
        }
        location {
            countryCode
            regionName
            zipCode
            latitude
            longitude
        }
        clientExperience
        features
    }
  }'''
    _GRAPHQL_PLAYBACK_QUERY = '''\
mutation initPlaybackSession(
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/germanupa.py (Python)

Code before:
            'duration': 2146,
            'release_timestamp': 1721373980,
            'uploader': 'German UPA',
            'uploader_id': 'germanupa',
            'upload_date': '20240719',
            'comment_count': int,
        },
        'expected_warnings': ['Failed to parse XML: not well-formed'],
        'skip': 'login required',
    }]
    _IFRAME_RE = r'<iframe[^>]+data-src\s*?=\s*?([\'"])(?P<url>https://germanupa\.de/media/oembed\?url=(?:(?!\1).)+)\1'
    _LOGIN_REQUIRED_RE = r'<div[^>]+class\s*?=\s*?([\'"])(?:(?!\1).)*login-wrapper(?:(?!\1).)*\1'

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)

        embed_url = self._search_regex(self._IFRAME_RE, webpage, 'embed video', None, group='url')
        if not embed_url:
            return self.url_result(url, 'Generic')  # Fall back to generic to extract audio

        param_url = traverse_obj(parse_qs(embed_url), ('url', 0, {url_or_none}))
        if not param_url:
            if self._search_regex(self._LOGIN_REQUIRED_RE, webpage, 'login wrapper', default=None):
                self.raise_login_required('This video is only available for members')
            raise ExtractorError('Failed to extract URL from the query parameters', video_id=video_id)

        real_url = param_url.replace('https://vimeo.com/', 'https://player.vimeo.com/video/')
        return self.url_result(VimeoIE._smuggle_referrer(real_url, url), VimeoIE, video_id)


Review comment (bug):
No need to pass the video id
```suggestion
            raise ExtractorError('Failed to extract URL from the query parameters')
```

Code after:
            'release_timestamp': 1721373980,
            'uploader': 'German UPA',
            'uploader_id': 'germanupa',
            'upload_date': '20240719',
            'comment_count': int,
        },
        'expected_warnings': ['Failed to parse XML: not well-formed'],
        'skip': 'login required',
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)

        param_url = traverse_obj(
            self._search_regex(
                r'<iframe[^>]+data-src\s*?=\s*?([\'"])(?P<url>https://germanupa\.de/media/oembed\?url=(?:(?!\1).)+)\1',
                webpage, 'embedded video', default=None, group='url'),
            ({parse_qs}, 'url', 0, {url_or_none}))

        if not param_url:
            if self._search_regex(
                    r'<div[^>]+class\s*?=\s*?([\'"])(?:(?!\1).)*login-wrapper(?:(?!\1).)*\1',
                    webpage, 'login wrapper', default=None):
                self.raise_login_required('This video is only available for members')
            return self.url_result(url, 'Generic')  # Fall back to generic to extract audio

        real_url = param_url.replace('https://vimeo.com/', 'https://player.vimeo.com/video/')
        return self.url_result(VimeoIE._smuggle_referrer(real_url, url), VimeoIE, video_id)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/kick.py (Python)

Code before:
from yt_dlp.utils._utils import unified_strdate

from .common import InfoExtractor
from ..networking import HEADRequest
from ..utils import (
    UserNotLive,
    determine_ext,
    float_or_none,
    int_or_none,
    merge_dicts,
    traverse_obj,
    unified_timestamp,
    url_or_none,
)

# Routes ----------------------

# Clips
# https://kick.com/mxddy?clip=clip_01GYXVB5Y8PWAPWCWMSBCFB05X
# https://kick.com/api/v2/clips/clip_01GYXVB5Y8PWAPWCWMSBCFB05X

# Livestream
# https://kick.com/xqc
# https://kick.com/api/v2/channels/xqc

# VODs
# https://kick.com/video/e74614f4-5270-4319-90ad-32179f19a45c
# https://kick.com/api/v1/video/e74614f4-5270-4319-90ad-32179f19a45c

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

Code after:
import functools

from .common import InfoExtractor
from ..networking import HEADRequest
from ..utils import (
    UserNotLive,
    determine_ext,
    float_or_none,
    int_or_none,
    merge_dicts,
    parse_iso8601,
    str_or_none,
    traverse_obj,
    unified_timestamp,
    url_or_none,
)


class KickBaseIE(InfoExtractor):
    def _real_initialize(self):
        self._request_webpage(
            HEADRequest('https://kick.com/'), None, 'Setting up session', fatal=False, impersonate=True)
        xsrf_token = self._get_cookies('https://kick.com/').get('XSRF-TOKEN')
        if not xsrf_token:
            self.write_debug('kick.com did not set XSRF-TOKEN cookie')
        KickBaseIE._API_HEADERS = {
            'Authorization': f'Bearer {xsrf_token.value}',
            'X-XSRF-TOKEN': xsrf_token.value,