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

Code before:
import json

from .common import InfoExtractor
from ..utils import ExtractorError, traverse_obj


class CanalsurmasIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?canalsurmas\.es/videos/(?P<id>\d+)'

    _TESTS = [
        {
            'url': 'https://www.canalsurmas.es/videos/44006-el-gran-queo-1-lora-del-rio-sevilla-20072014',
            'md5': '861f86fdc1221175e15523047d0087ef',
            'info_dict': {
                'id': '44006',
                'ext': 'mp4',
                'title': 'Lora del Río (Sevilla)  ',
                'description': 'md5:3d9ee40a9b1b26ed8259e6b71ed27b8b',
                'thumbnail': 'https://cdn2.rtva.interactvty.com/content_cards/00f3e8f67b0a4f3b90a4a14618a48b0d.jpg',
                'tags': [],
            },
        },
    ]

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

        access_token: str | None = traverse_obj(
            self._download_json(

Review comment (suggestion):
```suggestion
from ..utils import determine_ext, parse_iso8601, url_or_none
from ..utils.traversal import traverse_obj
```

Code after:
import json
import time

from .common import InfoExtractor
from ..utils import (
    determine_ext,
    float_or_none,
    jwt_decode_hs256,
    parse_iso8601,
    url_or_none,
    variadic,
)
from ..utils.traversal import traverse_obj


class CanalsurmasIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?canalsurmas\.es/videos/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.canalsurmas.es/videos/44006-el-gran-queo-1-lora-del-rio-sevilla-20072014',
        'md5': '861f86fdc1221175e15523047d0087ef',
        'info_dict': {
            'id': '44006',
            'ext': 'mp4',
            'title': 'Lora del Río (Sevilla)',
            'description': 'md5:3d9ee40a9b1b26ed8259e6b71ed27b8b',
            'thumbnail': 'https://cdn2.rtva.interactvty.com/content_cards/00f3e8f67b0a4f3b90a4a14618a48b0d.jpg',
            'timestamp': 1648123182,
            'upload_date': '20220324',
        },
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/msn.py (Python)

Code before:
                        }))
            for caption in traverse_obj(json_data, ('videoMetadata', 'closedCaptions', lambda _, v: url_or_none(v['href']))):
                lang = caption.get('locale') or 'en-us'
                subtitles.setdefault(lang, []).append({
                    'url': caption['href'],
                    'ext': 'ttml'})

            return {
                'id': page_id,
                'display_id': display_id,
                'formats': formats,
                'subtitles': subtitles,
                **common_metadata,
            }
        elif page_type == 'webcontent':
            if not source_url:
                raise ExtractorError('Could not find source URL')
            return self.url_result(source_url)
        elif page_type == 'article':
            entries = []
            for embed_url in traverse_obj(json_data, ('socialEmbeds', ..., 'postUrl', {url_or_none})):
                entries.append(self.url_result(embed_url))

            return self.playlist_result(entries, page_id, **common_metadata)
        else:
            raise ExtractorError('Unsupported URL type')


Review comment (refactor):
```suggestion

        raise ExtractorError(f'Unsupported page type: {page_type}')
```

Code after:
                        traverse_obj(file, {
                            'url': 'url',
                            'format_id': ('format', {str}),
                            'filesize': ('fileSize', {int_or_none}),
                            'height': ('height', {int_or_none}),
                            'width': ('width', {int_or_none}),
                        }))
            for caption in traverse_obj(json_data, ('videoMetadata', 'closedCaptions', lambda _, v: url_or_none(v['href']))):
                lang = caption.get('locale') or 'en-us'
                subtitles.setdefault(lang, []).append({
                    'url': caption['href'],
                    'ext': 'ttml',
                })

            return {
                'id': page_id,
                'display_id': display_id,
                'formats': formats,
                'subtitles': subtitles,
                **common_metadata,
            }
        elif page_type == 'webcontent':
            if not source_url:
                raise ExtractorError('Could not find source URL')
            return self.url_result(source_url)
        elif page_type == 'article':
            entries = []
            for embed_url in traverse_obj(json_data, ('socialEmbeds', ..., 'postUrl', {url_or_none})):
                entries.append(self.url_result(embed_url))

            return self.playlist_result(entries, page_id, **common_metadata)

        raise ExtractorError(f'Unsupported page type: {page_type}')
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/msn.py (Python)

Code before:
from .common import InfoExtractor
from ..utils import ExtractorError, clean_html, determine_ext, int_or_none, parse_iso8601, url_or_none
from ..utils.traversal import traverse_obj


class MSNIE(InfoExtractor):
    _VALID_URL = r'https?://(?:(?:www|preview)\.)?msn\.com/(?P<locale>[a-z]{2}-[a-z]{2})/(?:[^/?#]+/)+(?P<display_id>[^/?#]+)/[a-z]{2}-(?P<id>[\da-zA-Z]+)'
    _TESTS = [{
        'url': 'https://www.msn.com/en-gb/video/news/president-macron-interrupts-trump-over-ukraine-funding/vi-AA1zMcD7',
        'info_dict': {
            'id': 'AA1zMcD7',
            'ext': 'mp4',
            'display_id': 'president-macron-interrupts-trump-over-ukraine-funding',
            'title': 'President Macron interrupts Trump over Ukraine funding',
            'description': 'md5:5fd3857ac25849e7a56cb25fbe1a2a8b',
            'uploader': 'k! News UK',
            'uploader_id': 'BB1hz5Rj',
            'duration': 59,
            'thumbnail': 'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1zMagX.img',
            'tags': 'count:14',
            'timestamp': 1740510914,
            'upload_date': '20250225',
            'release_timestamp': 1740513600,
            'release_date': '20250225',
            'modified_timestamp': 1741413241,
            'modified_date': '20250308',
        },

Review comment (refactor):
```suggestion
from ..utils import (
    ExtractorError,
    clean_html,
    determine_ext,
    int_or_none,
    parse_iso8601,
    url_or_none,
)
```

Code after:
from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    clean_html,
    determine_ext,
    int_or_none,
    parse_iso8601,
    url_or_none,
)
from ..utils.traversal import traverse_obj


class MSNIE(InfoExtractor):
    _VALID_URL = r'https?://(?:(?:www|preview)\.)?msn\.com/(?P<locale>[a-z]{2}-[a-z]{2})/(?:[^/?#]+/)+(?P<display_id>[^/?#]+)/[a-z]{2}-(?P<id>[\da-zA-Z]+)'
    _TESTS = [{
        'url': 'https://www.msn.com/en-gb/video/news/president-macron-interrupts-trump-over-ukraine-funding/vi-AA1zMcD7',
        'info_dict': {
            'id': 'AA1zMcD7',
            'ext': 'mp4',
            'display_id': 'president-macron-interrupts-trump-over-ukraine-funding',
            'title': 'President Macron interrupts Trump over Ukraine funding',
            'description': 'md5:5fd3857ac25849e7a56cb25fbe1a2a8b',
            'uploader': 'k! News UK',
            'uploader_id': 'BB1hz5Rj',
            'duration': 59,
            'thumbnail': 'https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1zMagX.img',
            'tags': 'count:14',
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/vrsquare.py (Python)

Code before:
import itertools

from .common import InfoExtractor
from ..networking.exceptions import HTTPError
from ..utils import (
    ExtractorError,
    clean_html,
    extract_attributes,
    filter_dict,
    parse_duration,
    parse_qs,
    try_get,
)
from ..utils.traversal import (
    find_element,
    find_elements,
    traverse_obj,
)


class VRSQUAREIE(InfoExtractor):
    IE_NAME = 'vrsquare'
    IE_DESC = 'VR SQUARE'

    _VALID_URL = r'https?://livr\.jp/contents/(?P<id>[\w-]+)'
    _BASE_URL = 'https://livr.jp'
    _TESTS = [{
        'url': 'https://livr.jp/contents/P470896661',
        'info_dict': {
            'id': 'P470896661',
            'ext': 'mp4',
            'title': 'そこ曲がったら、櫻坂？ ７年間お疲れ様！菅井友香の卒業を祝う会！前半 2022年11月6日放送分',
            'description': 'md5:523726dc835aa8014dfe1e2b38d36cd1',
            'duration': 1515.0,
            'tags': list,
            'thumbnail': 're:^https?://media.livr.jp/vod/img/.*$',
        },
    }, {
        'url': 'https://livr.jp/contents/P589523973',
        'info_dict': {
            'id': 'P589523973',
            'ext': 'mp4',
            'title': '薄闇に仰ぐ しだれ桜の妖艶',
            'description': 'md5:a042f517b2cbb4ed6746707afec4d306',
            'duration': 1084.0,
            'tags': list,

Review comment (refactor):
I think like so is more legible
```suggestion
class VrSquareIE(InfoExtractor):
```

Code after:
import itertools

from .common import InfoExtractor
from ..networking.exceptions import HTTPError
from ..utils import (
    ExtractorError,
    clean_html,
    extract_attributes,
    parse_duration,
    parse_qs,
)
from ..utils.traversal import (
    find_element,
    find_elements,
    traverse_obj,
)


class VrSquareIE(InfoExtractor):
    IE_NAME = 'vrsquare'
    IE_DESC = 'VR SQUARE'

    _BASE_URL = 'https://livr.jp'
    _VALID_URL = r'https?://livr\.jp/contents/(?P<id>[\w-]+)'
    _TESTS = [{
        'url': 'https://livr.jp/contents/P470896661',
        'info_dict': {
            'id': 'P470896661',
            'ext': 'mp4',
            'title': 'そこ曲がったら、櫻坂？ ７年間お疲れ様！菅井友香の卒業を祝う会！前半 2022年11月6日放送分',
            'description': 'md5:523726dc835aa8014dfe1e2b38d36cd1',
            'duration': 1515.0,
            'tags': 'count:2',
            'thumbnail': r're:https?://media\.livr\.jp/vod/img/.+\.jpg',
        },
    }, {
        'url': 'https://livr.jp/contents/P589523973',
        'info_dict': {
            'id': 'P589523973',
            'ext': 'mp4',
            'title': '薄闇に仰ぐ しだれ桜の妖艶',
            'description': 'md5:a042f517b2cbb4ed6746707afec4d306',
            'duration': 1084.0,
            'tags': list,
            'thumbnail': r're:https?://media\.livr\.jp/vod/img/.+\.jpg',
        },
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/afreecatv.py (Python)

Code before:
import datetime
import functools

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


class AfreecaTVBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'afreecatv'

    def _perform_login(self, username, password):
        login_form = {

Review comment (suggestion):
import convention
```suggestion
import datetime as dt
```

Code after:
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


class AfreecaTVBaseIE(InfoExtractor):
    _NETRC_MACHINE = 'afreecatv'

    def _perform_login(self, username, password):
        login_form = {
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/loco.py (Python)

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


class LocoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?loco\.com/(?P<type>streamers|stream)/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://loco.com/streamers/teuzinfps',
        'info_dict': {
            'id': 'teuzinfps',
            'ext': 'mp4',
            'title': r're:MS BOLADAO, RESENHA & GAMEPLAY ALTO NIVEL',
            'description': 'bom e novo',
            'uploader_id': 'RLUVE3S9JU',
            'channel': 'teuzinfps',
            'channel_follower_count': int,
            'comment_count': int,
            'view_count': int,
            'concurrent_view_count': int,
            'like_count': int,
            'thumbnail': 'https://static.ivory.getloconow.com/default_thumb/743701a9-98ca-41ae-9a8b-70bd5da070ad.jpg',
            'tags': ['MMORPG', 'Gameplay'],
            'series': 'Tibia',
            'timestamp': int,
            'modified_timestamp': int,
            'live_status': 'is_live',
        },

Review comment (suggestion):
try to be concise with import location, ie
```suggestion
from ..utils.traversal import traverse_obj
```

Code after:
from .common import InfoExtractor
from ..utils import int_or_none, url_or_none
from ..utils.traversal import require, traverse_obj


class LocoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?loco\.com/(?P<type>streamers|stream)/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://loco.com/streamers/teuzinfps',
        'info_dict': {
            'id': 'teuzinfps',
            'ext': 'mp4',
            'title': r're:MS BOLADAO, RESENHA & GAMEPLAY ALTO NIVEL',
            'description': 'bom e novo',
            'uploader_id': 'RLUVE3S9JU',
            'channel': 'teuzinfps',
            'channel_follower_count': int,
            'comment_count': int,
            'view_count': int,
            'concurrent_view_count': int,
            'like_count': int,
            'thumbnail': 'https://static.ivory.getloconow.com/default_thumb/743701a9-98ca-41ae-9a8b-70bd5da070ad.jpg',
            'tags': ['MMORPG', 'Gameplay'],
            'series': 'Tibia',
            'timestamp': int,
            'modified_timestamp': int,
            'live_status': 'is_live',
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/loco.py (Python)

Code before:
from .common import InfoExtractor
from ..utils import int_or_none
from ..utils.traversal import require, traverse_obj


class LocoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?loco\.com/(?P<type>streamers|stream)/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://loco.com/streamers/teuzinfps',
        'info_dict': {
            'id': 'teuzinfps',
            'ext': 'mp4',
            'title': r're:MS BOLADAO, RESENHA & GAMEPLAY ALTO NIVEL',
            'description': 'bom e novo',
            'uploader_id': 'RLUVE3S9JU',
            'channel': 'teuzinfps',
            'channel_follower_count': int,
            'comment_count': int,
            'view_count': int,
            'concurrent_view_count': int,
            'like_count': int,
            'thumbnail': 'https://static.ivory.getloconow.com/default_thumb/743701a9-98ca-41ae-9a8b-70bd5da070ad.jpg',
            'tags': ['MMORPG', 'Gameplay'],
            'series': 'Tibia',
            'timestamp': int,
            'modified_timestamp': int,
            'live_status': 'is_live',

Review comment (suggestion):
```suggestion
from ..utils import int_or_none, url_or_none
```

Code after:
from .common import InfoExtractor
from ..utils import int_or_none, url_or_none
from ..utils.traversal import require, traverse_obj


class LocoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?loco\.com/(?P<type>streamers|stream)/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://loco.com/streamers/teuzinfps',
        'info_dict': {
            'id': 'teuzinfps',
            'ext': 'mp4',
            'title': r're:MS BOLADAO, RESENHA & GAMEPLAY ALTO NIVEL',
            'description': 'bom e novo',
            'uploader_id': 'RLUVE3S9JU',
            'channel': 'teuzinfps',
            'channel_follower_count': int,
            'comment_count': int,
            'view_count': int,
            'concurrent_view_count': int,
            'like_count': int,
            'thumbnail': 'https://static.ivory.getloconow.com/default_thumb/743701a9-98ca-41ae-9a8b-70bd5da070ad.jpg',
            'tags': ['MMORPG', 'Gameplay'],
            'series': 'Tibia',
            'timestamp': int,
            'modified_timestamp': int,
            'live_status': 'is_live',
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/tvw.py (Python)

Code before:
            'live_status': 'is_live',
        },
    }, {
        'url': 'https://tvw.org/tvchannels/tvw2/',
        'info_dict': {
            'id': 'TVW2',
            'ext': 'mp4',
            'display_id': 'tvw2',
            'title': r're:^TVW-2 Broadcast Channel',
            'thumbnail': r're:^https?://.*\.(?:jpe?g|png)$',
            'live_status': 'is_live',
        },
    }]

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

        stream_url = self._html_search_regex(r"<iframe[\s]*id=\"invintus-persistent-stream-frame\"[^>]+src=[\"\'](?P<url>.+?)[\"\']", webpage, 'url')
        parsed_url = urllib.parse.urlparse(unescapeHTML(stream_url))
        parsed_querystring = urllib.parse.parse_qs(parsed_url.query)

        encoder_obj = json.loads(traverse_obj(parsed_querystring, ('encoder', 0)))

        video_id = encoder_obj.get('streamName')
        m3u8_url = encoder_obj.get('live247URI')

        formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', m3u8_id='hls', fatal=True)

        return {
            'id': video_id,
            'display_id': display_id,
            'formats': formats,
            'title': remove_end(self._og_search_title(webpage, default=None), ' - TVW'),
            'thumbnail': self._og_search_thumbnail(webpage, default=None),
            'is_live': True,
        }


Review comment (refactor):
How about
```suggestion
        m3u8_url = traverse_obj(webpage, (
            {find_element(id='invintus-persistent-stream-frame', html=True)}, {extract_attributes},
            'src', {parse_qs}, 'encoder', 0, {json.loads}, 'live247URI', {url_or_none}, {require('stream url')}))
```

Code after:
        'url': 'https://tvw.org/tvchannels/air/',
        'info_dict': {
            'id': 'air',
            'ext': 'mp4',
            'title': r're:TVW Cable Channel Live Stream',
            'thumbnail': r're:https?://.+/.+\.(?:jpe?g|png)$',
            'live_status': 'is_live',
        },
    }, {
        'url': 'https://tvw.org/tvchannels/tvw2/',
        'info_dict': {
            'id': 'tvw2',
            'ext': 'mp4',
            'title': r're:TVW-2 Broadcast Channel',
            'thumbnail': r're:https?://.+/.+\.(?:jpe?g|png)$',
            'live_status': 'is_live',
        },
    }]

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

        m3u8_url = traverse_obj(webpage, (
            {find_element(id='invintus-persistent-stream-frame', html=True)}, {extract_attributes},
            'src', {parse_qs}, 'encoder', 0, {json.loads}, 'live247URI', {url_or_none}, {require('stream url')}))

        return {
            'id': video_id,
            'formats': self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', m3u8_id='hls', live=True),
            'title': remove_end(self._og_search_title(webpage, default=None), ' - TVW'),
            'thumbnail': self._og_search_thumbnail(webpage, default=None),
            'is_live': True,
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/tvw.py (Python)

Code before:
            'display_id': 'tvw2',
            'title': r're:^TVW-2 Broadcast Channel',
            'thumbnail': r're:^https?://.*\.(?:jpe?g|png)$',
            'live_status': 'is_live',
        },
    }]

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

        stream_url = self._html_search_regex(r"<iframe[\s]*id=\"invintus-persistent-stream-frame\"[^>]+src=[\"\'](?P<url>.+?)[\"\']", webpage, 'url')
        parsed_url = urllib.parse.urlparse(unescapeHTML(stream_url))
        parsed_querystring = urllib.parse.parse_qs(parsed_url.query)

        encoder_obj = json.loads(traverse_obj(parsed_querystring, ('encoder', 0)))

        video_id = encoder_obj.get('streamName')
        m3u8_url = encoder_obj.get('live247URI')

        formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', m3u8_id='hls', fatal=True)

        return {
            'id': video_id,
            'display_id': display_id,
            'formats': formats,
            'title': remove_end(self._og_search_title(webpage, default=None), ' - TVW'),
            'thumbnail': self._og_search_thumbnail(webpage, default=None),
            'is_live': True,
        }


Review comment (refactor):
`fatal=True` is the default. I would also inline
```suggestion
        return {
            'id': video_id,
            'formats': self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', m3u8_id='hls', live=True),
```

Code after:
        },
    }, {
        'url': 'https://tvw.org/tvchannels/tvw2/',
        'info_dict': {
            'id': 'tvw2',
            'ext': 'mp4',
            'title': r're:TVW-2 Broadcast Channel',
            'thumbnail': r're:https?://.+/.+\.(?:jpe?g|png)$',
            'live_status': 'is_live',
        },
    }]

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

        m3u8_url = traverse_obj(webpage, (
            {find_element(id='invintus-persistent-stream-frame', html=True)}, {extract_attributes},
            'src', {parse_qs}, 'encoder', 0, {json.loads}, 'live247URI', {url_or_none}, {require('stream url')}))

        return {
            'id': video_id,
            'formats': self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', m3u8_id='hls', live=True),
            'title': remove_end(self._og_search_title(webpage, default=None), ' - TVW'),
            'thumbnail': self._og_search_thumbnail(webpage, default=None),
            'is_live': True,
        }