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

Code before:
import datetime as dt
import functools

from .common import InfoExtractor
from ..networking import Request
from ..utils import (
    ExtractorError,
    OnDemandPagedList,
    UserNotLive,
    determine_ext,
    filter_dict,
    int_or_none,
    orderedSet,
    parse_iso8601,
    url_or_none,
    urlencode_postdata,
    urljoin,
)
from ..utils.traversal import traverse_obj


def _cloudfront_auth_request(m3u8_url, strm_id, video_id, url):
    post_data = {
        'type': 'vod',
        'strm_id': strm_id,
        'title_no': video_id,
        'url': m3u8_url,
    }
    return Request(
        'https://live.sooplive.co.kr/api/private_auth.php',
        method='POST',
        headers={
            'Content-Type': 'application/x-www-form-urlencoded',
            'Referer': url,
            'Origin': 'https://vod.sooplive.co.kr',
        },
        data=urlencode_postdata(post_data))


class AfreecaTVBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'afreecatv'

    def _perform_login(self, username, password):
        login_form = {
            'szWork': 'login',
            'szType': 'json',
            'szUid': username,

Review comment (security):
To avoid confusion let's be specific with both URL var names
```suggestion
def _cloudfront_auth_request(m3u8_url, strm_id, video_id, referer_url):
```

Code after:
import datetime as dt
import functools
import time

from .common import InfoExtractor
from ..networking import Request
from ..utils import (
    ExtractorError,
    OnDemandPagedList,
    UserNotLive,
    determine_ext,
    filter_dict,
    int_or_none,
    orderedSet,
    parse_iso8601,
    url_or_none,
    urlencode_postdata,
    urljoin,
)
from ..utils.traversal import require, traverse_obj


def _cloudfront_auth_request(m3u8_url, strm_id, video_id, referer_url):
    return Request(
        'https://live.sooplive.co.kr/api/private_auth.php',
        method='POST',
        headers={
            'Referer': referer_url,
            'Origin': 'https://vod.sooplive.co.kr',
        },
        data=urlencode_postdata({
            'type': 'vod',
            'strm_id': strm_id,
            'title_no': video_id,
            'url': m3u8_url,
        }))


class AfreecaTVBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'afreecatv'

    def _perform_login(self, username, password):
        login_form = {
            'szWork': 'login',
            'szType': 'json',
            'szUid': username,
            'szPassword': password,
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/afreecatv.py (Python)

Code before:
    UserNotLive,
    determine_ext,
    filter_dict,
    int_or_none,
    orderedSet,
    parse_iso8601,
    url_or_none,
    urlencode_postdata,
    urljoin,
)
from ..utils.traversal import traverse_obj


def _cloudfront_auth_request(m3u8_url, strm_id, video_id, url):
    post_data = {
        'type': 'vod',
        'strm_id': strm_id,
        'title_no': video_id,
        'url': m3u8_url,
    }
    return Request(
        'https://live.sooplive.co.kr/api/private_auth.php',
        method='POST',
        headers={
            'Content-Type': 'application/x-www-form-urlencoded',
            'Referer': url,
            'Origin': 'https://vod.sooplive.co.kr',
        },
        data=urlencode_postdata(post_data))


class AfreecaTVBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'afreecatv'

    def _perform_login(self, username, password):
        login_form = {
            'szWork': 'login',
            'szType': 'json',
            'szUid': username,
            'szPassword': password,
            'isSaveId': 'false',
            'szScriptVar': 'oLoginRet',
            'szAction': '',
        }

        response = self._download_json(
            'https://login.sooplive.co.kr/app/LoginAction.php', None,
            'Logging in', data=urlencode_postdata(login_form))

        _ERRORS = {
            -4: 'Your account has been suspended due to a violation of our terms and policies.',

Review comment (suggestion):
```suggestion
            'Referer': referer_url,
```

Code after:
    OnDemandPagedList,
    UserNotLive,
    determine_ext,
    filter_dict,
    int_or_none,
    orderedSet,
    parse_iso8601,
    url_or_none,
    urlencode_postdata,
    urljoin,
)
from ..utils.traversal import require, traverse_obj


def _cloudfront_auth_request(m3u8_url, strm_id, video_id, referer_url):
    return Request(
        'https://live.sooplive.co.kr/api/private_auth.php',
        method='POST',
        headers={
            'Referer': referer_url,
            'Origin': 'https://vod.sooplive.co.kr',
        },
        data=urlencode_postdata({
            'type': 'vod',
            'strm_id': strm_id,
            'title_no': video_id,
            'url': m3u8_url,
        }))


class AfreecaTVBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'afreecatv'

    def _perform_login(self, username, password):
        login_form = {
            'szWork': 'login',
            'szType': 'json',
            'szUid': username,
            'szPassword': password,
            'isSaveId': 'false',
            'szScriptVar': 'oLoginRet',
            'szAction': '',
        }

        response = self._download_json(
            'https://login.sooplive.co.kr/app/LoginAction.php', None,
            'Logging in', data=urlencode_postdata(login_form))

        _ERRORS = {
            -4: 'Your account has been suspended due to a violation of our terms and policies.',
            -5: 'https://member.sooplive.co.kr/app/user_delete_progress.php',
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/afreecatv.py (Python)

Code before:
import time

from .common import InfoExtractor
from ..networking import Request
from ..utils import (
    ExtractorError,
    OnDemandPagedList,
    UserNotLive,
    determine_ext,
    filter_dict,
    int_or_none,
    orderedSet,
    parse_iso8601,
    url_or_none,
    urlencode_postdata,
    urljoin,
)
from ..utils.traversal import require, traverse_obj


def _cloudfront_auth_request(m3u8_url, strm_id, video_id, referer_url):
    return Request(
        'https://live.sooplive.co.kr/api/private_auth.php',
        method='POST',
        headers={
            'Content-Type': 'application/x-www-form-urlencoded',
            'Referer': referer_url,
            'Origin': 'https://vod.sooplive.co.kr',
        },
        data=urlencode_postdata({
            'type': 'vod',
            'strm_id': strm_id,
            'title_no': video_id,
            'url': m3u8_url,
        }))


class AfreecaTVBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'afreecatv'

    def _perform_login(self, username, password):
        login_form = {
            'szWork': 'login',
            'szType': 'json',
            'szUid': username,
            'szPassword': password,
            'isSaveId': 'false',
            'szScriptVar': 'oLoginRet',
            'szAction': '',
        }


Review comment (suggestion):
`Request` sets this by default if `data` is passed
```suggestion
```

Code after:
import time

from .common import InfoExtractor
from ..networking import Request
from ..utils import (
    ExtractorError,
    OnDemandPagedList,
    UserNotLive,
    determine_ext,
    filter_dict,
    int_or_none,
    orderedSet,
    parse_iso8601,
    url_or_none,
    urlencode_postdata,
    urljoin,
)
from ..utils.traversal import require, traverse_obj


def _cloudfront_auth_request(m3u8_url, strm_id, video_id, referer_url):
    return Request(
        'https://live.sooplive.co.kr/api/private_auth.php',
        method='POST',
        headers={
            'Referer': referer_url,
            'Origin': 'https://vod.sooplive.co.kr',
        },
        data=urlencode_postdata({
            'type': 'vod',
            'strm_id': strm_id,
            'title_no': video_id,
            'url': m3u8_url,
        }))


class AfreecaTVBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'afreecatv'

    def _perform_login(self, username, password):
        login_form = {
            'szWork': 'login',
            'szType': 'json',
            'szUid': username,
            'szPassword': password,
            'isSaveId': 'false',
            'szScriptVar': 'oLoginRet',
            'szAction': '',
        }

        response = self._download_json(
Repository: yt-dlp/yt-dlp
File: test/test_devalue.py (Python)

Code before:

        name = 'Object (cyclical)'
        result = devalue.parse([{'self': 0}])
        self.assertIs(result, result['self'], name)

        name = 'Object with null prototype (cyclical)'
        result = devalue.parse([['null', 'self', 0]])
        self.assertIs(result, result['self'], name)

        name = 'Objects (cyclical)'
        result = devalue.parse([[1, 2], {'second': 2}, {'first': 1}])
        self.assertIs(result[0], result[1]['first'], name)
        self.assertIs(result[1], result[0]['second'], name)

    def test_devalue_parse_revivers(self):
        self.assertEqual(
            devalue.parse([['indirect', 1], {'a': 2}, 'b'], revivers={'indirect': lambda x: x}),
            {'a': 'b'}, 'revivers (indirect)')

        self.assertEqual(
            devalue.parse([['parse', 1], '{"a":0}'], revivers={'parse': json.loads}),
            {'a': 0}, 'revivers (parse)')

        self.assertEqual(
            devalue.parse([{'a': 1, 'b': 3}, ['EmptyRef', 2], 'false', ['EmptyRef', 2]], revivers={'EmptyRef': json.loads}),
            {'a': False, 'b': False}, msg='EmptyRef (json.parse)')


if __name__ == '__main__':
    unittest.main()


Review comment (refactor):
```suggestion
            {'a': False, 'b': False}, msg='revivers (duplicate EmptyRef)')
```

Code after:

        name = 'Object (cyclical)'
        result = devalue.parse([{'self': 0}])
        self.assertIs(result, result['self'], name)

        name = 'Object with null prototype (cyclical)'
        result = devalue.parse([['null', 'self', 0]])
        self.assertIs(result, result['self'], name)

        name = 'Objects (cyclical)'
        result = devalue.parse([[1, 2], {'second': 2}, {'first': 1}])
        self.assertIs(result[0], result[1]['first'], name)
        self.assertIs(result[1], result[0]['second'], name)

    def test_devalue_parse_revivers(self):
        self.assertEqual(
            devalue.parse([['indirect', 1], {'a': 2}, 'b'], revivers={'indirect': lambda x: x}),
            {'a': 'b'}, 'revivers (indirect)')

        self.assertEqual(
            devalue.parse([['parse', 1], '{"a":0}'], revivers={'parse': json.loads}),
            {'a': 0}, 'revivers (parse)')

        self.assertEqual(
            devalue.parse([{'a': 1, 'b': 3}, ['EmptyRef', 2], 'false', ['EmptyRef', 2]], revivers={'EmptyRef': json.loads}),
            {'a': False, 'b': False}, msg='revivers (duplicate EmptyRef)')


if __name__ == '__main__':
    unittest.main()
Repository: yt-dlp/yt-dlp
File: yt_dlp/utils/_jsruntime.py (Python)

Code before:
from __future__ import annotations

import abc
import dataclasses
import functools
import os.path
import sys

from ._utils import _get_exe_version_output, detect_exe_version, version_tuple


_FALLBACK_PATHEXT = ('.COM', '.EXE', '.BAT', '.CMD')


def _find_exe(basename: str) -> str:
    if basename == 'deno':
        try: # look for a PyPI-installed version first.
            import deno
            return deno.find_deno_bin()
        except ImportError:
            pass # try the other options.

    if os.name != 'nt':
        return basename

    paths: list[str] = []

    # binary dir
    if getattr(sys, 'frozen', False):
        paths.append(os.path.dirname(sys.executable))
    # cwd
    paths.append(os.getcwd())
    # PATH items
    if path := os.environ.get('PATH'):
        paths.extend(filter(None, path.split(os.path.pathsep)))

    pathext = os.environ.get('PATHEXT')
    if pathext is None:
        exts = _FALLBACK_PATHEXT
    else:
        exts = tuple(ext for ext in pathext.split(os.pathsep) if ext)

    visited = []
    for path in map(os.path.realpath, paths):
        normed = os.path.normcase(path)
        if normed in visited:

Review comment (suggestion):
I think we can do it ourselves instead of importing the `deno` package
```suggestion
    # Check in Python "scripts" path, e.g. for pipx-installed binaries
    binary = os.path.join(
        sysconfig.get_path('scripts'),
        basename + sysconfig.get_config_var('EXE'))
    if os.access(binary, os.F_OK | os.X_OK) and not os.path.isdir(binary):
        return binary
```

Code after:
from __future__ import annotations

import abc
import dataclasses
import functools
import os.path
import sys
import sysconfig

from ._utils import _get_exe_version_output, detect_exe_version, version_tuple


_FALLBACK_PATHEXT = ('.COM', '.EXE', '.BAT', '.CMD')


def _find_exe(basename: str) -> str:
    # Check in Python "scripts" path, e.g. for pipx-installed binaries
    binary = os.path.join(
        sysconfig.get_path('scripts'),
        basename + sysconfig.get_config_var('EXE'))
    if os.access(binary, os.F_OK | os.X_OK) and not os.path.isdir(binary):
        return binary

    if os.name != 'nt':
        return basename

    paths: list[str] = []

    # binary dir
    if getattr(sys, 'frozen', False):
        paths.append(os.path.dirname(sys.executable))
    # cwd
    paths.append(os.getcwd())
    # PATH items
    if path := os.environ.get('PATH'):
        paths.extend(filter(None, path.split(os.path.pathsep)))

    pathext = os.environ.get('PATHEXT')
    if pathext is None:
        exts = _FALLBACK_PATHEXT
    else:
        exts = tuple(ext for ext in pathext.split(os.pathsep) if ext)

    visited = []
    for path in map(os.path.realpath, paths):
        normed = os.path.normcase(path)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/err.py (Python)

Code before:
        formats, subtitles = [], {}
        media_data = traverse_obj(data, ('media', 'src'), {url_or_none})
        if 'hls' in media_data:
            fmts, subs = self._extract_m3u8_formats_and_subtitles(
                media_data['hls'], video_id, 'mp4', m3u8_id='hls', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)
        if 'dash' in media_data:
            fmts, subs = self._extract_mpd_formats_and_subtitles(
                media_data['dash'], video_id, mpd_id='dash', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(data['info'], {
                'title': ('title', {str}),
                'series': ('seriesTitle', {str}),
                'series_id': ('seriesId', {str}),
                'episode_id': ('episode', {int_or_none}),
                'description': ('synopsis', {str}),
                'timestamp': ('date', {parse_iso8601}),
                'modified_timestamp': ('dateModified', {parse_iso8601}),
                'release_timestamp': ('uploadDate', {parse_iso8601}),
                'release_year': ('year', {int_or_none}),
            }, get_all=False),
        }


Review comment (refactor):
Maybe swap the values for `timestamp` and `release_timestamp`? Since then `upload_date` would be sourced from `uploadDate`. Or do you have a reason for extracting them this way?

Code after:
                hls_url, video_id, 'mp4', m3u8_id='hls', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)
        if dash_url := traverse_obj(data, ('media', 'src', 'dash', {url_or_none})):
            fmts, subs = self._extract_mpd_formats_and_subtitles(
                dash_url, video_id, mpd_id='dash', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(data, ('info', {
                'title': ('title', {str}),
                'series': ('seriesTitle', {str}, filter),
                'series_id': ('seriesId', {str}, filter),
                'episode_id': ('episode', {int_or_none}),
                'description': ('synopsis', {str}, filter),
                'timestamp': ('uploadDate', {parse_iso8601}),
                'modified_timestamp': ('dateModified', {parse_iso8601}),
                'release_timestamp': ('date', {parse_iso8601}),
                'release_year': ('year', {int_or_none}),
            })),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/err.py (Python)

Code before:
        if hls_url := traverse_obj(data, ('media', 'src', 'hls', {url_or_none})):
            fmts, subs = self._extract_m3u8_formats_and_subtitles(
                hls_url, video_id, 'mp4', m3u8_id='hls', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)
        if dash_url := traverse_obj(data, ('media', 'src', 'dash', {url_or_none})):
            fmts, subs = self._extract_mpd_formats_and_subtitles(
                dash_url, video_id, mpd_id='dash', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(data['info'], {
                'title': ('title', {str}),
                'series': ('seriesTitle', {str}, filter),
                'series_id': ('seriesId', {str}, filter),
                'episode_id': ('episode', {int_or_none}),
                'description': ('synopsis', {str}, filter),
                'timestamp': ('uploadDate', {parse_iso8601}),
                'modified_timestamp': ('dateModified', {parse_iso8601}),
                'release_timestamp': ('date', {parse_iso8601}),
                'release_year': ('year', {int_or_none}),
            }, get_all=False),
        }


Review comment (suggestion):
`get_all=False` is only necessary if one of the paths is branching, and none of the above are. plus a closing parentheses to go with my other suggestion
```suggestion
            })),
```

Code after:
        if hls_url := traverse_obj(data, ('media', 'src', 'hls', {url_or_none})):
            fmts, subs = self._extract_m3u8_formats_and_subtitles(
                hls_url, video_id, 'mp4', m3u8_id='hls', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)
        if dash_url := traverse_obj(data, ('media', 'src', 'dash', {url_or_none})):
            fmts, subs = self._extract_mpd_formats_and_subtitles(
                dash_url, video_id, mpd_id='dash', fatal=False)
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(data, ('info', {
                'title': ('title', {str}),
                'series': ('seriesTitle', {str}, filter),
                'series_id': ('seriesId', {str}, filter),
                'episode_id': ('episode', {int_or_none}),
                'description': ('synopsis', {str}, filter),
                'timestamp': ('uploadDate', {parse_iso8601}),
                'modified_timestamp': ('dateModified', {parse_iso8601}),
                'release_timestamp': ('date', {parse_iso8601}),
                'release_year': ('year', {int_or_none}),
            })),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/whyp.py (Python)

Code before:
    }, {
        'url': 'https://www.whyp.it/tracks/18337',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        unique_id = self._match_id(url)
        webpage = self._download_webpage(url, unique_id)
        data = self._search_nuxt_data(webpage, unique_id)['rawTrack']

        return {
            'id': unique_id,
            'formats': [{
                'url': data[f'{prefix}_url'],
                'format_id': prefix,
                'filesize': int_or_none(data.get(f'{prefix}_size')),
                'vcodec': 'none',
                'quality': 10 if prefix == 'lossless' else -1,
                'http_headers': {'Referer': 'https://whyp.it/'},
            } for prefix in ('audio', 'lossy', 'lossless') if url_or_none(data.get(f'{prefix}_url'))],
            **traverse_obj(data, {
                'title': 'title',
                'display_id': 'slug',
                'description': 'description',
                'duration': ('duration', {float_or_none}),
                'timestamp': ('created_at', {unified_timestamp}),
                'uploader': ('user', 'username'),
                'uploader_id': ('user', 'id', {str_or_none}),
                'thumbnail': ('artwork_url', {url_or_none}),
            }),
        }


Review comment (suggestion):
Although `unified_timestamp` works perfectly fine here, when the site is giving the value in ISO8601 format, I like to use `parse_iso8601` since it then also documents the original timestamp format in the code
```suggestion
                'timestamp': ('created_at', {parse_iso8601}),
```

Code after:
            'uploader_id': '1',
            'thumbnail': 'https://cdn.whyp.it/6ad0bbd9-577d-42bb-9b61-2a4f57f647eb.jpg',
        },
    }, {
        'url': 'https://www.whyp.it/tracks/18337',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        unique_id = self._match_id(url)
        webpage = self._download_webpage(url, unique_id)
        data = self._search_nuxt_data(webpage, unique_id)['rawTrack']

        return {
            'id': unique_id,
            'formats': [{
                'url': data[f'{prefix}_url'],
                'format_id': prefix,
                'filesize': int_or_none(data.get(f'{prefix}_size')),
                'vcodec': 'none',
                'quality': 10 if prefix == 'lossless' else -1,
                'http_headers': {'Referer': 'https://whyp.it/'},
            } for prefix in ('audio', 'lossy', 'lossless') if url_or_none(data.get(f'{prefix}_url'))],
            **traverse_obj(data, {
                'title': ('title', {str}),
                'display_id': ('slug', {str}),
                'description': 'description',
                'duration': ('duration', {float_or_none}),
                'timestamp': ('created_at', {parse_iso8601}),
                'uploader': ('user', 'username', {str}),
                'uploader_id': ('user', 'id', {str_or_none}),
                'thumbnail': ('artwork_url', {url_or_none}),
            }),
        }