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

Code before:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = r'https?://matchtv\.ru/on-air'
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'is_live': True,
        },
        'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage_url = 'https://video.matchtv.ru/iframe/channel/106'
        webpage = self._download_webpage(webpage_url, video_id)
        video_url = self._html_search_regex(
            r'data-config="config=([^?]*)?', webpage, 'video URL').replace('feed', 'media') + '.m3u8'
        formats = self._extract_m3u8_formats(video_url, video_id, 'mp4')
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': formats,
        }


Review comment (suggestion):
```suggestion
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
```

Code after:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/matchtv.py (Python)

Code before:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = r'https?://matchtv\.ru/on-air'
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'is_live': True,
        },
        'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage_url = 'https://video.matchtv.ru/iframe/channel/106'
        webpage = self._download_webpage(webpage_url, video_id)
        video_url = self._html_search_regex(
            r'data-config="config=([^?]*)?', webpage, 'video URL').replace('feed', 'media') + '.m3u8'
        formats = self._extract_m3u8_formats(video_url, video_id, 'mp4')
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': formats,
        }


Review comment (refactor):
I would inline this below and set `live=True`
```suggestion
```
```python
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }
```

Code after:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/matchtv.py (Python)

Code before:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = r'https?://matchtv\.ru/on-air'
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'is_live': True,
        },
        'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage_url = 'https://video.matchtv.ru/iframe/channel/106'
        webpage = self._download_webpage(webpage_url, video_id)
        video_url = self._html_search_regex(
            r'data-config="config=([^?]*)?', webpage, 'video URL').replace('feed', 'media') + '.m3u8'
        formats = self._extract_m3u8_formats(video_url, video_id, 'mp4')
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': formats,
        }


Review comment (suggestion):
```suggestion
            'live_status': 'is_live',
```

Code after:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/matchtv.py (Python)

Code before:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = r'https?://matchtv\.ru/on-air'
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=([^?]*)?', webpage, 'video URL').replace('feed', 'media') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }


Review comment (suggestion):
```suggestion
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air',
        r'https?://video\.matchtv\.ru/iframe/channel/106',
    ]
```

Code after:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
        return {
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/matchtv.py (Python)

Code before:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air',
        r'https?://video\.matchtv\.ru/iframe/channel/106',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=([^?]*)?', webpage, 'video URL').replace('feed', 'media') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }


Review comment (suggestion):
`[^?]*` could match a whole lot more than we want it to if the site starts using URLs without query params. Need to find a more well-defined pattern IMO

Code after:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/matchtv.py (Python)

Code before:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air',
        r'https?://video\.matchtv\.ru/iframe/channel/106',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=([^?"]*)[?"]', webpage, 'video URL').replace('feed', 'media') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }


Review comment (suggestion):
```suggestion
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
```

Code after:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/matchtv.py (Python)

Code before:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air',
        r'https?://video\.matchtv\.ru/iframe/channel/106',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=([^?"]*)[?"]', webpage, 'video URL').replace('feed', 'media') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }


Review comment (suggestion):
```suggestion
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]
```

Code after:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/matchtv.py (Python)

Code before:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air',
        r'https?://video\.matchtv\.ru/iframe/channel/106',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=([^?"]*)[?"]', webpage, 'video URL').replace('feed', 'media') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
            'is_live': True,
            'formats': self._extract_m3u8_formats(video_url, video_id, 'mp4', live=True),
        }

Review comment (suggestion):
```suggestion
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
```

Code after:
from .common import InfoExtractor


class MatchTVIE(InfoExtractor):
    _VALID_URL = [
        r'https?://matchtv\.ru/on-air/?(?:$|[?#])',
        r'https?://video\.matchtv\.ru/iframe/channel/106/?(?:$|[?#])',
    ]
    _TESTS = [{
        'url': 'http://matchtv.ru/on-air/',
        'info_dict': {
            'id': 'matchtv-live',
            'ext': 'mp4',
            'title': r're:^Матч ТВ - Прямой эфир \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
            'live_status': 'is_live',
        },
        'params': {
            'skip_download': True,
        },
    }, {
        'url': 'https://video.matchtv.ru/iframe/channel/106',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        video_id = 'matchtv-live'
        webpage = self._download_webpage('https://video.matchtv.ru/iframe/channel/106', video_id)
        video_url = self._html_search_regex(
            r'data-config="config=(https?://[^?"]+)[?"]', webpage, 'video URL').replace('/feed/', '/media/') + '.m3u8'
        return {
            'id': video_id,
            'title': 'Матч ТВ - Прямой эфир',
Repository: yt-dlp/yt-dlp
File: yt_dlp/compat/imghdr.py (Python)

Code before:
def what(file=None, h=None):
    """ Detect format of image (Currently supports jpeg, png, webp, gif only) """
    if h is None:
        with open(file, 'rb') as f:
            h = f.read(12)

    if h.startswith(b'RIFF') and h.startswith(b'WEBP', 8):
        return 'webp'

    if h.startswith(b'\x89PNG'):
        return 'png'

    if h.startswith(b'\xFF\xD8\xFF'):
        return 'jpeg'

    if h.startswith(b'GIF'):
        return 'gif'

    return None


Review comment (suggestion):
```suggestion
    """ Detect format of image (Currently supports jpeg, png, webp, gif only)
    Ref: https://github.com/python/cpython/blob/3.11/Lib/imghdr.py
    Ref: https://www.w3.org/Graphics/JPEG/itu-t81.pdf
    """
```

Code after:
def what(file=None, h=None):
    """Detect format of image (Currently supports jpeg, png, webp, gif only)
    Ref: https://github.com/python/cpython/blob/3.11/Lib/imghdr.py
    Ref: https://www.w3.org/Graphics/JPEG/itu-t81.pdf
    """
    if h is None:
        with open(file, 'rb') as f:
            h = f.read(12)

    if h.startswith(b'RIFF') and h.startswith(b'WEBP', 8):
        return 'webp'

    if h.startswith(b'\x89PNG'):
        return 'png'

    if h.startswith(b'\xFF\xD8\xFF'):
        return 'jpeg'

    if h.startswith(b'GIF'):
        return 'gif'

    return None
Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/graspop.py (Python)

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


class GraspopIE(InfoExtractor):
    _VALID_URL = r'https?://vod\.graspop\.be/(?P<lang>fr|nl)/(?P<id>[0-9]+)/(?P<title>.*)/'
    _TESTS = [{
        'url': 'https://vod.graspop.be/fr/101556/thy-art-is-murder-concert/',
        'info_dict': {
            'id': '101556',
            'ext': 'mp4',
            'title': 'Thy Art Is Murder',
            'description': 'Thy Art Is Murder @ Graspop',
            'thumbnail': r're:https://cdn-mds\.pickx\.be/festivals/v3/global/original/.*\.jpg',
            'formats': 'count:4',
        },
        'params': {
            'nocheckcertificate': True,
        },
    }]

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

        # e.g. https://vod.graspop.be/fr/101556/thy-art-is-murder-concert/
        # e.g. https://tv.proximus.be/MWC/videocenter/festivals/101556/stream
        metadata = self._download_json(f'https://tv.proximus.be/MWC/videocenter/festivals/{video_id}/stream', video_id)
        band_name = metadata.get('name')
        asset_uri = traverse_obj(metadata, ('source', 'assetUri'))
        poster = traverse_obj(metadata, ('source', 'poster'))


Review comment (suggestion):
- let's match any locale code just in case
- we don't need capture groups that we don't actually capture

```suggestion
    _VALID_URL = r'https?://vod\.graspop\.be/[a-z]{2}/(?P<id>\d+)/'
```

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


class GraspopIE(InfoExtractor):
    _VALID_URL = r'https?://vod\.graspop\.be/[a-z]{2}/(?P<id>\d+)/'
    _TESTS = [{
        'url': 'https://vod.graspop.be/fr/101556/thy-art-is-murder-concert/',
        'info_dict': {
            'id': '101556',
            'ext': 'mp4',
            'title': 'Thy Art Is Murder',
            'thumbnail': r're:https://cdn-mds\.pickx\.be/festivals/v3/global/original/.+\.jpg',
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        metadata = self._download_json(
            f'https://tv.proximus.be/MWC/videocenter/festivals/{video_id}/stream', video_id)

        return {
            'id': video_id,
            'formats': self._extract_m3u8_formats(
                # Downgrade manifest request to avoid incomplete certificate chain error
                update_url(metadata['source']['assetUri'], scheme='http'), video_id, 'mp4'),
            **traverse_obj(metadata, {
                'title': ('name', {str}),
                'thumbnail': ('source', 'poster', {url_or_none}),
            }),
        }