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