]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/huya.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / huya.py
CommitLineData
7e6a1870
HTL
1import hashlib
2import random
fbbb5508 3import re
7e6a1870 4
e897bd82
SS
5from .common import InfoExtractor
6from ..compat import compat_b64decode, compat_urlparse
7e6a1870
HTL
7from ..utils import (
8 ExtractorError,
9 int_or_none,
7e6a1870
HTL
10 str_or_none,
11 try_get,
12 unescapeHTML,
13 update_url_query,
14)
15
7e6a1870
HTL
16
17class HuyaLiveIE(InfoExtractor):
18 _VALID_URL = r'https?://(?:www\.|m\.)?huya\.com/(?P<id>[^/#?&]+)(?:\D|$)'
19 IE_NAME = 'huya:live'
20 IE_DESC = 'huya.com'
21 TESTS = [{
22 'url': 'https://www.huya.com/572329',
23 'info_dict': {
24 'id': '572329',
25 'title': str,
26 'description': str,
27 'is_live': True,
28 'view_count': int,
29 },
30 'params': {
31 'skip_download': True,
32 },
33 }, {
34 'url': 'https://www.huya.com/xiaoyugame',
35 'only_matching': True
36 }]
37
38 _RESOLUTION = {
fbbb5508 39 '蓝光': {
7e6a1870
HTL
40 'width': 1920,
41 'height': 1080,
42 },
43 '超清': {
44 'width': 1280,
45 'height': 720,
46 },
47 '流畅': {
48 'width': 800,
49 'height': 480
50 }
51 }
52
53 def _real_extract(self, url):
54 video_id = self._match_id(url)
55 webpage = self._download_webpage(url, video_id=video_id)
304ad45a 56 stream_data = self._search_json(r'stream:\s', webpage, 'stream', video_id=video_id, default=None)
7e6a1870
HTL
57 room_info = try_get(stream_data, lambda x: x['data'][0]['gameLiveInfo'])
58 if not room_info:
59 raise ExtractorError('Can not extract the room info', expected=True)
04f3fd2c 60 title = room_info.get('roomName') or room_info.get('introduction') or self._html_extract_title(webpage)
7e6a1870
HTL
61 screen_type = room_info.get('screenType')
62 live_source_type = room_info.get('liveSourceType')
63 stream_info_list = stream_data['data'][0]['gameStreamInfoList']
5135ed3d
O
64 if not stream_info_list:
65 raise ExtractorError('Video is offline', expected=True)
7e6a1870
HTL
66 formats = []
67 for stream_info in stream_info_list:
68 stream_url = stream_info.get('sFlvUrl')
69 if not stream_url:
70 continue
71 stream_name = stream_info.get('sStreamName')
72 re_secret = not screen_type and live_source_type in (0, 8, 13)
73 params = dict(compat_urlparse.parse_qsl(unescapeHTML(stream_info['sFlvAntiCode'])))
74 fm, ss = '', ''
75 if re_secret:
76 fm, ss = self.encrypt(params, stream_info, stream_name)
77 for si in stream_data.get('vMultiStreamInfo'):
fbbb5508
FY
78 display_name, bitrate = re.fullmatch(
79 r'(.+?)(?:(\d+)M)?', si.get('sDisplayName')).groups()
7e6a1870
HTL
80 rate = si.get('iBitRate')
81 if rate:
82 params['ratio'] = rate
83 else:
84 params.pop('ratio', None)
fbbb5508
FY
85 if bitrate:
86 rate = int(bitrate) * 1000
7e6a1870
HTL
87 if re_secret:
88 params['wsSecret'] = hashlib.md5(
89 '_'.join([fm, params['u'], stream_name, ss, params['wsTime']]))
90 formats.append({
91 'ext': stream_info.get('sFlvUrlSuffix'),
92 'format_id': str_or_none(stream_info.get('iLineIndex')),
93 'tbr': rate,
94 'url': update_url_query(f'{stream_url}/{stream_name}.{stream_info.get("sFlvUrlSuffix")}',
95 query=params),
fbbb5508 96 **self._RESOLUTION.get(display_name, {}),
7e6a1870
HTL
97 })
98
7e6a1870
HTL
99 return {
100 'id': video_id,
101 'title': title,
102 'formats': formats,
103 'view_count': room_info.get('totalCount'),
104 'thumbnail': room_info.get('screenshot'),
105 'description': room_info.get('contentIntro'),
106 'http_headers': {
107 'Origin': 'https://www.huya.com',
108 'Referer': 'https://www.huya.com/',
109 },
110 }
111
112 def encrypt(self, params, stream_info, stream_name):
113 ct = int_or_none(params.get('wsTime'), 16) + random.random()
114 presenter_uid = stream_info['lPresenterUid']
115 if not stream_name.startswith(str(presenter_uid)):
116 uid = presenter_uid
117 else:
118 uid = int_or_none(ct % 1e7 * 1e6 % 0xffffffff)
119 u1 = uid & 0xffffffff00000000
120 u2 = uid & 0xffffffff
121 u3 = uid & 0xffffff
122 u = u1 | u2 >> 24 | u3 << 8
123 params.update({
124 'u': str_or_none(u),
125 'seqid': str_or_none(int_or_none(ct * 1000) + uid),
126 'ver': '1',
127 'uuid': int_or_none(ct % 1e7 * 1e6 % 0xffffffff),
128 't': '100',
129 })
130 fm = compat_b64decode(params['fm']).decode().split('_', 1)[0]
131 ss = hashlib.md5('|'.join([params['seqid'], params['ctype'], params['t']]))
132 return fm, ss