]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/huya.py
4 from ..compat
import compat_urlparse
, compat_b64decode
16 from .common
import InfoExtractor
19 class HuyaLiveIE(InfoExtractor
):
20 _VALID_URL
= r
'https?://(?:www\.|m\.)?huya\.com/(?P<id>[^/#?&]+)(?:\D|$)'
24 'url': 'https://www.huya.com/572329',
33 'skip_download': True,
36 'url': 'https://www.huya.com/xiaoyugame',
55 def _real_extract(self
, url
):
56 video_id
= self
._match
_id
(url
)
57 webpage
= self
._download
_webpage
(url
, video_id
=video_id
)
58 json_stream
= self
._search
_regex
(r
'"stream":\s+"([a-zA-Z0-9+=/]+)"', webpage
, 'stream', default
=None)
60 raise ExtractorError('Video is offline', expected
=True)
61 stream_data
= self
._parse
_json
(compat_b64decode(json_stream
).decode(), video_id
=video_id
,
62 transform_source
=js_to_json
)
63 room_info
= try_get(stream_data
, lambda x
: x
['data'][0]['gameLiveInfo'])
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']
71 for stream_info
in stream_info_list
:
72 stream_url
= stream_info
.get('sFlvUrl')
75 stream_name
= stream_info
.get('sStreamName')
76 re_secret
= not screen_type
and live_source_type
in (0, 8, 13)
77 params
= dict(compat_urlparse
.parse_qsl(unescapeHTML(stream_info
['sFlvAntiCode'])))
80 fm
, ss
= self
.encrypt(params
, stream_info
, stream_name
)
81 for si
in stream_data
.get('vMultiStreamInfo'):
82 rate
= si
.get('iBitRate')
84 params
['ratio'] = rate
86 params
.pop('ratio', None)
88 params
['wsSecret'] = hashlib
.md5(
89 '_'.join([fm
, params
['u'], stream_name
, ss
, params
['wsTime']]))
91 'ext': stream_info
.get('sFlvUrlSuffix'),
92 'format_id': str_or_none(stream_info
.get('iLineIndex')),
94 'url': update_url_query(f
'{stream_url}/{stream_name}.{stream_info.get("sFlvUrlSuffix")}',
96 **self
._RESOLUTION
.get(si
.get('sDisplayName'), {}),
99 self
._sort
_formats
(formats
)
105 'view_count': room_info
.get('totalCount'),
106 'thumbnail': room_info
.get('screenshot'),
107 'description': room_info
.get('contentIntro'),
109 'Origin': 'https://www.huya.com',
110 'Referer': 'https://www.huya.com/',
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
)):
120 uid
= int_or_none(ct
% 1e7
* 1e6
% 0xffffffff)
121 u1
= uid
& 0xffffffff00000000
122 u2
= uid
& 0xffffffff
124 u
= u1 | u2
>> 24 | u3
<< 8
127 'seqid': str_or_none(int_or_none(ct
* 1000) + uid
),
129 'uuid': int_or_none(ct
% 1e7
* 1e6
% 0xffffffff),
132 fm
= compat_b64decode(params
['fm']).decode().split('_', 1)[0]
133 ss
= hashlib
.md5('|'.join([params
['seqid'], params
['ctype'], params
['t']]))