Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/huya.py (Python)

Code before:

    def encrypt(self, params, stream_info, stream_name):
        ct = int_or_none(params.get('wsTime'), 16) + random.random()
        presenter_uid = stream_info['lPresenterUid']
        if not stream_name.startswith(str(presenter_uid)):
            uid = presenter_uid
        else:
            uid = int_or_none(ct % 1e7 * 1e6 % 0xffffffff)
        u1 = uid & 0xffffffff00000000
        u2 = uid & 0xffffffff
        u3 = uid & 0xffffff
        u = u1 | u2 >> 24 | u3 << 8
        params.update({
            'u': str_or_none(u),
            'seqid': str_or_none(int_or_none(ct * 1000) + uid),
            'ver': '1',
            'uuid': int_or_none(ct % 1e7 * 1e6 % 0xffffffff),
            't': '100',
        })
        fm = base64.b64decode(params['fm']).decode().split('_', 1)[0]
        ss = hashlib.md5('|'.join([params['seqid'], params['ctype'], params['t']]))
        return fm, ss


class HuyaVideoIE(InfoExtractor):
    _VALID_URL = r'https://(?:www\.)?huya\.com/video/play/(?P<id>\d+)\.html'

    IE_NAME = 'huya:video'
    IE_DESC = '虎牙视频'

    _TESTS = [{
        'url': 'https://www.huya.com/video/play/1002412640.html',
        'info_dict': {
            'id': '1002412640',
            'ext': 'mp4',
            'title': '8月3日',
            'thumbnail': r're:https?://.*\.jpg',
            'duration': 14,
            'uploader': '虎牙-ATS欧卡车队青木',
            'uploader_id': '1564376151',
            'upload_date': '20240803',
            'view_count': int,
            'comment_count': int,
            'like_count': int,
        },
    },
        {
        'url': 'https://www.huya.com/video/play/556054543.html',
        'info_dict': {
            'id': '556054543',
            'ext': 'mp4',

Review comment (suggestion):
```suggestion
    _VALID_URL = r'https?://(?:www\.)?huya\.com/video/play/(?P<id>\d+)\.html'
```

Code after:

    def encrypt(self, params, stream_info, stream_name):
        ct = int_or_none(params.get('wsTime'), 16) + random.random()
        presenter_uid = stream_info['lPresenterUid']
        if not stream_name.startswith(str(presenter_uid)):
            uid = presenter_uid
        else:
            uid = int_or_none(ct % 1e7 * 1e6 % 0xffffffff)
        u1 = uid & 0xffffffff00000000
        u2 = uid & 0xffffffff
        u3 = uid & 0xffffff
        u = u1 | u2 >> 24 | u3 << 8
        params.update({
            'u': str_or_none(u),
            'seqid': str_or_none(int_or_none(ct * 1000) + uid),
            'ver': '1',
            'uuid': int_or_none(ct % 1e7 * 1e6 % 0xffffffff),
            't': '100',
        })
        fm = base64.b64decode(params['fm']).decode().split('_', 1)[0]
        ss = hashlib.md5('|'.join([params['seqid'], params['ctype'], params['t']]))
        return fm, ss


class HuyaVideoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?huya\.com/video/play/(?P<id>\d+)\.html'
    IE_NAME = 'huya:video'
    IE_DESC = '虎牙视频'

    _TESTS = [{
        'url': 'https://www.huya.com/video/play/1002412640.html',
        'info_dict': {
            'id': '1002412640',
            'ext': 'mp4',
            'title': '8月3日',
            'thumbnail': r're:https?://.*\.jpg',
            'duration': 14,
            'uploader': '虎牙-ATS欧卡车队青木',
            'uploader_id': '1564376151',
            'upload_date': '20240803',
            'view_count': int,
            'comment_count': int,
            'like_count': int,
        },
    },
        {
        'url': 'https://www.huya.com/video/play/556054543.html',
        'info_dict': {
            'id': '556054543',
            'ext': 'mp4',
            'title': '我不挑事 也不怕事',