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