o
    iC#                     @   s   d dl Z d dlZd dlZd dlZd dlZddlmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZmZ ddlmZ G dd deZG d	d
 d
eZdS )    N   )InfoExtractor   )
ExtractorError
clean_htmlint_or_noneparse_durationstr_or_nonetry_getunescapeHTML
update_urlupdate_url_queryurl_or_none)traverse_objc                   @   sn   e Zd ZdZdZdZddedededd	did
dddgZddddddddddZ	dd Z
dd ZdS )
HuyaLiveIEzLhttps?://(?:www\.|m\.)?huya\.com/(?!(?:video/play/))(?P<id>[^/#?&]+)(?:\D|$)z	huya:liveu   虎牙直播zhttps://www.huya.com/572329572329flvT)idtitleextdescriptionis_live
view_countskip_download)url	info_dictparamszhttps://www.huya.com/xiaoyugame)r   only_matchingi  i8  )widthheighti   i  i   i  )u   蓝光u   超清u   流畅c                 C   s  |  |}| j||d}| jd|d|d d}t|dd }|s%tddd	|d
p3|dp3| |}|d}|d}|d d d }	|	sNtddd	g }
|	D ]}|d}|s\qR|d}| og|dv }ttj	
t|d }d\}}|r| |||\}}|dD ]f}td|d \}}|d}|r||d< n|dd  |rt|d }|rtd||d |||d g|d < |
|d!t|d"|t| d#| d$|d! |d%d&| j|i  qqR|||
|d'|d(|d)d*d+d,d-S ).N)video_idz	stream:\sstream)r    defaultc                 S   s   | d d d S )Ndatar   gameLiveInfo )xr%   r%   I/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/huya.py<lambda>@   s    z*HuyaLiveIE._real_extract.<locals>.<lambda>zCan not extract the room infoT)expectedroomNameintroduction
screenTypeliveSourceTyper#   r   gameStreamInfoListzVideo is offlinesFlvUrlsStreamName)r         sFlvAntiCode) r4   vMultiStreamInfoz(.+?)(?:(\d+)M)?sDisplayNameiBitRateratio  _uwsTimewsSecretsFlvUrlSuffix
iLineIndex/.query)r   	format_idtbrr   
totalCount
screenshotcontentIntrozhttps://www.huya.comzhttps://www.huya.com/)OriginReferer)r   r   formatsr   	thumbnailr   http_headers)	_match_id_download_webpage_search_jsonr
   r   get_html_extract_titledicturllibparse	parse_qslr   encryptre	fullmatchgroupspopinthashlibmd5joinappendr	   r   _RESOLUTION)selfr   r    webpagestream_data	room_infor   screen_typelive_source_typestream_info_listrK   stream_info
stream_urlstream_name	re_secretr   fmsssidisplay_namebitraterater%   r%   r'   _real_extract<   sx   







zHuyaLiveIE._real_extractc                 C   s   t |ddt  }|d }|t|s|}n
t |d d d }|d@ }|d@ }|d@ }	||d	? B |	d
> B }
|t|
tt |d | dt |d d d dd t|d 	 
ddd }td|d |d |d g}||fS )Nr<      lPresenterUidg    cAg    .Al    l        i    r1   r9   1100)r;   seqidveruuidtrm   r:   r   r   |ry   ctyper|   )r   rQ   random
startswithstrupdater	   base64	b64decodedecodesplitr]   r^   r_   )rb   r   ri   rk   ctpresenter_uiduidu1u2u3r;   rm   rn   r%   r%   r'   rW   w   s&   "zHuyaLiveIE.encryptN)__name__
__module____qualname__
_VALID_URLIE_NAMEIE_DESCr   r\   _TESTSra   rs   rW   r%   r%   r%   r'   r      s>    	;r   c                   @   s   e Zd ZdZdZdZdddddgd	d
ddeeedddddddddgdddddeeeddddddddddged edd!d"d#d$ed%dgZd&efd'd(Z	d)S )*HuyaVideoIEz9https?://(?:www\.)?huya\.com/video/play/(?P<id>\d+)\.htmlz
huya:videou   虎牙视频z/https://www.huya.com/video/play/1002412640.html
1002412640mp4u   8月3日u   主机游戏g      ,@u   虎牙-ATS欧卡车队青木
156437615120240803zre:https?://.+\.jpgif)r   r   r   
categoriesdurationuploaderuploader_idupload_dater   comment_count
like_countrL   	timestamp)r   r   z.https://www.huya.com/video/play/556054543.html	556054543u   我不挑事 也不怕事u   英雄联盟z$md5:58184869687d18ce62dc7b4b2ad21201g      @u   卡尔	36713863220210811zcount:4ina)r   r   r   r   r   r   r   r   r   r   r   r   tagsrL   r   z/https://www.huya.com/video/play/1063345618.html
1063345618u6   峡谷第一中！黑铁上钻石顶级教学对抗elog    @iIh20250611u   北枫CC
2183525275)r   r   r   r   r   r   r   rL   r   r   r   r   r   r   c                 C   sP  |  |}| jd|d|idd d }g }t|dddd	 fD ]7}| j|d
 |ddd}|D ]"}|jd$i t|dthfdthfdthfdthfdthfd q.|| q||dt|dthfdtht	fdthfdthfdt|ddthfdtht	t
t	fdthfddtht	t
t	fdthtd dhtfd thfd!thfd"thfd#fS )%Nz0https://liveapi.huya.com/moment/getMomentContentvideoIdrB   r#   moment	videoInfodefinitionsc                 S   s   t |d S )Nm3u8)r   )r:   vr%   r%   r'   r(      s    z+HuyaVideoIE._real_extract.<locals>.<lambda>r   r   F)fatalsizedefNamer   
definitionr   )filesizerD   r   qualityr   )r   rK   commentCountcontent
favorCountcTime)r   r   r   r   
videoTitlecategoryvideoDurationr   .)videoBigCover
videoCovernickNamer   videoPlayNum)r   r   r   r   rL   r   r   r   r%   )rN   _download_jsonr   _extract_m3u8_formatsr   r   r   extendr   filterallr   r   r   anyr	   )rb   r   r    r   rK   r   fmtsfmtr%   r%   r'   rs      sX   



	zHuyaVideoIE._real_extractN)
r   r   r   r   r   r   r\   r   r   rs   r%   r%   r%   r'   r      sp    8r   )r   r]   r   rX   urllib.parserT   commonr   utilsr   r   r   r   r	   r
   r   r   r   r   utils.traversalr   r   r   r%   r%   r%   r'   <module>   s    0w