]>
Commit | Line | Data |
---|---|---|
1 | import base64 | |
2 | import hashlib | |
3 | import random | |
4 | import re | |
5 | import urllib.parse | |
6 | ||
7 | from .common import InfoExtractor | |
8 | from ..utils import ( | |
9 | ExtractorError, | |
10 | int_or_none, | |
11 | str_or_none, | |
12 | try_get, | |
13 | unescapeHTML, | |
14 | update_url_query, | |
15 | ) | |
16 | ||
17 | ||
18 | class 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 | '蓝光': { | |
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) | |
57 | stream_data = self._search_json(r'stream:\s', webpage, 'stream', video_id=video_id, default=None) | |
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) | |
61 | title = room_info.get('roomName') or room_info.get('introduction') or self._html_extract_title(webpage) | |
62 | screen_type = room_info.get('screenType') | |
63 | live_source_type = room_info.get('liveSourceType') | |
64 | stream_info_list = stream_data['data'][0]['gameStreamInfoList'] | |
65 | if not stream_info_list: | |
66 | raise ExtractorError('Video is offline', expected=True) | |
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(urllib.parse.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 | display_name, bitrate = re.fullmatch( | |
80 | r'(.+?)(?:(\d+)M)?', si.get('sDisplayName')).groups() | |
81 | rate = si.get('iBitRate') | |
82 | if rate: | |
83 | params['ratio'] = rate | |
84 | else: | |
85 | params.pop('ratio', None) | |
86 | if bitrate: | |
87 | rate = int(bitrate) * 1000 | |
88 | if re_secret: | |
89 | params['wsSecret'] = hashlib.md5( | |
90 | '_'.join([fm, params['u'], stream_name, ss, params['wsTime']])) | |
91 | formats.append({ | |
92 | 'ext': stream_info.get('sFlvUrlSuffix'), | |
93 | 'format_id': str_or_none(stream_info.get('iLineIndex')), | |
94 | 'tbr': rate, | |
95 | 'url': update_url_query(f'{stream_url}/{stream_name}.{stream_info.get("sFlvUrlSuffix")}', | |
96 | query=params), | |
97 | **self._RESOLUTION.get(display_name, {}), | |
98 | }) | |
99 | ||
100 | return { | |
101 | 'id': video_id, | |
102 | 'title': title, | |
103 | 'formats': formats, | |
104 | 'view_count': room_info.get('totalCount'), | |
105 | 'thumbnail': room_info.get('screenshot'), | |
106 | 'description': room_info.get('contentIntro'), | |
107 | 'http_headers': { | |
108 | 'Origin': 'https://www.huya.com', | |
109 | 'Referer': 'https://www.huya.com/', | |
110 | }, | |
111 | } | |
112 | ||
113 | def encrypt(self, params, stream_info, stream_name): | |
114 | ct = int_or_none(params.get('wsTime'), 16) + random.random() | |
115 | presenter_uid = stream_info['lPresenterUid'] | |
116 | if not stream_name.startswith(str(presenter_uid)): | |
117 | uid = presenter_uid | |
118 | else: | |
119 | uid = int_or_none(ct % 1e7 * 1e6 % 0xffffffff) | |
120 | u1 = uid & 0xffffffff00000000 | |
121 | u2 = uid & 0xffffffff | |
122 | u3 = uid & 0xffffff | |
123 | u = u1 | u2 >> 24 | u3 << 8 | |
124 | params.update({ | |
125 | 'u': str_or_none(u), | |
126 | 'seqid': str_or_none(int_or_none(ct * 1000) + uid), | |
127 | 'ver': '1', | |
128 | 'uuid': int_or_none(ct % 1e7 * 1e6 % 0xffffffff), | |
129 | 't': '100', | |
130 | }) | |
131 | fm = base64.b64decode(params['fm']).decode().split('_', 1)[0] | |
132 | ss = hashlib.md5('|'.join([params['seqid'], params['ctype'], params['t']])) | |
133 | return fm, ss |