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

Code before:
from .common import InfoExtractor
from ..utils import (
    clean_html,
    determine_ext,
    int_or_none,
    parse_duration,
    parse_resolution,
    try_get,
    unified_timestamp,
    url_or_none,
)


class CCMAIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?3cat\.cat/(?:[^/]+/)*?(?P<type>video|audio)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.3cat.cat/3cat/lespot-de-la-marato-de-tv3/video/5630208/',
        'md5': '7296ca43977c8ea4469e719c609b0871',
        'info_dict': {
            'id': '5630208',
            'ext': 'mp4',
            'title': 'L\'espot de La Marató de TV3',
            'description': 'md5:f12987f320e2f6e988e9908e4fe97765',
            'timestamp': 1478608140,
            'upload_date': '20161108',
            'age_limit': 0,
        },
    }, {
        'url': 'https://www.3cat.cat/3cat/el-consell-de-savis-analitza-el-derbi/audio/943685/',
        'md5': 'fa3e38f269329a278271276330261425',
        'info_dict': {
            'id': '943685',
            'ext': 'mp3',
            'title': 'El Consell de Savis analitza el derbi',
            'description': 'md5:e2a3648145f3241cb9c6b4b624033e53',
            'upload_date': '20170512',
            'timestamp': 1494622500,
            'vcodec': 'none',
            'categories': ['Esports'],
        },

Review comment (suggestion):
```suggestion
    _VALID_URL = r'https?://(?:www\.)?3cat\.cat/(?:3cat|tv3/sx3)/[^/?#]+/(?P<type>video|audio)/(?P<id>\d+)'
```

Code after:
from .common import InfoExtractor
from ..utils import (
    clean_html,
    determine_ext,
    int_or_none,
    parse_duration,
    parse_resolution,
    try_get,
    unified_timestamp,
    url_or_none,
)


class CCMAIE(InfoExtractor):
    IE_DESC = '3Cat, TV3 and Catalunya Ràdio'
    _VALID_URL = r'https?://(?:www\.)?3cat\.cat/(?:3cat|tv3/sx3)/[^/?#]+/(?P<type>video|audio)/(?P<id>\d+)'
    _TESTS = [{
        # ccma.cat/tv3/alacarta/ URLs redirect to 3cat.cat/3cat/
        'url': 'https://www.3cat.cat/3cat/lespot-de-la-marato-de-tv3/video/5630208/',
        'md5': '7296ca43977c8ea4469e719c609b0871',
        'info_dict': {
            'id': '5630208',
            'ext': 'mp4',
            'title': 'L\'espot de La Marató 2016: Ictus i les lesions medul·lars i cerebrals traumàtiques',
            'description': 'md5:f12987f320e2f6e988e9908e4fe97765',
            'timestamp': 1478608140,
            'upload_date': '20161108',
            'age_limit': 0,
            'alt_title': 'EsportMarató2016WEB_PerPublicar',
            'duration': 79,
            'thumbnail': 'https://img.3cat.cat/multimedia/jpg/4/6/1478536106664.jpg',
            'series': 'Dedicada a l\'ictus i les lesions medul·lars i cerebrals traumàtiques',
            'categories': ['Divulgació'],
        },
    }, {
        # ccma.cat/catradio/alacarta/ URLs redirect to 3cat.cat/3cat/
        'url': 'https://www.3cat.cat/3cat/el-consell-de-savis-analitza-el-derbi/audio/943685/',
        'md5': 'fa3e38f269329a278271276330261425',
        'info_dict': {
            'id': '943685',
Repository: yt-dlp/yt-dlp
File: test/test_plugins.py (Python)

Code before:
import importlib
import os
import shutil
import sys
import unittest
from pathlib import Path

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEST_DATA_DIR = Path(os.path.dirname(os.path.abspath(__file__)), 'testdata')
sys.path.append(str(TEST_DATA_DIR))
importlib.invalidate_caches()

from yt_dlp.plugins import Config, PACKAGE_NAME, directories, load_plugins


class TestPlugins(unittest.TestCase):

    TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME

    def test_directories_containing_plugins(self):
        self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories()))

    def test_extractor_classes(self):
        for module_name in tuple(sys.modules):
            if module_name.startswith(f'{PACKAGE_NAME}.extractor'):
                del sys.modules[module_name]
        plugins_ie = load_plugins('extractor', 'IE')

        self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
        self.assertIn('NormalPluginIE', plugins_ie.keys())

        # don't load modules with underscore prefix
        self.assertFalse(
            f'{PACKAGE_NAME}.extractor._ignore' in sys.modules,
            'loaded module beginning with underscore')
        self.assertNotIn('IgnorePluginIE', plugins_ie.keys())

        # Don't load extractors with underscore prefix

Review comment (suggestion):
```suggestion
from yt_dlp import Config
from yt_dlp.plugins import PACKAGE_NAME, directories, load_plugins
```

Code after:
import importlib
import os
import shutil
import sys
import unittest
from pathlib import Path

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEST_DATA_DIR = Path(os.path.dirname(os.path.abspath(__file__)), 'testdata')
sys.path.append(str(TEST_DATA_DIR))
importlib.invalidate_caches()

from yt_dlp.utils import Config
from yt_dlp.plugins import PACKAGE_NAME, directories, load_plugins


class TestPlugins(unittest.TestCase):

    TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME

    def test_directories_containing_plugins(self):
        self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories()))

    def test_extractor_classes(self):
        for module_name in tuple(sys.modules):
            if module_name.startswith(f'{PACKAGE_NAME}.extractor'):
                del sys.modules[module_name]
        plugins_ie = load_plugins('extractor', 'IE')

        self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
        self.assertIn('NormalPluginIE', plugins_ie.keys())

        # don't load modules with underscore prefix
        self.assertFalse(
            f'{PACKAGE_NAME}.extractor._ignore' in sys.modules,
            'loaded module beginning with underscore')
        self.assertNotIn('IgnorePluginIE', plugins_ie.keys())
Repository: yt-dlp/yt-dlp
File: test/test_plugins.py (Python)

Code before:
import importlib
import os
import shutil
import sys
import unittest
from pathlib import Path

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEST_DATA_DIR = Path(os.path.dirname(os.path.abspath(__file__)), 'testdata')
sys.path.append(str(TEST_DATA_DIR))
importlib.invalidate_caches()

from yt_dlp import Config
from yt_dlp.plugins import PACKAGE_NAME, directories, load_plugins


class TestPlugins(unittest.TestCase):

    TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME

    def test_directories_containing_plugins(self):
        self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories()))

    def test_extractor_classes(self):
        for module_name in tuple(sys.modules):
            if module_name.startswith(f'{PACKAGE_NAME}.extractor'):
                del sys.modules[module_name]
        plugins_ie = load_plugins('extractor', 'IE')

        self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
        self.assertIn('NormalPluginIE', plugins_ie.keys())

        # don't load modules with underscore prefix
        self.assertFalse(
            f'{PACKAGE_NAME}.extractor._ignore' in sys.modules,
            'loaded module beginning with underscore')
        self.assertNotIn('IgnorePluginIE', plugins_ie.keys())


Review comment (suggestion):
```suggestion
from yt_dlp.utils import Config
```

Code after:
import importlib
import os
import shutil
import sys
import unittest
from pathlib import Path

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEST_DATA_DIR = Path(os.path.dirname(os.path.abspath(__file__)), 'testdata')
sys.path.append(str(TEST_DATA_DIR))
importlib.invalidate_caches()

from yt_dlp.utils import Config
from yt_dlp.plugins import PACKAGE_NAME, directories, load_plugins


class TestPlugins(unittest.TestCase):

    TEST_PLUGIN_DIR = TEST_DATA_DIR / PACKAGE_NAME

    def test_directories_containing_plugins(self):
        self.assertIn(self.TEST_PLUGIN_DIR, map(Path, directories()))

    def test_extractor_classes(self):
        for module_name in tuple(sys.modules):
            if module_name.startswith(f'{PACKAGE_NAME}.extractor'):
                del sys.modules[module_name]
        plugins_ie = load_plugins('extractor', 'IE')

        self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
        self.assertIn('NormalPluginIE', plugins_ie.keys())

        # don't load modules with underscore prefix
        self.assertFalse(
            f'{PACKAGE_NAME}.extractor._ignore' in sys.modules,
            'loaded module beginning with underscore')
        self.assertNotIn('IgnorePluginIE', plugins_ie.keys())
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/atresplayer.py (Python)

Code before:
            formats.extend(fmts)
            self._merge_subtitles(subs, target=subtitles)

        return {
            'display_id': display_id,
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(video_data, {
                'title': ('titulo', {str}),
                'description': ('descripcion', {str}),
                'duration': ('duration', {int_or_none}),
                'thumbnail': ('imgPoster', {url_or_none}, {lambda v: f'{v}1920x1080.jpg'}),
                'age_limit': ('ageRating', {parse_age_limit}),
            }),
            **traverse_obj(metadata, {
                'title': ('title', {str}),
                'description': ('description', {str}),
                'duration': ('duration', {int_or_none}),
                'tags': ('tags', ..., 'title', {str}),
                'age_limit': ('ageRating', {parse_age_limit}),
                'series': ('format', 'title', {str}),
                'season': ('currentSeason', 'title', {str}),
                'season_number': ('currentSeason', 'seasonNumber', {int_or_none}),
                'episode_number': ('numberOfEpisode', {int_or_none}),
                'timestamp': ('publicationDate', {float_or_none(scale=1000)}),
                'channel': ('channel', 'title', {str}),
            }),
        }


Review comment (suggestion):
```suggestion
                'timestamp': ('publicationDate', {int_or_none(scale=1000)}),
```

Code after:
        return {
            'display_id': display_id,
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(video_data, {
                'title': ('titulo', {str}),
                'description': ('descripcion', {str}),
                'duration': ('duration', {int_or_none}),
                'thumbnail': ('imgPoster', {url_or_none}, {lambda v: f'{v}1920x1080.jpg'}),
                'age_limit': ('ageRating', {parse_age_limit}),
            }),
            **traverse_obj(metadata, {
                'title': ('title', {str}),
                'description': ('description', {str}),
                'duration': ('duration', {int_or_none}),
                'tags': ('tags', ..., 'title', {str}),
                'age_limit': ('ageRating', {parse_age_limit}),
                'series': ('format', 'title', {str}),
                'season': ('currentSeason', 'title', {str}),
                'season_number': ('currentSeason', 'seasonNumber', {int_or_none}),
                'episode_number': ('numberOfEpisode', {int_or_none}),
                'timestamp': ('publicationDate', {int_or_none(scale=1000)}),
                'channel': ('channel', 'title', {str}),
            }),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/webvtt.py (Python)

Code before:
    (?:([0-9]{1,}):)?
    ([0-9]{2}):
    ([0-9]{2})\.
    ([0-9]{3})?
''')
_REGEX_EOF = re.compile(r'\Z')
_REGEX_NL = re.compile(r'(?:\r\n|[\r\n]|$)')
_REGEX_BLANK = re.compile(r'(?:\r\n|[\r\n])+')
_REGEX_OPTIONAL_WHITESPACE = re.compile(r'[ \t]*')


def _parse_ts(ts):
    """
    Convert a parsed WebVTT timestamp (a re.Match obtained from _REGEX_TS)
    into an MPEG PES timestamp: a tick counter at 90 kHz resolution.
    """
    return 90 * sum(
        int(part or 0) * mult for part, mult in zip(ts.groups(), (3600_000, 60_000, 1000, 1)))


def _format_ts(ts):
    """
    Convert an MPEG PES timestamp into a WebVTT timestamp.
    This will lose sub-millisecond precision.
    """
    return '{:02d}:{:02d}:{:02d}.{:03d}'.format(*timetuple_from_msec(int((ts + 45) // 90)))


class Block:
    """
    An abstract WebVTT block.
    """

    def __init__(self, **kwargs):
        for key, val in kwargs.items():
            setattr(self, key, val)

    @classmethod
    def parse(cls, parser):
        m = parser.match(cls._REGEX)
        if not m:
            return None
        parser.advance(m)
        return cls(raw=m.group(0))

    def write_into(self, stream):
        stream.write(self.raw)


class HeaderBlock(Block):
    """

Review comment (suggestion):
```suggestion
    return '{:02.0f}:{:02.0f}:{:02.0f}.{:03.0f}'.format(*timetuple_from_msec(int((ts + 45) // 90)))
```

Code after:
    (?:([0-9]{1,}):)?
    ([0-9]{2}):
    ([0-9]{2})\.
    ([0-9]{3})?
''')
_REGEX_EOF = re.compile(r'\Z')
_REGEX_NL = re.compile(r'(?:\r\n|[\r\n]|$)')
_REGEX_BLANK = re.compile(r'(?:\r\n|[\r\n])+')
_REGEX_OPTIONAL_WHITESPACE = re.compile(r'[ \t]*')


def _parse_ts(ts):
    """
    Convert a parsed WebVTT timestamp (a re.Match obtained from _REGEX_TS)
    into an MPEG PES timestamp: a tick counter at 90 kHz resolution.
    """
    return 90 * sum(
        int(part or 0) * mult for part, mult in zip(ts.groups(), (3600_000, 60_000, 1000, 1)))


def _format_ts(ts):
    """
    Convert an MPEG PES timestamp into a WebVTT timestamp.
    This will lose sub-millisecond precision.
    """
    return '%02u:%02u:%02u.%03u' % timetuple_from_msec(int((ts + 45) // 90))


class Block:
    """
    An abstract WebVTT block.
    """

    def __init__(self, **kwargs):
        for key, val in kwargs.items():
            setattr(self, key, val)

    @classmethod
    def parse(cls, parser):
        m = parser.match(cls._REGEX)
        if not m:
            return None
        parser.advance(m)
        return cls(raw=m.group(0))

    def write_into(self, stream):
        stream.write(self.raw)


class HeaderBlock(Block):
    """
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/nest.py (Python)

Code before:
from .common import InfoExtractor
from ..utils import ExtractorError, float_or_none, url_or_none
from ..utils.traversal import traverse_obj


class NestIE(InfoExtractor):
    _VALID_URL = r'https?://video\.nest\.com/(?:embedded/)?live/(?P<id>\w+)'
    _EMBED_REGEX = [rf'<iframe[^>]+\bsrc=[\'"](?P<url>{_VALID_URL})']
    _TESTS = [{
        'url': 'https://video.nest.com/embedded/live/4fvYdSo8AX?autoplay=0',
        'info_dict': {
            'id': '4fvYdSo8AX',
            'ext': 'mp4',
            'title': 'startswith:Outside ',
            'alt_title': 'Outside',
            'description': '<null>',
            'location': 'Los Angeles',
            'availability': 'public',
            'thumbnail': r're:https?://',
            'live_status': 'is_live',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
    }, {
        'url': 'https://video.nest.com/live/4fvYdSo8AX',
        'only_matching': True,
    }]
    _WEBPAGE_TESTS = [{
        'url': 'https://www.pacificblue.biz/noyo-harbor-webcam/',
        'info_dict': {
            'id': '4fvYdSo8AX',

Review comment (suggestion):
```suggestion
    _EMBED_REGEX = [rf'<iframe [^>]*\bsrc=[\'"](?P<url>{_VALID_URL})']
```

Code after:
from .common import InfoExtractor
from ..utils import ExtractorError, float_or_none, update_url_query, url_or_none
from ..utils.traversal import traverse_obj


class NestIE(InfoExtractor):
    _VALID_URL = r'https?://video\.nest\.com/(?:embedded/)?live/(?P<id>\w+)'
    _EMBED_REGEX = [rf'<iframe [^>]*\bsrc=[\'"](?P<url>{_VALID_URL})']
    _TESTS = [{
        'url': 'https://video.nest.com/embedded/live/4fvYdSo8AX?autoplay=0',
        'info_dict': {
            'id': '4fvYdSo8AX',
            'ext': 'mp4',
            'title': 'startswith:Outside ',
            'alt_title': 'Outside',
            'description': '<null>',
            'location': 'Los Angeles',
            'availability': 'public',
            'thumbnail': r're:https?://',
            'live_status': 'is_live',
        },
        'params': {
            # m3u8 download
            'skip_download': True,
        },
    }, {
        'url': 'https://video.nest.com/live/4fvYdSo8AX',
        'only_matching': True,
    }]
    _WEBPAGE_TESTS = [{
        'url': 'https://www.pacificblue.biz/noyo-harbor-webcam/',
        'info_dict': {
            'id': '4fvYdSo8AX',
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/globo.py (Python)

Code before:
import json
import re
import uuid

from .common import InfoExtractor
from ..utils import (
    float_or_none,
    int_or_none,
    orderedSet,
    str_or_none,
    try_get,
)
from ..utils.traversal import subs_list_to_dict, traverse_obj


class GloboIE(InfoExtractor):
    _VALID_URL = r'(?:globo:|https?://.+?\.globo\.com/(?:[^/]+/))(?P<id>\d{7,})'
    _NETRC_MACHINE = 'globo'
    _VIDEO_VIEW = '''
    query getVideoView($videoId: ID!) {
        video(id: $videoId) {
            duration
            description
            relatedEpisodeNumber
            relatedSeasonNumber
            headline
            title {
                originProgramId
                headline
            }
        }
    }
    '''
    _TESTS = [{
        'url': 'https://globoplay.globo.com/v/3607726/',
        'info_dict': {
            'id': '3607726',
            'ext': 'mp4',
            'title': 'Mercedes-Benz GLA passa por teste de colisão na Europa',
            'duration': 103.204,
            'uploader': 'G1 ao vivo',
            'uploader_id': '4209',

Review comment (suggestion):
```suggestion
    _VALID_URL = r'(?:globo:|https?://[^/?#]+?\.globo\.com/(?:[^/?#]+/))(?P<id>\d{7,})'
```

Code after:
import json
import re
import uuid

from .common import InfoExtractor
from ..utils import (
    determine_ext,
    filter_dict,
    float_or_none,
    int_or_none,
    orderedSet,
    str_or_none,
    try_get,
    url_or_none,
)
from ..utils.traversal import subs_list_to_dict, traverse_obj


class GloboIE(InfoExtractor):
    _VALID_URL = r'(?:globo:|https?://[^/?#]+?\.globo\.com/(?:[^/?#]+/))(?P<id>\d{7,})'
    _NETRC_MACHINE = 'globo'
    _VIDEO_VIEW = '''
    query getVideoView($videoId: ID!) {
        video(id: $videoId) {
            duration
            description
            relatedEpisodeNumber
            relatedSeasonNumber
            headline
            title {
                originProgramId
                headline
            }
        }
    }
    '''
    _TESTS = [{
        'url': 'https://globoplay.globo.com/v/3607726/',
        'info_dict': {
            'id': '3607726',
            'ext': 'mp4',
            'title': 'Mercedes-Benz GLA passa por teste de colisão na Europa',
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/puhutv.py (Python)

Code before:
                        'page': page,
                        'per': 100,
                    })['data']

                for episode in traverse_obj(season, ('episodes', lambda _, v: v.get('slug') or v['assets'][0]['slug'])):
                    slug = episode.get('slug') or episode['assets'][0]['slug']
                    yield self.url_result(
                        f'https://puhutv.com/{slug}', PuhuTVIE, episode.get('id'), episode.get('name'))

                page += 1
                has_more = traverse_obj(season, 'has_more')

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

        info = self._download_json(
            urljoin(url, f'/api/slug/{playlist_id}-detay'),
            playlist_id)['data']

        seasons = info.get('seasons')
        if seasons:
            return self.playlist_result(
                self._extract_entries(seasons), playlist_id, info.get('name'))

        # For films, these are using same url with series
        video_id = (info['slug'] or info['assets'][0]['slug']).removesuffix('-detay').removesuffix('-izle')
        return self.url_result(
            f'https://puhutv.com/{video_id}-izle', PuhuTVIE, video_id)


Review comment (suggestion):
```suggestion
        video_id = info.get('slug')
        if video_id:
            video_id = video_id.removesuffix('-detay')
        else:
            video_id = info['assets'][0]['slug'].removesuffix('-izle')
```

Code after:
                        'page': page,
                        'per': 100,
                    })['data']

                for episode in traverse_obj(season, ('episodes', lambda _, v: v.get('slug') or v['assets'][0]['slug'])):
                    slug = episode.get('slug') or episode['assets'][0]['slug']
                    yield self.url_result(
                        f'https://puhutv.com/{slug}', PuhuTVIE, episode.get('id'), episode.get('name'))

                page += 1
                has_more = traverse_obj(season, 'has_more')

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

        info = self._download_json(
            urljoin(url, f'/api/slug/{playlist_id}-detay'),
            playlist_id)['data']

        seasons = info.get('seasons')
        if seasons:
            return self.playlist_result(
                self._extract_entries(seasons), playlist_id, info.get('name'))

        # For films, these are using same url with series
        video_id = info.get('slug')
        if video_id:
            video_id = video_id.removesuffix('-detay')
        else:
            video_id = info['assets'][0]['slug'].removesuffix('-izle')
        return self.url_result(
            f'https://puhutv.com/{video_id}-izle', PuhuTVIE, video_id)