o
    i[                     @   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 ddl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G dd deZG dd deZG dd deZdS )    N   )InfoExtractor   )ExtractorErrorint_or_nonejoin_nonemptytry_callurl_or_noneurljoin)traverse_objc                   @   s   e Zd ZdZdgZdZdZi dddddd	d
ddddddddddddddddddddddd d!d"d#d$d%d&d'd(d&d)d*d+d,d-d.
Zd/d0 Zd<d2d3Z	d4d5 Z
d6d7 Zd8d9 Zd:d; Zd1S )=ZingMp3BaseIEzThttps?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:%s))/[^/?#]+/(?P<id>\w+)(?:\.html|\?)VNzhttps://zingmp3.vn2   bai-hatz/api/v2/page/get/songembed
video-clipz/api/v2/page/get/videolyricz/api/v2/lyric/get/lyricsong-streamingz/api/v2/song/get/streaming	liveradioz/api/v2/livestream/get/infoepsz /api/v2/page/get/podcast-episodeepisode-streamingz%/api/v2/podcast/episode/get/streamingplaylistz/api/v2/page/get/playlistalbumpgrz /api/v2/page/get/podcast-programpgr-listz /api/v2/podcast/episode/get/listcgrz!/api/v2/page/get/podcast-categorycgr-listz(/api/v2/podcast/program/get/list-by-catecgrsz#/api/v2/page/get/podcast-categories
zing-chartz/api/v2/page/get/chart-homezing-chart-tuanz/api/v2/page/get/week-chartz!/api/v2/page/get/newrelease-chartz/api/v2/video/get/listz/api/v2/page/get/artistz/api/v2/song/get/listz/api/v2/page/get/hub-detailz/api/v2/chart/get/new-releasez/api/v2/page/get/top-100z(/api/v2/podcast/program/get/list-by-typez'/api/v2/podcast/program/get/top-episode)
moi-phat-hanhzthe-loai-videoinfo-artistuser-list-songuser-list-videohubnew-releasetop100podcast-newtop-podcastc                 C   s   | j | }|ddi tddd t| D   }i |dt	
d| |  tj d}| j | d	tj| S )
Nctime1 c                 s   s"    | ]\}}| d | V  qdS )=N ).0kvr-   r-   L/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/zingmp3.py	<genexpr>>   s     z)ZingMp3BaseIE._api_url.<locals>.<genexpr> X5BM3w8N7MKozC0B85o4KMlzLZKhV00ys    acOrvUS15XRW2o9JksiK1KgQ6Vbds8ZW)apiKeysig?)
_API_SLUGSupdatehashlibsha256joinsorteditemsencode	hexdigesthmacnewsha512_DOMAINurllibparse	urlencode)selfurl_typeparamsapi_slugr:   datar-   r-   r1   _api_url:   s"   
zZingMp3BaseIE._api_urlNc                 K   sD   | j | |||p|dfdd| di|}|pi dp!i S )NidnotezDownloading z JSON metadatarK   )_download_jsonrL   get)rG   rH   rI   
display_idkwargsrespr-   r-   r1   	_call_apiG   s   
zZingMp3BaseIE._call_apic                 C   s*   | j s| j| dddid dd d S d S )Nr   rM   r+   zUpdating cookies)rN   )_cookies_passed_request_webpagerL   )rG   r-   r-   r1   _real_initializeM   s
   
zZingMp3BaseIE._real_initializec                 c   s.    t |dpg D ]}| t| j|V  qd S )N).link)r   
url_resultr
   rC   )rG   r=   urlr-   r-   r1   _parse_itemsR   s   zZingMp3BaseIE._parse_itemsc                 C   s   t d)Nz-This method must be implemented by subclasses)NotImplementedError)rG   id_rH   pager-   r-   r1   _fetch_pageV   s   zZingMp3BaseIE._fetch_pagec                 #   st    d t dD ]/}| |||t| d} t|7  |E d H  dr4t fddr7 d S qd S )Nr   r   r=   hasMorec                      s    d kS )Ntotalr-   r-   countrK   r-   r1   <lambda>`       z+ZingMp3BaseIE._paged_list.<locals>.<lambda>)	itertoolsrc   r_   listr[   rP   lenr   )rG   _idrH   r^   entriesr-   rb   r1   _paged_listY   s   
zZingMp3BaseIE._paged_list)N)__name__
__module____qualname___VALID_URL_TMPL_GEO_COUNTRIESrC   	_PER_PAGEr7   rL   rT   rW   r[   r_   rk   r-   r-   r-   r1   r      sr    	"
r   c                   @   s   e Zd Zejd ZdZdZdddddd	d
ddigidddddd
ddddddd	dddddd	ddddddd	ddd d!d 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	d0S )1	ZingMp3IEzbai-hat|video-clip|embed|epszingmp3z
zingmp3.vnz;https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html ead7ae13693b3205cbc89536a077daedZWZB9WABu
   Xa Mãi Xamp3zre:^https?://.+\.jpgoriginextlrc   u	   Bảo ThyzSpecial Album)
rM   titlerx   	thumbnail	subtitlesdurationtrackartistr   album_artist)rZ   md5	info_dictzGhttps://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html 92c6e7a019f06b4682a6c35ae5785fabZO8ZF7C7u   Sương Hoa Đưa Lốimp4   z
K-ICM, RYOu    Sương Hoa Đưa Lối (Single))	rM   r{   rx   r|   r~   r   r   r   r   zOhttps://zingmp3.vn/bai-hat/Nguoi-Yeu-Toi-Lanh-Lung-Sat-Da-Mr-Siro/ZZ6IW7OU.html 3e9f7a9bd0d965573dbff8d7c68b629dZZ6IW7OUu*   Người Yêu Tôi Lạnh Lùng Sắt Đái/  zMr. Sirou3   Người Yêu Tôi Lạnh Lùng Sắt Đá (Single)z>https://zingmp3.vn/eps/Cham-x-Ban-Noi-Goi-La-Nha/ZZD9ACWI.html d52f9f63e2631e004e4f15188eedcf80ZZD9ACWIu$   Chạm x Bạn - Nơi Gọi Là Nhài  zOn AirzTop Podcast)	rM   r{   rx   r~   r|   r   r   r   r   z2https://zingmp3.vn/embed/song/ZWZEI76B?start=falseTrZ   only_matchingz:https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.htmlc                 C   s  |  |dd\}}| |d|i}|dp|}|dkr9|d}| jd|dtd|iidd	d
|d< n|dkrF| dd|i}n| dd|i}g }|pSi  D ]L\}}	|	r`|	dkraqV|dvrv|d|t	|| 
|	dd qV|	 D ]'\}
}|sqz|dkr|| j||d|dd qz|d|
 |t	|
d qzqV|s|ddkr| j| jdd n| d |dp| jdd|iddd }|t|d!d"t|d#d$t	|d%t|d!d"t|d&d'd(t|d)d*dd+t|d,d(dd+||rd-d.|igid/
S d d/
S )0NrM   typeencodeIdr   	streamingz4http://api.mp3.zing.vn/api/mobile/video/getvideoinforequestdatazDownloading mp4 JSON metadata)queryrN   sourcer   r   r   r   VIP)r   hlsrv   none)rx   	format_idtbrrZ   vcodecr   F)m3u8_idfatalzmp4-)r   rZ   heightmsgz5Sorry, this content is not available in your country.T)	countriesmetadata_availablez"The song is only for VIP accounts.r   r   filer{   aliasr|   
thumbnailMr~   artistsNamesartists_names)artistsr   name)r   )r   r{   )genresr   r   get_all)r   )r   r   rw   rZ   )
rM   r{   r|   r~   r   r   r   r   formatsr}   )_match_valid_urlgrouprT   rP   rO   jsondumpsr=   appendr   _proto_relative_urlextend_extract_m3u8_formatsraise_geo_restrictedrp   raise_no_formatsr   )rG   rZ   song_idrH   itemitem_idr   r   r/   r0   res	video_urlr   r-   r-   r1   _real_extract   sx   


$


zZingMp3IE._real_extractN)
rl   rm   rn   r   ro   
_VALID_URLIE_NAMEIE_DESC_TESTSr   r-   r-   r-   r1   rr   d   s    
Err   c                   @   sV   e Zd Zejd Zdddddddd	d
dddddddddgZdZdd ZdS )ZingMp3AlbumIEzalbum|playlistzZhttps://zingmp3.vn/album/Ca-Phe-Quan-Quen-Hoang-Dung-My-Anh-Da-LAB-Thinh-Suy/ZOC7WUZC.htmlZOC7WUZCu   Cà Phê Quán QuenrM   r{   
   rZ   r   playlist_mincountzQhttps://zingmp3.vn/album/Nhung-Bai-Hat-Hay-Nhat-Cua-Mr-Siro-Mr-Siro/ZWZAEZZD.htmlZWZAEZZDu+   Những Bài Hát Hay Nhất Của Mr. Siro   zChttp://mp3.zing.vn/playlist/Duong-Hong-Loan-apollobee/IWCAACCB.htmlTr   zKhttps://zingmp3.vn/album/Lau-Dai-Tinh-Ai-Bang-Kieu-Minh-Tuyet/ZWZBWDAF.htmlzzingmp3:albumc                 C   sP   |  |dd\}}| |d|i}| | t|dt|ddt|ddS )NrM   r   )songr=   r   r   r{   )r   r   rT   playlist_resultr[   r   rG   rZ   r   rH   rK   r-   r-   r1   r     s   zZingMp3AlbumIE._real_extractN)	rl   rm   rn   r   ro   r   r   r   r   r-   r-   r-   r1   r      s.    
r   c                   @   sX   e Zd ZdZdddidddddiddd	dd
idddddiddgZdZdd ZdS )ZingMp3ChartHomeIEzkhttps?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:zing-chart|moi-phat-hanh|top100|podcast-discover))/?(?:[#?]|$)zhttps://zingmp3.vn/zing-chartrM   r   d   r   z https://zingmp3.vn/moi-phat-hanhr    zhttps://zingmp3.vn/top100r&   r   z#https://zingmp3.vn/podcast-discoverpodcast-discover   zzingmp3:chart-homec                 C   s   |  |}d|i}|dkrd|d< | ||}g }|dkr+|t|dddthf n|dkr=|t|d	ddthf n|t|ddthf | | ||S )
NrM   r   discoverr   r&   .r=   r   RTChart)	_match_idrT   r   r   dictr   r[   )rG   rZ   rH   rI   rK   r=   r-   r-   r1   r   +  s   
z ZingMp3ChartHomeIE._real_extractN)rl   rm   rn   r   r   r   r   r-   r-   r-   r1   r     s2    r   c                   @   sV   e Zd Zej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 )ZingMp3WeekChartIEr   zzingmp3:week-chartzAhttps://zingmp3.vn/zing-chart-tuan/Bai-hat-Viet-Nam/IWZ9Z08I.htmlIWZ9Z08Izzing-chart-vnr   r   r   z>https://zingmp3.vn/zing-chart-tuan/Bai-hat-US-UK/IWZ9Z0BW.htmlIWZ9Z0BWzzing-chart-usz=https://zingmp3.vn/zing-chart-tuan/Bai-hat-KPop/IWZ9Z0BO.htmlIWZ9Z0BOzzing-chart-koreac              	   C   sL   |  |dd\}}| |d|i}| | |d |d|dd S )NrM   r   r=   zzing-chart-countryr+   )r   r   rT   r   r[   rP   r   r-   r-   r1   r   U  s
   z ZingMp3WeekChartIE._real_extractN)	rl   rm   rn   r   ro   r   r   r   r   r-   r-   r-   r1   r   ;  s.    
r   c                   @   sh   e 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dddgZdd Zdd ZdS )ZingMp3ChartMusicVideoIEz]https?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>the-loai-video)/(?P<regions>[^/]+)/(?P<id>[^\.]+)zzingmp3:chart-music-videoz8https://zingmp3.vn/the-loai-video/Viet-Nam/IWZ9Z08I.htmlr   zthe-loai-video_Viet-Namr   i  r   z5https://zingmp3.vn/the-loai-video/Au-My/IWZ9Z08O.htmlIWZ9Z08Ozthe-loai-video_Au-My(   z8https://zingmp3.vn/the-loai-video/Han-Quoc/IWZ9Z08W.htmlIWZ9Z08Wzthe-loai-video_Han-Quoc   z9https://zingmp3.vn/the-loai-video/Khong-Loi/IWZ9Z086.htmlIWZ9Z086zthe-loai-video_Khong-Loir   c                 C   s   |  ||d|| jdS )NgenrerM   r   r^   rc   rT   rq   )rG   r   rH   r^   r-   r-   r1   r_   }  s   z$ZingMp3ChartMusicVideoIE._fetch_pagec                 C   s:   |  |ddd\}}}| | |||| d| S )NrM   regionsr   _)r   r   r   rk   )rG   rZ   r   r   rH   r-   r-   r1   r     s    z&ZingMp3ChartMusicVideoIE._real_extractNrl   rm   rn   r   r   r   r_   r   r-   r-   r-   r1   r   \  s<    r   c                	   @   s   e 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	ddddddd	dddidd	dddidd	gZdd Zdd ZdS )ZingMp3UserIEzhhttps?://(?:mp3\.zing|zingmp3)\.vn/(?P<user>[^/]+)/(?P<type>bai-hat|single|album|video|song)/?(?:[?#]|$)zzingmp3:userz"https://zingmp3.vn/Mr-Siro/bai-hatIWZ98609zMr. Siro - bai-hatz$md5:5bdcf45e955dc1b8d7f518f322ffef36rM   r{   description[   r   z https://zingmp3.vn/Mr-Siro/albumzMr. Siro - album   z!https://zingmp3.vn/Mr-Siro/singlezMr. Siro - singler   z https://zingmp3.vn/Mr-Siro/videozMr. Siro - video   z#https://zingmp3.vn/new-release/songrM   znew-release-songr   z$https://zingmp3.vn/new-release/albumznew-release-albumc                 C   s(   |dkrdnd}|  ||d|| jdS )Nr   r"   r#   r   r   r   )rG   user_idrH   r^   r-   r-   r1   r_     s   zZingMp3UserIE._fetch_pagec              	      s   |  |dd\}}|sd}| jdi |d|id}|dkr:|dv r:| d	| }| | | jdd|i|d
|S |dv rG| |d |}n|dkrMdnd | t|d fddddf}| ||d t|d|dd|dS )Nuserr   r   r!   r   )r   r%   )r   r   -)rI   rQ   )r   videorM   r   aAlbumaSinglesectionsc                    s   |d  kS )N	sectionIdr-   r   r0   
section_idr-   r1   rd     re   z-ZingMp3UserIE._real_extract.<locals>.<lambda>r=   .r   z - )delim	biography)	r   r   rT   r   r[   rk   r   r   rP   )rG   rZ   r   rH   	user_infori   rj   r-   r   r1   r     s*   zZingMp3UserIE._real_extractNr   r-   r-   r-   r1   r     sX    				.	r   c                   @   sD   e Zd ZdZdZddddddd	d
dddddd	gZdd ZdS )ZingMp3HubIEzzingmp3:hubzIhttps?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>hub)/[^/?#]+/(?P<id>[^./?#]+)z-https://zingmp3.vn/hub/Nhac-Moi/IWZ9Z0CA.htmlIWZ9Z0CAu   BXH Nhạc Mớiz$md5:1cc31b68a6f746427b07b2756c22a558r   r   r   z.https://zingmp3.vn/hub/Nhac-Viet/IWZ9Z087.htmlIWZ9Z087u   Nhạc Việtz$md5:acc976c8bdde64d5c6ee4a92c39f7a77r   c                 C   s^   |  |dd\}}| |d|i}| t|ddd ddf}| |||d|d	S )
NrM   r   r   c                 S   s   |d dkS )Nr   r$   r-   r   r-   r-   r1   rd     re   z,ZingMp3HubIE._real_extract.<locals>.<lambda>r=   .r{   r   )r   r   rT   r[   r   r   rP   )rG   rZ   r   rH   
hub_detailrj   r-   r-   r1   r     s   zZingMp3HubIE._real_extractNrl   rm   rn   r   r   r   r   r-   r-   r-   r1   r     s&    	r   c                   @   s`   e Zd ZdZdZdddddeded	d
ddiddddddeded	d
ddidgZdd ZdS )ZingMp3LiveRadioIEzzingmp3:liveradiozShttps?://(?:mp3\.zing|zingmp3)\.vn/(?P<type>(?:liveradio))/(?P<id>\w+)(?:\.html|\?)z*https://zingmp3.vn/liveradio/IWZ979UB.htmlIWZ979UBz
re:^V\-POPz$md5:aa857f8a91dc9ce69e862a809e4bdc10r   zre:^https?://.*\.jpgis_live)rM   r{   r   rx   
view_countr|   
like_countlive_statusskip_downloadT)rZ   r   rI   z*https://zingmp3.vn/liveradio/IWZ97CWB.htmlIWZ97CWBzre:^Live\s247z$md5:d41d8cd98f00b204e9800998ecf8427em4ac              	   C   s   |  |dd\}}| |d|i}|d}|s tddd| j||dd\}}|d||d	t|d
dthfdthfdthfddddS )Nr   rM   r   zThis radio is offline.T)expectedFr   )rM   r   r   r}   r{   )r|   r   
thumbnailV
thumbnailHactiveUserstotalReactionr   )r{   r|   r  r  r   r   )	r   r   rT   rP   r   #_extract_m3u8_formats_and_subtitlesr   r	   r   )rG   rZ   rH   live_radio_idinfomanifest_urlfmtsr}   r-   r-   r1   r     s*   
z ZingMp3LiveRadioIE._real_extractN)rl   rm   rn   r   r   intr   r   r-   r-   r-   r1   r     s>     r   c                   @   sP   e Zd ZdZejd Zddddddd	d
ddddd	gZdd Zdd Z	dS )ZingMp3PodcastEpisodeIEzzingmp3:podcast-episodezpgr|cgrz6https://zingmp3.vn/pgr/Nhac-Moi-Moi-Ngay/68Z9W66B.html68Z9W66Bu   Nhạc Mới Mỗi Ngàyz$md5:2875dfa951f8e5356742f1610cf20691r   r   r   z,https://zingmp3.vn/cgr/Am-nhac/IWZ980AO.htmlIWZ980AOu
   Âm nhạcr   r   c                 C   s   |  |||| jdS )N)rM   r^   rc   r   )rG   eps_idrH   r^   r-   r-   r1   r_   H  s
   z#ZingMp3PodcastEpisodeIE._fetch_pagec                 C   sZ   |  |dd\}}| |d|i}| ||dkrdnd}| |||d|dS )NrM   r   r   r   r   r{   r   )r   r   rT   rk   r   rP   )rG   rZ   
podcast_idrH   podcast_inforj   r-   r-   r1   r   O  s   z%ZingMp3PodcastEpisodeIE._real_extractN)
rl   rm   rn   r   r   ro   r   r   r_   r   r-   r-   r-   r1   r  4  s&    
	r  c                   @   sJ   e Zd ZdZdZdddiddddd	id
ddddiddgZdd ZdS )ZingMp3PodcastIEzzingmp3:podcastzVhttps?://(?:mp3\.zing|zingmp3)\.vn/(?P<id>(?:cgr|top-podcast|podcast-new))/?(?:[#?]|$)zhttps://zingmp3.vn/cgrrM   r      r   zhttps://zingmp3.vn/top-podcastr(      zhttps://zingmp3.vn/podcast-newr'   r   c                 C   sP   |  |}d|i}|dkrd|d< | |dkrdn||d }| | ||S )NrM   r'   rA   r   r   r   r=   )r   rT   r   r[   )rG   rZ   rH   rI   r=   r-   r-   r1   r   n  s   
zZingMp3PodcastIE._real_extractNr   r-   r-   r-   r1   r  W  s(    r  )r9   r@   rf   r   urllib.parserD   commonr   utilsr   r   r   r   r	   r
   utils.traversalr   r   rr   r   r   r   r   r   r   r   r  r  r-   r-   r-   r1   <module>   s(     Q 
!-!.S9#