U
    gWb                     @   s   d dl Z 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
 ddlmZmZmZmZmZmZmZmZmZmZ G dd deZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    N   )InfoExtractor   )aes_ecb_encryptpkcs7_padding)
ExtractorErrorint_or_nonejoin_nonemptystr_or_nonestrftime_or_nonetraverse_objunified_strdateurl_or_noneurljoinvariadicc                   @   sZ   e Zd ZdZdZdZedd Zdd Zi fdd	Z	d
d Z
dd Zdd ZdddZdS )NetEaseMusicBaseIE)standardZhigherZexhighlosslessZhiresZjyeffectjymasterZskyzhttp://music.163.com/api/Fc                 C   s   t | ddS )N  )Zscaler   value r   A/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/neteasemusic.py_kilo_or_none'   s    z NetEaseMusicBaseIE._kilo_or_nonec           	   	   C   s   t j|d|idd}d| d| dd}t| }ttt| d| d| }t	t
|td	}d
|    S )Nheader),:)
separatorsZnobodyZuseZmd5forencryptlatin1z-36cd479b6b5-s   e82ckenh8dichen8zparams=)jsondumpsencodehashlibmd5	hexdigestr   liststrbytesr   hexupper)	selfZapi_path
query_bodycookiesZrequest_textmessageZ
msg_digestdataZ	encryptedr   r   r   _create_eapi_cipher+   s    z&NetEaseMusicBaseIE._create_eapi_cipherc                 K   s   ddddddddddt t d  d	td
dddt| | jdddd hfi}| jtdd| |f| 	d| ||dd
dd | D d|d|S )NZ	undefinedz8.0.0Z140Z
1623435496Z	1920x1080 Zpcr   _r   Z04)ZosverZdeviceIdZappverZversioncodeZ
mobilenameZbuildver
resolutionZ__csrfosZchannelZ	requestIdZMUSIC_Uc                 S   s   | j S Nr   )ir   r   r   <lambda>D       z8NetEaseMusicBaseIE._download_eapi_json.<locals>.<lambda>z!https://interface3.music.163.com/z/eapiz/apizhttps://music.163.comz; c                 S   s   g | ]\}}| d | qS )=r   ).0kvr   r   r   
<listcomp>K   s     z:NetEaseMusicBaseIE._download_eapi_json.<locals>.<listcomp>)RefererCookie)r0   headers)inttimerandomrandintr   Z_get_cookies	_API_BASE_download_jsonr   r1   joinitems)r,   pathvideo_idr-   rA   kwargsr.   r   r   r   _download_eapi_json6   s<    "  z&NetEaseMusicBaseIE._download_eapi_jsonc                 C   s(   | j d|d| d|ddd| dS )Nz/song/enhance/player/url/v1[]Zflac)idslevelZ
encodeTypez!Downloading song URL info: level note)rM   )r,   song_idrQ   r   r   r   _call_player_apiO   s     z#NetEaseMusicBaseIE._call_player_apic              
   C   s   g }|d }| j D ]}t| ||ddd tf}|s: q|d}|r^||kr^|dkr qq||d |dd	t|d
thfd| jhfdthfd |s qq|s| j	ddgd |S )Nidr0   c                 S   s   t |d S )Nurl)r   r3   r=   r   r   r   r8   Z   r9   z5NetEaseMusicBaseIE._extract_formats.<locals>.<lambda>rQ   )r   r   rW   none)rW   	format_idZvcodectypebrsize)extZabrfilesizez5No media links found; possibly due to geo restrictionZCN)Z	countries)
_LEVELSr   rU   anygetappendr(   r   r   Zraise_geo_restricted)r,   infoformatsrT   rQ   songZactual_levelr   r   r   _extract_formatsU   sB    

 


 z#NetEaseMusicBaseIE._extract_formatsc                 C   s   | j | j | ||d| jid}t|dthf}t|dthfpDd}|dkr`| d|  n|dkr|td	| d
| |S )Nr?   )rA   coder/   r2   i2zLogin required to download:    zFailed to get meta info:  )rG   rF   r   rB   r(   Zraise_login_requiredr   )r,   ZendpointrK   rS   resultrh   r/   r   r   r   
_query_apis   s       zNetEaseMusicBaseIE._query_apiNrV   namec              
   #   s`   t |t|ttttf fddfD ]4}t|  }| d| t|t ||thfV  q&d S )Nc                    s   t |  d k	S r6   r   rX   id_keyr   r   r8      r9   z1NetEaseMusicBaseIE._get_entries.<locals>.<lambda>zhttp://music.163.com/#/song?id=)r   r   r(   r)   dictset
url_resultNetEaseMusicIE)r,   Z
songs_data
entry_keysro   Zname_keyrf   rT   r   rn   r   _get_entries~   s    
  zNetEaseMusicBaseIE._get_entries)NrV   rm   )__name__
__module____qualname__r`   rF   Z_GEO_BYPASSstaticmethodr   r1   rM   rU   rg   rl   ru   r   r   r   r   r      s   

r   c                   @   s(  e Zd ZdZdZdZdddddd	d
ddddedgddddddddddddeddgdgdddigidddd d!dd"d#d$d%dddigid&d'ded(d)d*gd)gd+d,d-d.d/dd0d1d2gd3d4d5dddigid6dd0ed1d7d8d9d:d;d<dd=d>gd?d@dAdddigidBdCdedDd>dEd8dFgZdGdH ZdIdJ Z	dKS )Lrs   znetease:songu   网易云音乐zGhttps?://(?:y\.)?music\.163\.com/(?:[#m]/)?song\?.*?\bid=(?P<id>[0-9]+)z)https://music.163.com/#/song?id=550136151Z	550136151mp3zIt's Ok (Live)zcount:10i HZZ20180405z$md5:9fd07059c2ccee3950dc8363429a3135   re:^http.*\.jpgu"   偶像练习生 表演曲目合集u   偶像练习生)rV   r^   titlecreators	timestampupload_datedescriptionduration	thumbnailalbumaverage_ratingalbum_artistsrW   	info_dictz%http://music.163.com/song?id=17241424Z17241424zOpus 28Z20080211i qGi  zPiano Solos Vol. 2zDustin O'Halloranu    [00:05.00]纯音乐，请欣赏
lyricsr^   lrc)rV   r^   r}   r   r   r   r   r   album_artistr   r   r   r~   	subtitleszehttps://y.music.163.com/m/song?app_version=8.8.45&id=95670&uct2=sKnvS4+0YStsWkqsPhFijw%3D%3D&dlt=0846Z b896be78d8d34bd7bb665b26710913ffZ95670u	   国际歌Z19911130i7)z$md5:1ba2f911a2b0aa398479f595224f2141i  u-   伴唱:现代人乐队 合唱:总政歌舞团u   红色摇滚u	   侯牧人u   马备)rV   r^   r}   r   r   r   r   r   	alt_titler   r   r   r   r~   r   )rW   r%   r   z'http://music.163.com/#/song?id=32102397Z 3e909614ce09b1ccef4a3eb205441190Z32102397z	Bad BloodzTaylor SwiftzKendrick LamarZ20150516iiWUz$md5:21535156efb73d6d1c355f95616e285a   )rV   r^   r}   r~   r   r   r   r   r   r   r   r   r   Blocked outside Mainland China)rW   r%   r   skipzHas translated name.z'http://music.163.com/#/song?id=22735043Z22735043u   소원을 말해봐 (Genie)u   少女时代Z20100127i c`Kz$md5:03d1ffebec3139aa4bafe302369269c5   u   说出愿望吧(Genie)zOh!)rV   r^   r}   r~   r   r   r   r   r   r   r   r   r   r   )rS   rW   r   r   c                    s   t |ddthf}t |ddthf}|r0|dkr4d S |sHd|ddgiS d}t||}tt|| d fd	d
|D }|ddg|ddg|ddgdS )Nr   ZlyricZtlyricu    [99:00.00]纯音乐，请欣赏
r   )r0   r^   z*(\[[0-9]{2}:[0-9]{2}\.[0-9]{2,}\])([^\n]+)
c                 3   s0   | ](\}}t | |  |d ddV  qdS )r2   z / delimN)r	   rb   )r;   r   textZtranslation_ts_dictr   r   	<genexpr>   s   z1NetEaseMusicIE._process_lyrics.<locals>.<genexpr>)lyrics_mergedr   Zlyrics_translated)r   r(   refindallrp   rH   )r,   Zlyrics_infooriginalZ
translatedZlyrics_exprZoriginal_ts_textsmergedr   r   r   _process_lyrics   s$     



zNetEaseMusicIE._process_lyricsc                 C   s   |  |}| d| d| d|dd d }| |}| | d| d|d	}|rlt|d
dd|dni }||dt|dpd t|dpd t|dpd d|t|dthfdd| jhfddthfd| jhfddthfdt	hfdS )Nzsong/detail?id=z&ids=%5Bz%5DzDownloading song infosongsr   zsong/lyric?id=z&lv=-1&tv=-1zDownloading lyrics data))r   r   r   r0   Fget_all)r   r   /))Z
transNamesalias.artists.rm   )r   r   .rm   )rV   re   r   r~   r   rm   r   publishTimeZpicUrlr   Zscore)r}   r   r   r   r   r   )
	_match_idrl   rg   r   r   rH   r(   r   r   r   )r,   rW   rT   rd   re   r   Z
lyric_datar   r   r   _real_extract  sN    
  

  


zNetEaseMusicIE._real_extractN)
rv   rw   rx   IE_NAMEIE_DESC
_VALID_URLrB   _TESTSr   r   r   r   r   r   rs      s   _rs   c                   @   sP   e Zd ZdZdZdZdddddd	d
dddddddd	d
ddgZdd ZdS )NetEaseMusicAlbumIEznetease:albumu   网易云音乐 - 专辑z8https?://music\.163\.com/(?:#/)?album\?id=(?P<id>[0-9]+)z*https://music.163.com/#/album?id=133153666Z	133153666u   桃几的翻唱Z20210913u   桃几2021年翻唱合集r|   )rV   r}   r   r   r      rW   r   Zplaylist_mincountz&http://music.163.com/#/album?id=220780Z220780zB'DayZ20060904z$md5:71a74e1d8f392d88cf1bbe48879ad0b0   )rW   r   playlist_countc              
   C   s   |  |}| d| |}| jd|d|ddd}| jd|ddd	| jd
d dD |dtjdd| jd|ddd	t| jd|ddd	d}| j	| 
||f|S )Nzhttps://music.163.com/album?id=z-<textarea[^>]+\bid="song-list-pre-data"[^>]*>metainfoz</textarea>z\[(?s:.+)\])Zend_patternZcontains_patternr}   F)fatalc                 s   s   | ]}d | dV  qdS )z<div[^>]+\bid="album-desc-z"[^>]*>(.*?)</div>Nr   )r;   suffixr   r   r   r   J  s     z4NetEaseMusicAlbumIE._real_extract.<locals>.<genexpr>)moredotr   )flagsr   imager   zmusic:release_datedate)r}   r   r   r   )r   Z_download_webpageZ_search_jsonZ_og_search_propertyZ_html_search_regexr   Sr   Z_html_search_metaplaylist_resultru   )r,   rW   Zalbum_idZwebpager   r   r   r   r   r   @  s,    
       z!NetEaseMusicAlbumIE._real_extractNrv   rw   rx   r   r   r   r   r   r   r   r   r   r   &  s.   r   c                   @   sZ   e Zd ZdZdZdZdddddd	d
dddddd	d
dddddd	d
gZdd ZdS )NetEaseMusicSingerIEznetease:singeru   网易云音乐 - 歌手z9https?://music\.163\.com/(?:#/)?artist\?id=(?P<id>[0-9]+)zSinger has aliases.z&http://music.163.com/#/artist?id=10559Z10559u!   张惠妹 - aMEI;阿妹;阿密特)rV   r}   2   rS   rW   r   r   zSinger has translated name.z'http://music.163.com/#/artist?id=124098Z124098u   李昇基 - 이승기z%Singer with both translated and aliasz(https://music.163.com/#/artist?id=159692Z159692u(   初音ミク - 初音未来;Hatsune Mikuc                 C   sp   |  |}| jd| d| |dd}tt|ddthftt|ddthfdd	id
d}| | |d||S )Nzartist/z?id=zDownloading singer datarR   Zartistrm   )Ztrans)r   .r   ;z - r   ZhotSongs)r   rl   r	   r   r(   r   ru   )r,   rW   Z	singer_idrd   rm   r   r   r   r   p  s    
  z"NetEaseMusicSingerIE._real_extractNr   r   r   r   r   r   R  s4   		r   c                   @   s   e Zd ZdZdZdZdddddgd	d
eddddddddddddgddedddddddddddd dgd!d"d#d$dgZd%d& Zd'S )(NetEaseMusicListIEznetease:playlistu   网易云音乐 - 歌单zPhttps?://music\.163\.com/(?:#/)?(?:playlist|discover/toplist)\?id=(?P<id>[0-9]+)z+http://music.163.com/#/playlist?id=79177352Z79177352zBillboard 2007 Top 100z$md5:12fd0819cab2965b9583ace0f8b7b022u   欧美u   浑然破灭Z67549805zre:\d{8})rV   r}   r   tagsuploaderuploader_idr   r   _   r   zToplist/Charts samplez1https://music.163.com/#/discover/toplist?id=60198Z60198u0   re:美国Billboard榜 [0-9]{4}-[0-9]{2}-[0-9]{2}u   美国Billboard排行榜u   流行u   榜单u   Billboard公告牌Z48171d   r   z2http://music.163.com/#/discover/toplist?id=3733003Z3733003u=   re:韩国Melon排行榜周榜(?: [0-9]{4}-[0-9]{2}-[0-9]{2})?z$md5:73ec782a612711cadc7872d9c1e134fcZ20200109Z2937386u   韩语u   Melon榜单i^)rV   r}   r   r   r   r   r   r   r   c              
   C   s   |  |}| jd||dddddd}t|dd	thfd
thfddthfddthfddthfd| jhfdf}t|ddkr|d dt|dd |d< | j| 	|d|f|S )Nz/v3/playlist/detailz-15000)rV   tnszDownloading playlist inforR   playlistrm   r   r   .creatorZnicknameZuserIdZ
updateTime)r}   r   r   r   r   r   )r   ZspecialType
   r}   rj   r   z%Y-%m-%d)r   Ztracks)
r   rM   r   r(   r
   r   rb   r   r   ru   )r,   rW   Zlist_idrd   r   r   r   r   r     s$    
 




$z NetEaseMusicListIE._real_extractN	rv   rw   rx   r   r   r   rB   r   r   r   r   r   r   r   ~  sV   


+r   c                   @   s   e Zd ZdZdZdZdddddd	gd
ddeeeddddddddgdddeeedddddddddddgdd eeded!
d"gZd#d$ Zd%S )&NetEaseMusicMvIEz
netease:mvu   网易云音乐 - MVz5https?://music\.163\.com/(?:#/)?mv\?id=(?P<id>[0-9]+)z&https://music.163.com/#/mv?id=10958064Z10958064mp4u   交换余生z$md5:e845872cff28820642a2b02eda428feau	   林俊杰Z20200916re:http.*\.jpgil  )rV   r^   r}   r   r~   r   r   r   
view_count
like_countcomment_countr   z#http://music.163.com/#/mv?id=415350Z415350u   이럴거면 그러지말지u!   白雅言自作曲唱甜蜜爱情u	   白娥娟Z20150520   r   )rW   r   r   zThis MV has multiple creators.z&https://music.163.com/#/mv?id=22593543Z22593543u   老北京杀器u   秃子2zu   辉子u   Saber梁维嘉   Z20240618)
rV   r^   r}   r~   r   r   r   r   r   r   )rS   rW   r   c                 C   s   |  |}| d| d|dd }dd |d  D }||t|dpR|d	gd
t|dthfdthdd hfdthfdthfd| jhfdt	hfdt	hfdt	hfdddS )Nzmv/detail?id=z	&type=mp4zDownloading mv infor0   c                 S   s(   g | ] \}}|d | dt |dqS )r   p)rW   r^   rZ   heightr   )r;   brsZmv_urlr   r   r   r>     s   z2NetEaseMusicMvIE._real_extract.<locals>.<listcomp>r   r   Z
artistName)rV   re   r~   rm   )descZ	briefDescc                 S   s   | pd S r6   r   )xr   r   r   r8     r9   z0NetEaseMusicMvIE._real_extract.<locals>.<lambda>r   Zcoverr   Z	playCountZ	likeCountZcommentCount)r}   r   r   r   r   r   r   r   Fr   )
r   rl   rI   r   rb   r(   r   r   r   r   )r,   rW   Zmv_idrd   re   r   r   r   r     s8    

  

	zNetEaseMusicMvIE._real_extractNr   r   r   r   r   r     s`   0r   c                   @   s   e Zd ZdZdZdZdddddd	gd
dddd	dddddddgdddddddddddddgddddd	ddidgZd d! Zd"S )#NetEaseMusicProgramIEznetease:programu   网易云音乐 - 电台节目z:https?://music\.163\.com/(?:#/)?program\?id=(?P<id>[0-9]+)z*http://music.163.com/#/program?id=10109055Z32593346rz   u   不丹足球背后的故事u   喜马拉雅人的足球梦 ...u   大话西藏i{UZ20150613r   i  )	rV   r^   r}   r   r~   r   r   r   r   r   z$This program has accompanying songs.z*http://music.163.com/#/program?id=10141022Z10141022u   滚滚电台的有声节目z$md5:8d594db46cc3e6509107ede70a4aaa3bu   滚滚电台ORZi-UZ20150616)rV   r}   r   r~   r   r   r      r   Z32647209iP  Z
noplaylistT)rS   rW   r   paramsc                 C   s   |  |}| jd| |ddd }t|dthfdthfddthfd	thfd
| jhfd}| j|d oj||d d ddds| |d }t|d d |t|dd| jhfd|S t|d}| j| 	||f|S )Nzdj/program/detail?id=zDownloading program inforR   programrm   r   ZdjZbrandZcoverUrlZ
createTime)r}   r   r   r   r   r   mainSongrV   rf   )Zplaylist_labelZvideo_labelr   )rV   re   r   ))r   )r   .)
r   rl   r   r(   r   r   Z_yes_playlistrg   r   ru   )r,   rW   Z
program_idrd   r   re   r   r   r   r   r   D  s<    
  


 
  
z#NetEaseMusicProgramIE._real_extractNr   r   r   r   r   r     sZ   	 -r   c                   @   s8   e Zd ZdZdZdZdddddd	d
ZdZdd ZdS )NetEaseMusicDjRadioIEznetease:djradiou   网易云音乐 - 电台z:https?://music\.163\.com/(?:#/)?djradio\?id=(?P<id>[0-9]+)z$http://music.163.com/#/djradio?id=42Z42u   声音蔓延z$md5:c7381ebd7989f9f367668a5aee7d5f08)rV   r}   r   (   r   r   c              	      s     |}i }g }tjd jdD ]~} jd j d| d| |d| d}| fdd	|d
 D  |st|d
dddthfdthfdf}|d s" qq" j||f|S )Nr   )startstepz#dj/program/byradio?asc=false&limit=z	&radioId=z&offset=zDownloading dj programs - rR   c              	   3   s2   | ]*}  d |d  t|d |dV  qdS )z"http://music.163.com/#/program?id=rV   rm   N)rr   r   rb   )r;   r   r,   r   r   r   z  s     z6NetEaseMusicDjRadioIE._real_extract.<locals>.<genexpr>ZprogramsZradiorm   r   )r}   r   r   )	r   	itertoolscount
_PAGE_SIZErl   extendr   r(   r   )r,   rW   Zdj_idr   entriesoffsetrd   r   r   r   r   p  s(    
 

z#NetEaseMusicDjRadioIE._real_extractN)	rv   rw   rx   r   r   r   Z_TESTr   r   r   r   r   r   r   a  s   	r   )r$   r   r!   rD   r   rC   commonr   Zaesr   r   utilsr   r   r	   r
   r   r   r   r   r   r   r   rs   r   r   r   r   r   r   r   r   r   r   <module>   s"   0p ,,EPN