Repository: yt-dlp/yt-dlp

Issue:
[Huya] New extractor for video live stream
### Checklist

- [X] I'm reporting a new site support request
- [X] I've verified that I'm running yt-dlp version **2022.03.08.1**. ([update instructions](https://github.com/yt-dlp/yt-dlp#update))
- [X] I've checked that all provided URLs are alive and playable in a browser
- [X] I've checked that none of provided URLs [violate any copyrights](https://github.com/ytdl-org/youtube-dl#can-you-add-support-for-this-anime-video-site-or-site-which-shows-current-movies-for-free) or contain any [DRM](https://en.wikipedia.org/wiki/Digital_rights_management) to the best of my knowledge
- [X] I've searched the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues including closed ones. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)
- [X] I've read about [sharing account credentials](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#are-you-willing-to-share-account-details-if-needed) and am willing to share it if required

### Region

China

### Example URLs

- Video live: https://www.huya.com/clkimmy
if video is offline, you can get one of video that is online in here https://www.huya.com/

### Description

I tried to open a video live stream with mpv, and mpv is using the yt-dlp to extract the website. But yt-dlp doesnt support for Huya.

### Verbose log

```shell
❯ yt-dlp "https://www.huya.com/clkimmy" --verbose
[debug] Command-line config: ['https://www.huya.com/clkimmy', '--verbose']
[debug] Encodings: locale UTF-8, fs utf-8, out utf-8, err utf-8, pref UTF-8
[debug] yt-dlp version 2022.03.08.2 [e491d06d3]
[debug] Python version 3.9.7 (CPython 64bit) - Linux-5.13.0-35-generic-x86_64-with-glibc2.34
[debug] exe versions: ffmpeg 4.4 (setts), ffprobe 4.4, rtmpdump 2.4
[debug] Optional libraries: brotli, Cryptodome, secretstorage, mutagen, sqlite, websockets
[debug] Proxy map: {}
[debug] [generic] Extracting URL: https://www.huya.com/clkimmy
[generic] clkimmy: Requesting header
WARNING: [generic] Falling back on generic information extractor.
[generic] clkimmy: Downloading webpage
[generic] clkimmy: Extracting information
[debug] Looking for video embeds
ERROR: Unsupported URL: https://www.huya.com/clkimmy
Traceback (most recent call last):
  File "/home/smch/.local/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 1389, in wrapper
    return func(self, *args, **kwargs)
  File "/home/smch/.local/lib/python3.9/site-packages/yt_dlp/YoutubeDL.py", line 1459, in __extract_info
    ie_result = ie.extract(url)
  File "/home/smch/.local/lib/python3.9/site-packages/yt_dlp/extractor/common.py", line 617, in extract
    ie_result = self._real_extract(url)
  File "/home/smch/.local/lib/python3.9/site-packages/yt_dlp/extractor/generic.py", line 3998, in _real_extract
    raise UnsupportedError(url)
yt_dlp.utils.UnsupportedError: Unsupported URL: https://www.huya.com/clkimmy


~ 
❯
```



Discussion:

Resolving patch:
diff --git a/yt_dlp/extractor/extractors.py b/yt_dlp/extractor/extractors.py
--- a/yt_dlp/extractor/extractors.py
+++ b/yt_dlp/extractor/extractors.py
@@ -622,6 +622,7 @@
     HSEProductIE,
 )
 from .huajiao import HuajiaoIE
+from .huya import HuyaLiveIE
 from .huffpost import HuffPostIE
 from .hungama import (
     HungamaIE,
diff --git a/yt_dlp/extractor/huya.py b/yt_dlp/extractor/huya.py
new file mode 100644
--- /dev/null
+++ b/yt_dlp/extractor/huya.py
@@ -0,0 +1,138 @@
+# coding: utf-8
+from __future__ import unicode_literals
+
+import hashlib
+import random
+
+from ..compat import compat_urlparse, compat_b64decode
+
+from ..utils import (
+    ExtractorError,
+    int_or_none,
+    js_to_json,
+    str_or_none,
+    try_get,
+    unescapeHTML,
+    update_url_query,
+)
+
+from .common import InfoExtractor
+
+
+class HuyaLiveIE(InfoExtractor):
+    _VALID_URL = r'https?://(?:www\.|m\.)?huya\.com/(?P<id>[^/#?&]+)(?:\D|$)'
+    IE_NAME = 'huya:live'
+    IE_DESC = 'huya.com'
+    TESTS = [{
+        'url': 'https://www.huya.com/572329',
+        'info_dict': {
+            'id': '572329',
+            'title': str,
+            'description': str,
+            'is_live': True,
+            'view_count': int,
+        },
+        'params': {
+            'skip_download': True,
+        },
+    }, {
+        'url': 'https://www.huya.com/xiaoyugame',
+        'only_matching': True
+    }]
+
+    _RESOLUTION = {
+        '蓝光4M': {
+            'width': 1920,
+            'height': 1080,
+        },
+        '超清': {
+            'width': 1280,
+            'height': 720,
+        },
+        '流畅': {
+            'width': 800,
+            'height': 480
+        }
+    }
+
+    def _real_extract(self, url):
+        video_id = self._match_id(url)
+        webpage = self._download_webpage(url, video_id=video_id)
+        json_stream = self._search_regex(r'"stream":\s+"([a-zA-Z0-9+=/]+)"', webpage, 'stream', default=None)
+        if not json_stream:
+            raise ExtractorError('Video is offline', expected=True)
+        stream_data = self._parse_json(compat_b64decode(json_stream).decode(), video_id=video_id,
+                                       transform_source=js_to_json)
+        room_info = try_get(stream_data, lambda x: x['data'][0]['gameLiveInfo'])
+        if not room_info:
+            raise ExtractorError('Can not extract the room info', expected=True)
+        title = room_info.get('roomName') or room_info.get('introduction') or self._html_search_regex(
+            r'<title>([^<]+)</title>', webpage, 'title')
+        screen_type = room_info.get('screenType')
+        live_source_type = room_info.get('liveSourceType')
+        stream_info_list = stream_data['data'][0]['gameStreamInfoList']
+        formats = []
+        for stream_info in stream_info_list:
+            stream_url = stream_info.get('sFlvUrl')
+            if not stream_url:
+                continue
+            stream_name = stream_info.get('sStreamName')
+            re_secret = not screen_type and live_source_type in (0, 8, 13)
+            params = dict(compat_urlparse.parse_qsl(unescapeHTML(stream_info['sFlvAntiCode'])))
+            fm, ss = '', ''
+            if re_secret:
+                fm, ss = self.encrypt(params, stream_info, stream_name)
+            for si in stream_data.get('vMultiStreamInfo'):
+                rate = si.get('iBitRate')
+                if rate:
+                    params['ratio'] = rate
+                else:
+                    params.pop('ratio', None)
+                if re_secret:
+                    params['wsSecret'] = hashlib.md5(
+                        '_'.join([fm, params['u'], stream_name, ss, params['wsTime']]))
+                formats.append({
+                    'ext': stream_info.get('sFlvUrlSuffix'),
+                    'format_id': str_or_none(stream_info.get('iLineIndex')),
+                    'tbr': rate,
+                    'url': update_url_query(f'{stream_url}/{stream_name}.{stream_info.get("sFlvUrlSuffix")}',
+                                            query=params),
+                    **self._RESOLUTION.get(si.get('sDisplayName'), {}),
+                })
+
+        self._sort_formats(formats)
+
+        return {
+            'id': video_id,
+            'title': title,
+            'formats': formats,
+            'view_count': room_info.get('totalCount'),
+            'thumbnail': room_info.get('screenshot'),
+            'description': room_info.get('contentIntro'),
+            'http_headers': {
+                'Origin': 'https://www.huya.com',
+                'Referer': 'https://www.huya.com/',
+            },
+        }
+
+    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 = compat_b64decode(params['fm']).decode().split('_', 1)[0]
+        ss = hashlib.md5('|'.join([params['seqid'], params['ctype'], params['t']]))
+        return fm, ss


Test patch: