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