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

Code before:
                self.processVideoOrStream(asset_id, videos[0])
            )

        livestream = meta.get('livestream')

        if livestream is not None:
            info.update(
                self.processVideoOrStream(asset_id, livestream)
            )

        return info

    def processVideoOrStream(self, asset_id, video):
        video_id = video.get('id')
        video_src = video.get('src')
        video_type = video.get('type')

        token_data = self._download_json(
            'https://api.sportdeutschland.tv/api/frontend/asset-token/' + asset_id
            + '?type=' + video_type
            + '&playback_id=' + video_src,
            video_id
        )

        m3u8_url = "https://stream.mux.com/" + video_src + '.m3u8?token=' + token_data.get('token')
        formats = self._extract_m3u8_formats(m3u8_url, video_id)

        videoData = {
            'display_id': video_id,
            'formats': formats,
        }
        if video_type == 'mux_vod':
            videoData.update({
                'duration': video.get('duration'),
                'timestamp': time.mktime(datetime.datetime.fromisoformat(video.get('created_at')).timetuple())
            })

        return videoData


Review comment (refactor):
```suggestion
        token = self._download_json(
            f'https://api.sportdeutschland.tv/api/frontend/asset-token/{asset_id}',
            video_id, query={'type': video_type, 'playback_id': video_src})['token']
        formats = self._extract_m3u8_formats(f'https://stream.mux.com/{video_src}.m3u8?token={token}', video_id)
```

Code after:
                'entries': self.processVideoOrStream(asset_id, video)
            } for video in enumerate(videos) if video.get('formats'))

        elif len(videos) == 1:
            info.update(
                self.processVideoOrStream(asset_id, videos[0])
            )

        livestream = meta.get('livestream')

        if livestream is not None:
            info.update(
                self.processVideoOrStream(asset_id, livestream)
            )

        return info

    def process_video_or_stream(self, asset_id, video):
        video_id = video['id']
        video_src = video['src']
        video_type = video['type']

        token = self._download_json(
            f'https://api.sportdeutschland.tv/api/frontend/asset-token/{asset_id}',
            video_id, query={'type': video_type, 'playback_id': video_src})['token']
        formats = self._extract_m3u8_formats(f'https://stream.mux.com/{video_src}.m3u8?token={token}', video_id)

        video_data = {
            'display_id': video_id,
            'formats': formats,
        }
        if video_type == 'mux_vod':
            video_data.update({
                'duration': video.get('duration'),
                'timestamp': unified_timestamp(video.get('created_at'))
            })

        return video_data
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/sportdeutschland.py (Python)

Code before:

        return info

    def processVideoOrStream(self, asset_id, video):
        video_id = video.get('id')
        video_src = video.get('src')
        video_type = video.get('type')

        token_data = self._download_json(
            'https://api.sportdeutschland.tv/api/frontend/asset-token/' + asset_id
            + '?type=' + video_type
            + '&playback_id=' + video_src,
            video_id
        )

        m3u8_url = "https://stream.mux.com/" + video_src + '.m3u8?token=' + token_data.get('token')
        formats = self._extract_m3u8_formats(m3u8_url, video_id)

        videoData = {
            'display_id': video_id,
            'formats': formats,
        }
        if video_type == 'mux_vod':
            videoData.update({
                'duration': video.get('duration'),
                'timestamp': time.mktime(datetime.datetime.fromisoformat(video.get('created_at')).timetuple())
            })

        return videoData


Review comment (suggestion):
```suggestion
                'timestamp': unified_timestamp(video.get('created_at'))
```

Code after:

        if livestream is not None:
            info.update(
                self.processVideoOrStream(asset_id, livestream)
            )

        return info

    def process_video_or_stream(self, asset_id, video):
        video_id = video['id']
        video_src = video['src']
        video_type = video['type']

        token = self._download_json(
            f'https://api.sportdeutschland.tv/api/frontend/asset-token/{asset_id}',
            video_id, query={'type': video_type, 'playback_id': video_src})['token']
        formats = self._extract_m3u8_formats(f'https://stream.mux.com/{video_src}.m3u8?token={token}', video_id)

        video_data = {
            'display_id': video_id,
            'formats': formats,
        }
        if video_type == 'mux_vod':
            video_data.update({
                'duration': video.get('duration'),
                'timestamp': unified_timestamp(video.get('created_at'))
            })

        return video_data
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/tumblr.py (Python)

Code before:
                'formats': formats,
                'duration': duration,
                'thumbnail': traverse_obj(
                    video_json, ('poster', 0, 'url')) or self._og_search_thumbnail(
                        webpage, default=None)},
                *entries]

        if ignored_providers:
            if not entries:
                raise ExtractorError(f'None of embed providers are supported: {", ".join(ignored_providers)!s}', video_id=video_id, expected=True)
            else:
                self.report_warning(f'Skipped embeds from unsupported providers: {", ".join(ignored_providers)!s}', video_id)
        if unknown_providers:
            self.report_warning(f'Unrecognized providers, please report: {", ".join(unknown_providers)!s}', video_id)

        if len(entries) > 1:
            return {
                **info_dict,
                '_type': 'playlist',
                'entries': entries,
            }
        else:
            return {
                **info_dict,
                **entries[0],
            }


Review comment (suggestion):
Either we should add a `if not entries:` code path here or else invert the logic here e.g. `if len(entries) == 1: ` so that we return a 0-item playlist for posts w/o media

Code after:
            entries.insert(0, {
                **info_dict,
                'formats': formats,
                'duration': duration,
                'thumbnail': (traverse_obj(video_json, ('poster', 0, 'url', {url_or_none}))
                              or self._og_search_thumbnail(webpage, default=None)),
            })

        if ignored_providers:
            if not entries:
                raise ExtractorError(f'None of embed providers are supported: {", ".join(ignored_providers)!s}', video_id=video_id, expected=True)
            else:
                self.report_warning(f'Skipped embeds from unsupported providers: {", ".join(ignored_providers)!s}', video_id)
        if unknown_providers:
            self.report_warning(f'Unrecognized providers, please report: {", ".join(unknown_providers)!s}', video_id)

        if not entries:
            self.raise_no_formats('No video could be found in this post', expected=True, video_id=video_id)
        if len(entries) == 1:
            return {
                **info_dict,
                **entries[0],
            }
        return {
            **info_dict,
            '_type': 'playlist',
            'entries': entries,
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/douyutv.py (Python)

Code before:
import time
from hashlib import md5
import re

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    unescapeHTML,
    unified_strdate,
    urljoin,
)


class DouyuTVIE(InfoExtractor):
    IE_DESC = '斗鱼'
    _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(topic/.*rid=|(?:[^/]+/))*(?P<id>[A-Za-z0-9]+)'
    _TESTS = [{
        'url': 'http://www.douyutv.com/iseven',
        'info_dict': {
            'id': '17732',
            'display_id': 'iseven',
            'ext': 'flv',
            'title': 're:^清晨醒脑！根本停不下来！ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
            'description': r're:.*m7show@163\.com.*',
            'thumbnail': r're:^https?://.*\.png',
            'uploader': '7师傅',
            'is_live': True,
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'http://www.douyutv.com/85982',
        'info_dict': {
            'id': '85982',
            'display_id': '85982',
            'ext': 'flv',
            'title': 're:^小漠从零单排记！——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
            'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
            'thumbnail': r're:^https?://.*\.png',
            'uploader': 'douyu小漠',

Review comment (suggestion):
Don't use `.*`. The regex should not match any of these
```
https://www.douyu.com/topic/rid=6560603
https://www.douyu.com/topic/ydxc#rid=6560603
https://www.douyu.com/topic/ydxc?drid=6560603
```

Code after:
import time
import hashlib
import re
import urllib

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    unescapeHTML,
    unified_strdate,
    urljoin,
)


class DouyuTVIE(InfoExtractor):
    IE_DESC = '斗鱼'
    _VALID_URL = r'https?://(?:www\.)?douyu(?:tv)?\.com/(topic/\w+\?rid=|(?:[^/]+/))*(?P<id>[A-Za-z0-9]+)'
    _TESTS = [{
        'url': 'http://www.douyutv.com/iseven',
        'info_dict': {
            'id': '17732',
            'display_id': 'iseven',
            'ext': 'flv',
            'title': 're:^清晨醒脑！根本停不下来！ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
            'description': r're:.*m7show@163\.com.*',
            'thumbnail': r're:^https?://.*\.png',
            'uploader': '7师傅',
            'is_live': True,
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'http://www.douyutv.com/85982',
        'info_dict': {
            'id': '85982',
            'display_id': '85982',
            'ext': 'flv',
            'title': 're:^小漠从零单排记！——CSOL2躲猫猫 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
            'description': 'md5:746a2f7a253966a06755a912f0acc0d2',
            'thumbnail': r're:^https?://.*\.png',
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/hypergryph.py (Python)

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


class MonsterSirenHypergryphMusicIE(InfoExtractor):
    _VALID_URL = r'https?://monster-siren\.hypergryph\.com/music/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://monster-siren.hypergryph.com/music/514562',
        'info_dict': {
            'id': '514562',
            'ext': 'wav',
            'artist': ['塞壬唱片-MSR'],
            'album': 'Flame Shadow',
            'title': 'Flame Shadow',
        }
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        webpage = self._download_webpage(url, audio_id)
        json_data = self._search_regex(
            r'window\.g_initialProps\s*=\s*(.+);', webpage, 'window.g_initialProps')

        # undefined is not valid value for json file
        json_data = json_data.replace("undefined", "null")
        json_data = self._parse_json(json_data, audio_id)
        return {
            'id': audio_id or json_data['player']['songDetail']['cid'],
            'title': traverse_obj(json_data, ('player', 'songDetail', 'name')),
            'url': traverse_obj(json_data, ('player', 'songDetail', 'sourceUrl')),
            'ext': 'wav',
            'artist': traverse_obj(json_data, ('player', 'songDetail', 'artists')),
            'album': traverse_obj(json_data, ('musicPlay', 'albumDetail', 'name'))
        }


Review comment (suggestion):
`js_to_json` can make this simpler
```suggestion
        json_data = self._search_json(
            r'window\.g_initialProps\s*=', webpage, 'data', audio_id, transform_source=js_to_json)

```

Code after:
from .common import InfoExtractor
from ..utils import js_to_json, traverse_obj


class MonsterSirenHypergryphMusicIE(InfoExtractor):
    _VALID_URL = r'https?://monster-siren\.hypergryph\.com/music/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://monster-siren.hypergryph.com/music/514562',
        'info_dict': {
            'id': '514562',
            'ext': 'wav',
            'artist': ['塞壬唱片-MSR'],
            'album': 'Flame Shadow',
            'title': 'Flame Shadow',
        }
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        webpage = self._download_webpage(url, audio_id)
        json_data = self._search_json(
            r'window\.g_initialProps\s*=', webpage, 'data', audio_id, transform_source=js_to_json)

        return {
            'id': audio_id,
            'title': traverse_obj(json_data, ('player', 'songDetail', 'name')),
            'url': traverse_obj(json_data, ('player', 'songDetail', 'sourceUrl')),
            'ext': 'wav',
            'vcodec': 'none',
            'artist': traverse_obj(json_data, ('player', 'songDetail', 'artists')),
            'album': traverse_obj(json_data, ('musicPlay', 'albumDetail', 'name'))
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/hypergryph.py (Python)

Code before:


class MonsterSirenHypergryphMusicIE(InfoExtractor):
    _VALID_URL = r'https?://monster-siren\.hypergryph\.com/music/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://monster-siren.hypergryph.com/music/514562',
        'info_dict': {
            'id': '514562',
            'ext': 'wav',
            'artist': ['塞壬唱片-MSR'],
            'album': 'Flame Shadow',
            'title': 'Flame Shadow',
        }
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        webpage = self._download_webpage(url, audio_id)
        json_data = self._search_regex(
            r'window\.g_initialProps\s*=\s*(.+);', webpage, 'window.g_initialProps')

        # undefined is not valid value for json file
        json_data = json_data.replace("undefined", "null")
        json_data = self._parse_json(json_data, audio_id)
        return {
            'id': audio_id or json_data['player']['songDetail']['cid'],
            'title': traverse_obj(json_data, ('player', 'songDetail', 'name')),
            'url': traverse_obj(json_data, ('player', 'songDetail', 'sourceUrl')),
            'ext': 'wav',
            'artist': traverse_obj(json_data, ('player', 'songDetail', 'artists')),
            'album': traverse_obj(json_data, ('musicPlay', 'albumDetail', 'name'))
        }


Review comment (suggestion):
`audio_id` can't be `None`
```suggestion
            'id': audio_id,
```

Code after:


class MonsterSirenHypergryphMusicIE(InfoExtractor):
    _VALID_URL = r'https?://monster-siren\.hypergryph\.com/music/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://monster-siren.hypergryph.com/music/514562',
        'info_dict': {
            'id': '514562',
            'ext': 'wav',
            'artist': ['塞壬唱片-MSR'],
            'album': 'Flame Shadow',
            'title': 'Flame Shadow',
        }
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        webpage = self._download_webpage(url, audio_id)
        json_data = self._search_json(
            r'window\.g_initialProps\s*=', webpage, 'data', audio_id, transform_source=js_to_json)

        return {
            'id': audio_id,
            'title': traverse_obj(json_data, ('player', 'songDetail', 'name')),
            'url': traverse_obj(json_data, ('player', 'songDetail', 'sourceUrl')),
            'ext': 'wav',
            'vcodec': 'none',
            'artist': traverse_obj(json_data, ('player', 'songDetail', 'artists')),
            'album': traverse_obj(json_data, ('musicPlay', 'albumDetail', 'name'))
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/hypergryph.py (Python)

Code before:


class MonsterSirenHypergryphMusicIE(InfoExtractor):
    _VALID_URL = r'https?://monster-siren\.hypergryph\.com/music/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://monster-siren.hypergryph.com/music/514562',
        'info_dict': {
            'id': '514562',
            'ext': 'wav',
            'artist': ['塞壬唱片-MSR'],
            'album': 'Flame Shadow',
            'title': 'Flame Shadow',
        }
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        webpage = self._download_webpage(url, audio_id)
        json_data = self._search_regex(
            r'window\.g_initialProps\s*=\s*(.+);', webpage, 'window.g_initialProps')

        # undefined is not valid value for json file
        json_data = json_data.replace("undefined", "null")
        json_data = self._parse_json(json_data, audio_id)
        return {
            'id': audio_id or json_data['player']['songDetail']['cid'],
            'title': traverse_obj(json_data, ('player', 'songDetail', 'name')),
            'url': traverse_obj(json_data, ('player', 'songDetail', 'sourceUrl')),
            'ext': 'wav',
            'artist': traverse_obj(json_data, ('player', 'songDetail', 'artists')),
            'album': traverse_obj(json_data, ('musicPlay', 'albumDetail', 'name'))
        }


Review comment (suggestion):
```suggestion
            'ext': 'wav',
            'vcodec': 'none',
```

Code after:


class MonsterSirenHypergryphMusicIE(InfoExtractor):
    _VALID_URL = r'https?://monster-siren\.hypergryph\.com/music/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://monster-siren.hypergryph.com/music/514562',
        'info_dict': {
            'id': '514562',
            'ext': 'wav',
            'artist': ['塞壬唱片-MSR'],
            'album': 'Flame Shadow',
            'title': 'Flame Shadow',
        }
    }]

    def _real_extract(self, url):
        audio_id = self._match_id(url)
        webpage = self._download_webpage(url, audio_id)
        json_data = self._search_json(
            r'window\.g_initialProps\s*=', webpage, 'data', audio_id, transform_source=js_to_json)

        return {
            'id': audio_id,
            'title': traverse_obj(json_data, ('player', 'songDetail', 'name')),
            'url': traverse_obj(json_data, ('player', 'songDetail', 'sourceUrl')),
            'ext': 'wav',
            'vcodec': 'none',
            'artist': traverse_obj(json_data, ('player', 'songDetail', 'artists')),
            'album': traverse_obj(json_data, ('musicPlay', 'albumDetail', 'name'))
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/viu.py (Python)

Code before:
            if lang and ext:
                subtitles.setdefault(lang, []).append({
                    'ext': ext,
                    'url': initial_state_json[key]
                })
                subtitles[lang].append({
                    'ext': 'srt',
                    'url': f'{remove_end(initial_state_json[key], "vtt")}srt'
                })

        return {
            'id': display_id,
            'title': (initial_state_json.get('title') or initial_state_json.get('display_title')
                      or episode_json.get('name')),
            'description': initial_state_json.get('description') or episode_json.get('description'),
            'duration': initial_state_json.get('duration'),
            'thumbnail': traverse_obj(episode_json, ('image', 'url')),
            'timestamp': unified_timestamp(episode_json.get('dateCreated')),
            'formats': formats,
            'subtitles': subtitles,
            'episode_number': (int_or_none(initial_state_json.get('episode_no')
                               or initial_state_json.get('episodeno')
                               or episode_json.get('episodeNumber'))),
            'cast': traverse_obj(episode_json, ('actor', ..., 'name'), default=None),
            'age_limit': int_or_none(traverse_obj(
                self._AGE_RATINGS_MAPPER, (traverse_obj(initial_state_json, ('internal_age_rating')))))
        }


Review comment (suggestion):
```suggestion
            'age_limit': self._AGE_RATINGS_MAPPER.get(
                initial_state_json.get('internal_age_rating'))
```

Code after:
                    'ext': ext,
                    'url': url,
                })

                if ext == 'vtt':
                    subtitles[lang].append({
                        'ext': 'srt',
                        'url': f'{remove_end(initial_state[key], "vtt")}srt',
                    })

        episode = traverse_obj(list(filter(
            lambda x: x.get('@type') in ('TVEpisode', 'Movie'), self._yield_json_ld(webpage, display_id))), 0) or {}
        return {
            'id': display_id,
            'title': (traverse_obj(initial_state, 'title', 'display_title')
                      or episode.get('name')),
            'description': initial_state.get('description') or episode.get('description'),
            'duration': initial_state.get('duration'),
            'thumbnail': traverse_obj(episode, ('image', 'url')),
            'timestamp': unified_timestamp(episode.get('dateCreated')),
            'formats': formats,
            'subtitles': subtitles,
            'episode_number': (traverse_obj(initial_state, 'episode_no', 'episodeno', expected_type=int_or_none)
                               or int_or_none(episode.get('episodeNumber'))),
            'cast': traverse_obj(episode, ('actor', ..., 'name'), default=None),
            'age_limit': self._AGE_RATINGS_MAPPER.get(initial_state.get('internal_age_rating'))
        }