[ie/facebook] Support more groups URLs (#11576)
[yt-dlp3.git] / yt_dlp / extractor / huya.py
blobf79e032e4a45652d14146d735cdb34a4b26e035e
1 import base64
2 import hashlib
3 import random
4 import re
5 import urllib.parse
7 from .common import InfoExtractor
8 from ..utils import (
9 ExtractorError,
10 int_or_none,
11 parse_duration,
12 str_or_none,
13 try_get,
14 unescapeHTML,
15 unified_strdate,
16 update_url_query,
17 url_or_none,
19 from ..utils.traversal import traverse_obj
22 class HuyaLiveIE(InfoExtractor):
23 _VALID_URL = r'https?://(?:www\.|m\.)?huya\.com/(?!(?:video/play/))(?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 'ext': 'flv',
32 'description': str,
33 'is_live': True,
34 'view_count': int,
36 'params': {
37 'skip_download': True,
39 }, {
40 'url': 'https://www.huya.com/xiaoyugame',
41 'only_matching': True,
44 _RESOLUTION = {
45 '蓝光': {
46 'width': 1920,
47 'height': 1080,
49 '超清': {
50 'width': 1280,
51 'height': 720,
53 '流畅': {
54 'width': 800,
55 'height': 480,
59 def _real_extract(self, url):
60 video_id = self._match_id(url)
61 webpage = self._download_webpage(url, video_id=video_id)
62 stream_data = self._search_json(r'stream:\s', webpage, 'stream', video_id=video_id, default=None)
63 room_info = try_get(stream_data, lambda x: x['data'][0]['gameLiveInfo'])
64 if not room_info:
65 raise ExtractorError('Can not extract the room info', expected=True)
66 title = room_info.get('roomName') or room_info.get('introduction') or self._html_extract_title(webpage)
67 screen_type = room_info.get('screenType')
68 live_source_type = room_info.get('liveSourceType')
69 stream_info_list = stream_data['data'][0]['gameStreamInfoList']
70 if not stream_info_list:
71 raise ExtractorError('Video is offline', expected=True)
72 formats = []
73 for stream_info in stream_info_list:
74 stream_url = stream_info.get('sFlvUrl')
75 if not stream_url:
76 continue
77 stream_name = stream_info.get('sStreamName')
78 re_secret = not screen_type and live_source_type in (0, 8, 13)
79 params = dict(urllib.parse.parse_qsl(unescapeHTML(stream_info['sFlvAntiCode'])))
80 fm, ss = '', ''
81 if re_secret:
82 fm, ss = self.encrypt(params, stream_info, stream_name)
83 for si in stream_data.get('vMultiStreamInfo'):
84 display_name, bitrate = re.fullmatch(
85 r'(.+?)(?:(\d+)M)?', si.get('sDisplayName')).groups()
86 rate = si.get('iBitRate')
87 if rate:
88 params['ratio'] = rate
89 else:
90 params.pop('ratio', None)
91 if bitrate:
92 rate = int(bitrate) * 1000
93 if re_secret:
94 params['wsSecret'] = hashlib.md5(
95 '_'.join([fm, params['u'], stream_name, ss, params['wsTime']]))
96 formats.append({
97 'ext': stream_info.get('sFlvUrlSuffix'),
98 'format_id': str_or_none(stream_info.get('iLineIndex')),
99 'tbr': rate,
100 'url': update_url_query(f'{stream_url}/{stream_name}.{stream_info.get("sFlvUrlSuffix")}',
101 query=params),
102 **self._RESOLUTION.get(display_name, {}),
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/',
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',
136 fm = base64.b64decode(params['fm']).decode().split('_', 1)[0]
137 ss = hashlib.md5('|'.join([params['seqid'], params['ctype'], params['t']]))
138 return fm, ss
141 class HuyaVideoIE(InfoExtractor):
142 _VALID_URL = r'https?://(?:www\.)?huya\.com/video/play/(?P<id>\d+)\.html'
143 IE_NAME = 'huya:video'
144 IE_DESC = '虎牙视频'
146 _TESTS = [{
147 'url': 'https://www.huya.com/video/play/1002412640.html',
148 'info_dict': {
149 'id': '1002412640',
150 'ext': 'mp4',
151 'title': '8月3日',
152 'thumbnail': r're:https?://.*\.jpg',
153 'duration': 14,
154 'uploader': '虎牙-ATS欧卡车队青木',
155 'uploader_id': '1564376151',
156 'upload_date': '20240803',
157 'view_count': int,
158 'comment_count': int,
159 'like_count': int,
163 'url': 'https://www.huya.com/video/play/556054543.html',
164 'info_dict': {
165 'id': '556054543',
166 'ext': 'mp4',
167 'title': '我不挑事 也不怕事',
168 'thumbnail': r're:https?://.*\.jpg',
169 'duration': 1864,
170 'uploader': '卡尔',
171 'uploader_id': '367138632',
172 'upload_date': '20210811',
173 'view_count': int,
174 'comment_count': int,
175 'like_count': int,
179 def _real_extract(self, url: str):
180 video_id = self._match_id(url)
181 video_data = self._download_json(
182 'https://liveapi.huya.com/moment/getMomentContent', video_id,
183 query={'videoId': video_id})['data']['moment']['videoInfo']
185 formats = []
186 for definition in traverse_obj(video_data, ('definitions', lambda _, v: url_or_none(v['url']))):
187 formats.append({
188 'url': definition['url'],
189 **traverse_obj(definition, {
190 'format_id': ('defName', {str}),
191 'width': ('width', {int_or_none}),
192 'height': ('height', {int_or_none}),
193 'filesize': ('size', {int_or_none}),
197 return {
198 'id': video_id,
199 'formats': formats,
200 **traverse_obj(video_data, {
201 'title': ('videoTitle', {str}),
202 'thumbnail': ('videoCover', {url_or_none}),
203 'duration': ('videoDuration', {parse_duration}),
204 'uploader': ('nickName', {str}),
205 'uploader_id': ('uid', {str_or_none}),
206 'upload_date': ('videoUploadTime', {unified_strdate}),
207 'view_count': ('videoPlayNum', {int_or_none}),
208 'comment_count': ('videoCommentNum', {int_or_none}),
209 'like_count': ('favorCount', {int_or_none}),