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

Code before:
        'skip': 'paid video, no DRM. expires at Dec 31, 2023',
        'info_dict': {
            'id': '1213',
            'ext': 'mp4',
            'title': '【特別トーク番組アーカイブス】Merm4id×燐舞曲 2nd LIVE「VERSUS」',
            'release_date': '20231201',
            'thumbnail': str,
        }
    }, {
        'url': 'https://mixch.tv/archive/1214',
        'only_matching': True,
    }]

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

        try:
            info_json = self._download_json(
                f'https://mixch.tv/api-web/archive/{video_id}', video_id)
        except ExtractorError as e:
            if isinstance(e.cause, HTTPError):
                if e.cause.status == 401:
                    self.raise_login_required()
                elif e.cause.status == 404:
                    raise ExtractorError('This video may have been expired', expected=True) from e
            raise

        return {
            'id': video_id,
            'title': traverse_obj(info_json, ('archive', 'title', {str})),
            'formats': self._extract_m3u8_formats(
                traverse_obj(info_json, ('archive', 'archiveURL')), video_id),
            'thumbnail': traverse_obj(info_json, ('archive', 'thumbnailURL', {url_or_none})),
        }


Review comment (refactor):
I'm not sure about expecting a 404... if the endpoint changes/breaks, we'd wanna get a report about it. I'd keep it simple and do this:
```suggestion
        try:
            info_json = self._download_json(
                f'https://mixch.tv/api-web/archive/{video_id}', video_id)['archive']
        except ExtractorError as e:
            if isinstance(e.cause, HTTPError) and e.cause.status == 401:
                self.raise_login_required()
            raise
```

Code after:
        'skip': 'paid video, no DRM. expires at Dec 31, 2023',
        'info_dict': {
            'id': '1213',
            'ext': 'mp4',
            'title': '【特別トーク番組アーカイブス】Merm4id×燐舞曲 2nd LIVE「VERSUS」',
            'release_date': '20231201',
            'thumbnail': str,
        }
    }, {
        'url': 'https://mixch.tv/archive/1214',
        'only_matching': True,
    }]

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

        try:
            info_json = self._download_json(
                f'https://mixch.tv/api-web/archive/{video_id}', video_id)['archive']
        except ExtractorError as e:
            if isinstance(e.cause, HTTPError) and e.cause.status == 401:
                self.raise_login_required()
            raise

        return {
            'id': video_id,
            'title': traverse_obj(info_json, ('title', {str})),
            'formats': self._extract_m3u8_formats(info_json['archiveURL'], video_id),
            'thumbnail': traverse_obj(info_json, ('thumbnailURL', {url_or_none})),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/rinsefm.py (Python)

Code before:
import re
import json
from datetime import datetime, timezone

from .common import InfoExtractor


class RinseFMIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/episodes/(?P<id>.+)'
    _TESTS = [{
        'url': 'https://rinse.fm/episodes/cameo-blush-01-09-2023-2300/',
        'md5': '9284abbd785e6b86e67d1cdca6224feb',
        'info_dict': {
            'id': '1351562',
            'ext': 'mp3',
            'title': 'Cameo Blush - 01/09/2023 - 23:00',
            'thumbnail': r're:^https?://.*\.JPG$',
        }
    }]

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

        response = re.findall(r'<script[^>]*id\s*=\s*[\'"]__NEXT_DATA__[\'"][^>]*>(.*?)<\/script>', webpage, re.DOTALL)
        info = json.loads(response[0])

        entry = info['props']['pageProps']['entry']

        track_id = entry['id']
        title = entry['title']

        date_obj = datetime.fromisoformat(entry['episodeDate'])
        unix_timestamp = int(date_obj.replace(tzinfo=timezone.utc).timestamp())

        url = entry['fileUrl']
        thumbnail = "https://rinse.imgix.net/media/" + entry['featuredImage'][0]['filename']

        return {
            'id': track_id,
            'title': title,
            'url': url,
            'release_timestamp': unix_timestamp,
            'thumbnail': thumbnail
        }


Review comment (suggestion):
```suggestion
        info = self._search_nextjs_data(webpage, video_id)
```

Code after:
from .common import InfoExtractor
from ..utils import format_field, parse_iso8601


class RinseFMIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/episodes/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://rinse.fm/episodes/club-glow-15-12-2023-2000/',
        'md5': '76ee0b719315617df42e15e710f46c7b',
        'info_dict': {
            'id': '1536535',
            'ext': 'mp3',
            'title': 'Club Glow - 15/12/2023 - 20:00',
            'thumbnail': r're:^https://.+\.(?:jpg|JPG)$',
            'release_timestamp': 1702598400,
            'release_date': '20231215'
        }
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)
        entry = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['entry']

        return {
            'id': entry['id'],
            'title': entry.get('title'),
            'url': entry['fileUrl'],
            'vcodec': 'none',
            'release_timestamp': parse_iso8601(entry.get('episodeDate')),
            'thumbnail': format_field(
                entry, [('featuredImage', 0, 'filename')], 'https://rinse.imgix.net/media/%s', default=None),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/rinsefm.py (Python)

Code before:
from datetime import datetime, timezone

from .common import InfoExtractor


class RinseFMIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/episodes/(?P<id>.+)'
    _TESTS = [{
        'url': 'https://rinse.fm/episodes/cameo-blush-01-09-2023-2300/',
        'md5': '9284abbd785e6b86e67d1cdca6224feb',
        'info_dict': {
            'id': '1351562',
            'ext': 'mp3',
            'title': 'Cameo Blush - 01/09/2023 - 23:00',
            'thumbnail': r're:^https?://.*\.JPG$',
            'release_timestamp': 1693522800,
            'release_date': '20230831'
        }
    }]

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

        info = self._search_nextjs_data(webpage, video_id)

        entry = info['props']['pageProps']['entry']

        track_id = entry['id']
        title = entry['title']

        date_obj = datetime.fromisoformat(entry['episodeDate'])

Review comment (suggestion):
We don't want to match query/fragment/etc
```suggestion
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/episodes/(?P<id>[^/?#]+)'
```

Code after:
from .common import InfoExtractor
from ..utils import format_field, parse_iso8601


class RinseFMIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/episodes/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://rinse.fm/episodes/club-glow-15-12-2023-2000/',
        'md5': '76ee0b719315617df42e15e710f46c7b',
        'info_dict': {
            'id': '1536535',
            'ext': 'mp3',
            'title': 'Club Glow - 15/12/2023 - 20:00',
            'thumbnail': r're:^https://.+\.(?:jpg|JPG)$',
            'release_timestamp': 1702598400,
            'release_date': '20231215'
        }
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)
        entry = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['entry']

        return {
            'id': entry['id'],
            'title': entry.get('title'),
            'url': entry['fileUrl'],
            'vcodec': 'none',
            'release_timestamp': parse_iso8601(entry.get('episodeDate')),
            'thumbnail': format_field(
                entry, [('featuredImage', 0, 'filename')], 'https://rinse.imgix.net/media/%s', default=None),
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/rinsefm.py (Python)

Code before:
    }]

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

        info = self._search_nextjs_data(webpage, video_id)

        entry = info['props']['pageProps']['entry']

        track_id = entry['id']
        title = entry['title']

        date_obj = datetime.fromisoformat(entry['episodeDate'])
        unix_timestamp = int(date_obj.replace(tzinfo=timezone.utc).timestamp())

        url = entry['fileUrl']
        thumbnail = "https://rinse.imgix.net/media/" + entry['featuredImage'][0]['filename']

        return {
            'id': track_id,
            'title': title,
            'url': url,
            'release_timestamp': unix_timestamp,
            'thumbnail': thumbnail
        }


Review comment (refactor):
- inline metadata extraction in the return when you are able to
- metadata extraction should be non-fatal; use `.get()` (when it is actually non-fatal) or use the `traverse_obj()` util func (instead of chaining `.get()`s)
- use helper funcs like `parse_iso8601` or `unified_timestamp` (import from utils) instead of `datetime`
- we don't need the `info` var
- import `format_field` from utils
- for audio-only formats we need to add `'vcodec': 'none'` to the format dict
```suggestion
        entry = self._search_nextjs_data(webpage, video_id)['props']['pageProps']['entry']

        return {
            'id': entry['id'],
            'title': entry.get('title'),
            'url': entry['fileUrl'],
            'vcodec': 'none',
            'release_timestamp': parse_iso8601(entry.get('episodeDate')),
            'thumbnail': format_field(
                entry, [('featuredImage', 0, 'filename')], 'https://rinse.imgix.net/media/%s', default=None),
        }
```

Code after:
from .common import InfoExtractor
from ..utils import format_field, parse_iso8601


class RinseFMIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/episodes/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://rinse.fm/episodes/club-glow-15-12-2023-2000/',
        'md5': '76ee0b719315617df42e15e710f46c7b',
        'info_dict': {
            'id': '1536535',
            'ext': 'mp3',
            'title': 'Club Glow - 15/12/2023 - 20:00',
            'thumbnail': r're:^https://.+\.(?:jpg|JPG)$',
            'release_timestamp': 1702598400,
            'release_date': '20231215'
        }
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)
        entry = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['entry']

        return {
            'id': entry['id'],
            'title': entry.get('title'),
            'url': entry['fileUrl'],
            'vcodec': 'none',
            'release_timestamp': parse_iso8601(entry.get('episodeDate')),
            'thumbnail': format_field(
                entry, [('featuredImage', 0, 'filename')], 'https://rinse.imgix.net/media/%s', default=None),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/rinsefm.py (Python)

Code before:
from .common import InfoExtractor
from ..utils import (
    parse_iso8601,
    format_field,
)


class RinseFMIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/episodes/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://rinse.fm/episodes/cameo-blush-01-09-2023-2300/',
        'md5': '9284abbd785e6b86e67d1cdca6224feb',
        'info_dict': {
            'id': '1351562',
            'ext': 'mp3',
            'title': 'Cameo Blush - 01/09/2023 - 23:00',
            'thumbnail': r're:^https?://.*\.JPG$',
            'release_timestamp': 1693522800,
            'release_date': '20230831'
        }
    }]

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

        entry = self._search_nextjs_data(webpage, video_id)['props']['pageProps']['entry']
        return {
            'id': entry['id'],
            'title': entry.get('title'),

Review comment (suggestion):
```suggestion
from ..utils import format_field, parse_iso8601
```

Code after:
from .common import InfoExtractor
from ..utils import format_field, parse_iso8601


class RinseFMIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/episodes/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://rinse.fm/episodes/club-glow-15-12-2023-2000/',
        'md5': '76ee0b719315617df42e15e710f46c7b',
        'info_dict': {
            'id': '1536535',
            'ext': 'mp3',
            'title': 'Club Glow - 15/12/2023 - 20:00',
            'thumbnail': r're:^https://.+\.(?:jpg|JPG)$',
            'release_timestamp': 1702598400,
            'release_date': '20231215'
        }
    }]

    def _real_extract(self, url):
        display_id = self._match_id(url)
        webpage = self._download_webpage(url, display_id)
        entry = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['entry']

        return {
            'id': entry['id'],
            'title': entry.get('title'),
            'url': entry['fileUrl'],
            'vcodec': 'none',
            'release_timestamp': parse_iso8601(entry.get('episodeDate')),
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/rinsefm.py (Python)

Code before:
        },
        'playlist_mincount': 40
    }, {
        'url': 'https://rinse.fm/shows/ivy/',
        'info_dict': {
            'id': 'ivy',
            'title': '[IVY]',
            'description': 'A dedicated space for DNB/Turbo House and 4x4.'
        },
        'playlist_mincount': 7
    }]

    def _entries(self, episodes):
        for episode in episodes:
            yield self._parse_entry(episode)

    def _real_extract(self, url):
        playlist_id = self._match_id(url)
        webpage = self._download_webpage(url, playlist_id)
        title = self._og_search_title(webpage) or self._html_search_meta('title', webpage)
        description = self._og_search_description(webpage) or self._html_search_meta(
            'description', webpage)

        episodes = traverse_obj(
            self._search_nextjs_data(webpage, playlist_id),
            ('props', 'pageProps', 'episodes', lambda _, v: v['fileUrl'].endswith('mp3')))

        return self.playlist_result(
            self._entries(episodes), playlist_id, title, description=description)


Review comment (suggestion):
this should be more future-proof in case the site changes their audio encoding

import `MEDIA_EXTENSIONS` and `determine_ext` from utils
```suggestion
        episodes = traverse_obj(
            self._search_nextjs_data(webpage, playlist_id),
            ('props', 'pageProps', 'episodes', lambda _, v: determine_ext(v['fileUrl']) in MEDIA_EXTENSIONS.audio))
```

Code after:


class RinseFMArtistPlaylistIE(RinseFMBaseIE):
    _VALID_URL = r'https?://(?:www\.)?rinse\.fm/shows/(?P<id>[^/?#]+)'
    _TESTS = [{
        'url': 'https://rinse.fm/shows/resources/',
        'info_dict': {
            'id': 'resources',
            'title': '[re]sources',
            'description': '[re]sources est un label parisien piloté par le DJ et producteur Tommy Kid.'
        },
        'playlist_mincount': 40
    }, {
        'url': 'https://rinse.fm/shows/ivy/',
        'info_dict': {
            'id': 'ivy',
            'title': '[IVY]',
            'description': 'A dedicated space for DNB/Turbo House and 4x4.'
        },
        'playlist_mincount': 7
    }]

    def _entries(self, data):
        for episode in traverse_obj(data, (
            'props', 'pageProps', 'episodes', lambda _, v: determine_ext(v['fileUrl']) in MEDIA_EXTENSIONS.audio)
        ):
            yield self._parse_entry(episode)

    def _real_extract(self, url):
        playlist_id = self._match_id(url)
        webpage = self._download_webpage(url, playlist_id)
        title = self._og_search_title(webpage) or self._html_search_meta('title', webpage)
        description = self._og_search_description(webpage) or self._html_search_meta(
            'description', webpage)
        data = self._search_nextjs_data(webpage, playlist_id)

        return self.playlist_result(
            self._entries(data), playlist_id, title, description=description)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/ninenews.py (Python)

Code before:
            'thumbnail': 'md5:5ee4d66717bdd0dee9fc9a705ef041b8',
            'duration': 91.307,
            'timestamp': 1703229584,
            'upload_date': '20231222',
            'uploader_id': '664969388001',
            'tags': ['networkclip', 'aunews_aunationalninenews', 'presents', 'teachers', 'kindergarten', 'au_news'],
        },
    }]

    def _real_extract(self, url):
        article_id = self._match_id(url)
        webpage = self._download_webpage(url, article_id)
        initial_state = self._search_json(
            r'var\s+__INITIAL_STATE__\s*=', webpage, 'initial state', article_id)
        video_id = traverse_obj(
            initial_state, ('videoIndex', 'currentVideo', 'brightcoveId'),
            ('article', ..., 'media', lambda _, v: v['type'] == 'video', 'urn'), get_all=False)
        account = traverse_obj(initial_state, (
            'videoIndex', 'config', (None, 'video'), 'account'), get_all=False)

        if not video_id or not account:
            raise ExtractorError('Unable to get the required video data')

        return self.url_result(
            f'https://players.brightcove.net/{account}/default_default/index.html?videoId={video_id}',
            ie=BrightcoveNewIE, video_id=video_id)


Review comment (suggestion):
```suggestion
            BrightcoveNewIE, video_id)
```

Code after:
            'thumbnail': 'md5:5ee4d66717bdd0dee9fc9a705ef041b8',
            'duration': 91.307,
            'timestamp': 1703229584,
            'upload_date': '20231222',
            'uploader_id': '664969388001',
            'tags': ['networkclip', 'aunews_aunationalninenews', 'presents', 'teachers', 'kindergarten', 'au_news'],
        },
    }]

    def _real_extract(self, url):
        article_id = self._match_id(url)
        webpage = self._download_webpage(url, article_id)
        initial_state = self._search_json(
            r'var\s+__INITIAL_STATE__\s*=', webpage, 'initial state', article_id)
        video_id = traverse_obj(
            initial_state, ('videoIndex', 'currentVideo', 'brightcoveId', {str}),
            ('article', ..., 'media', lambda _, v: v['type'] == 'video', 'urn', {str}), get_all=False)
        account = traverse_obj(initial_state, (
            'videoIndex', 'config', (None, 'video'), 'account', {str}), get_all=False)

        if not video_id or not account:
            raise ExtractorError('Unable to get the required video data')

        return self.url_result(
            f'https://players.brightcove.net/{account}/default_default/index.html?videoId={video_id}',
            BrightcoveNewIE, video_id)
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/ninenews.py (Python)

Code before:
from .common import InfoExtractor
from .brightcove import BrightcoveNewIE

from ..utils import ExtractorError, traverse_obj


class NineNewsIE(InfoExtractor):
    IE_NAME = '9News'
    _VALID_URL = r'https?://(?:www\.)?9news\.com\.au/\w+/(?:\w+/)?[^/]+/(?P<id>[^/]+)(?:$|[?#])'
    _TESTS = [{
        'url': 'https://www.9news.com.au/videos/national/fair-trading-pulls-dozens-of-toys-from-shelves/clqgc7dvj000y0jnvfism0w5m',
        'md5': 'd1a65b2e9d126e5feb9bc5cb96e62c80',
        'info_dict': {
            'id': '6343717246112',
            'ext': 'mp4',
            'title': 'Fair Trading pulls dozens of toys from shelves',
            'description': 'Fair Trading Australia have been forced to pull dozens of toys from shelves over hazard fears.',
            'thumbnail': 'md5:bdbe44294e2323b762d97acf8843f66c',
            'duration': 93.44,
            'timestamp': 1703231748,
            'upload_date': '20231222',
            'uploader_id': '664969388001',
            'tags': ['networkclip', 'aunews_aunationalninenews', 'christmas presents', 'toys', 'fair trading', 'au_news'],
        }
    }, {
        'url': 'https://www.9news.com.au/world/tape-reveals-donald-trump-pressured-michigan-officials-not-to-certify-2020-vote-a-new-report-says/0b8b880e-7d3c-41b9-b2bd-55bc7e492259',
        'md5': 'a885c44d20898c3e70e9a53e8188cea1',
        'info_dict': {
            'id': '6343587450112',

Review comment (refactor):
```suggestion
from .common import InfoExtractor
from .brightcove import BrightcoveNewIE
from ..utils import ExtractorError
from ..utils.traversal import traverse_obj
```

Code after:
from .common import InfoExtractor
from .brightcove import BrightcoveNewIE
from ..utils import ExtractorError
from ..utils.traversal import traverse_obj


class NineNewsIE(InfoExtractor):
    IE_NAME = '9News'
    _VALID_URL = r'https?://(?:www\.)?9news\.com\.au/(?:[\w-]+/){2,3}(?P<id>[\w-]+)/?(?:$|[?#])'
    _TESTS = [{
        'url': 'https://www.9news.com.au/videos/national/fair-trading-pulls-dozens-of-toys-from-shelves/clqgc7dvj000y0jnvfism0w5m',
        'md5': 'd1a65b2e9d126e5feb9bc5cb96e62c80',
        'info_dict': {
            'id': '6343717246112',
            'ext': 'mp4',
            'title': 'Fair Trading pulls dozens of toys from shelves',
            'description': 'Fair Trading Australia have been forced to pull dozens of toys from shelves over hazard fears.',
            'thumbnail': 'md5:bdbe44294e2323b762d97acf8843f66c',
            'duration': 93.44,
            'timestamp': 1703231748,
            'upload_date': '20231222',
            'uploader_id': '664969388001',
            'tags': ['networkclip', 'aunews_aunationalninenews', 'christmas presents', 'toys', 'fair trading', 'au_news'],
        }
    }, {
        'url': 'https://www.9news.com.au/world/tape-reveals-donald-trump-pressured-michigan-officials-not-to-certify-2020-vote-a-new-report-says/0b8b880e-7d3c-41b9-b2bd-55bc7e492259',
        'md5': 'a885c44d20898c3e70e9a53e8188cea1',
        'info_dict': {
            'id': '6343587450112',