a
    hS                     @   s   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mZmZmZmZ ddl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dS )    N   )InfoExtractor   )KNOWN_EXTENSIONSExtractorError
clean_htmlextract_attributesfloat_or_noneint_or_noneparse_filesizestr_or_nonetry_getunified_strdateunified_timestampupdate_url_queryurl_or_noneurljoin)find_elementfind_elementstraverse_objc                   @   s8  e Zd ZdZdgZddddddd	d
ddddddd	gd	gdddddddddddddddddddd g d!dgdgd"d#d$d%d&dd'd(dd)d*d+d,d-d.d/d&d0d1d2d3gd(gd(gd"d4d5d6d7dd8d9dd:d;d<d=d>d?d@d7dAdBdCdDgd9gdEgd"d4gZdFdGddHdIdJgdJgdKdLdMddNdOdGd@dPdJdQdRdSd#gZd[dVdWZdXdY ZdZS )\
BandcampIEzBhttps?://(?P<uploader>[^/]+)\.bandcamp\.com/track/(?P<id>[^/?#&]+)zC<meta property="og:url"[^>]*?content="(?P<url>.*?bandcamp\.com.*?)"z9http://youtube-dl.bandcamp.com/track/youtube-dl-test-songZ c557841d5e50261777a6585648adf439Z
1812978515mp3uL   youtube-dl "'/\ä↭ - youtube-dl "'/\ä↭ - youtube-dl test song "'/\ä↭gOn#@u   youtube-dl "'/\ä↭Z20121129're:https?://f4\.bcbits\.com/img/.+\.jpgiѷPu5   youtube-dl "'/\ä↭ - youtube-dl test song "'/\ä↭zhttps://youtube-dl.bandcamp.comz
youtube-dl)idexttitledurationuploaderupload_date	thumbnail	timestamptracktrack_iduploader_urluploader_idartistsalbum_artistsz@There is a limit of 200 free downloads / month for the test song)urlmd5	info_dictskipz1http://benprunty.bandcamp.com/track/lanius-battleZ
2650410135Zm4azBen Prunty - Lanius (Battle)z
Ben PruntyiK=SZ20140403i<Sgy&1Np@zLanius (Battle)r   z FTL: Advanced Edition Soundtrackzhttps://benprunty.bandcamp.comZ	benprunty)Z
soundtrackZ	chiptunesZ	cinematicZ
electroniczvideo game musicZ
California)r   r   r   r   r   r    r   release_timestamprelease_dater   r!   track_numberr"   albumr#   r$   tagsr%   r&   )r'   r)   z5https://relapsealumni.bandcamp.com/track/hail-to-fireZ fec12ff55e804bb7f7ebeb77a800c8b7Z
2584466013zMastodon - Hail to FireZMastodoni3NZ20111122i*$@Z20040207g(\2^@zHail to Fire   zCall of the Mastodonz"https://relapsealumni.bandcamp.comZrelapsealumniZPhiladelphia)r'   r(   r)   z/https://diskotopia.bandcamp.com/track/safehouseZ 19c5337bca1428afa54129f86a2f6a69Z
1978174799zsubmerse - submerse - SafehouseZsubmersei!BXZ20161203i QGXZ20161207g'1Bc@zsubmerse - Safehouse   z"DSK F/W 2016-2017 Free Compilationzhttps://diskotopia.bandcamp.comZ
diskotopiaZJapanZ
Diskotopiaz>https://www.punknews.org/article/85809/stay-inside-super-sonicZ
2475540375zStay Inside - Super SonicZLungerzStay Insideg/$d@Z20251003g   7Ag  @AzSuper SonicZ20250609Z
stayinsidezhttps://stayinside.bandcamp.com)r   r   r   r.   r&   r%   r   r,   r+   r   r    r!   r"   r-   r   r   r$   r#   tralbumTc                 C   s*   | j | jd| d||d dd||dS )Nzdata-z=(["\'])({.+?})\1z datar   )groupfatal)Z_parse_json_html_search_regex)selfwebpageZvideo_idattrr5    r:   N/mnt/pikpak/tmp/myenv/lib/python3.9/site-packages/yt_dlp/extractor/bandcamp.py_extract_data_attr   s    zBandcampIE._extract_data_attrc           #      C   s  |  |dd\}}| ||}| ||}| |}d }d }d }	d }
g }t|dd t}|r|d}t|tr|	 D ]F\}}t
|sq~|dd\}}||| |d|d	|t|d
 q~|d}t|dp|d}t|d}	t|d}
| ||dd}|dp"i }|dpD|dpD|d}| jd|ddd}t|dpn|d}|d}|rZt|d }| ||d}| ||d}t|dd dd ft}|rZ|d}t|trZ|s|d}|s|d}|s|d}i }|d}t|trt|d D ]B}|d |d! }}td"d# ||fD r0|d$||< q0|	 D ]\}}|d%}|sq|t|d&d'd(tt d) t  i} |d*p|}| j| |d+| d,d-d dd.}!|!sq|t
|!d/}"|"sq||| |"d||||d0t|d1d	|dd2 d3 q||rn| d4| n|}|
st| jd|d d5}
|||||d6| d7|t|d8|
||	|||d9||t|t d:d;hd<t!hfd=S )>Nr   r   c                 S   s   | d d S )N	trackinfor   r:   xr:   r:   r;   <lambda>       z*BandcampIE._real_extract.<locals>.<lambda>file-r   zhttp:none)	format_idr'   r   vcodecacodecZabrr   r"   Z	track_numr   embedFcurrentartistzO<h3 class="albumTitle">[\S\s]*?by\s*<span>\s*<a href="[^>]+">\s*([^>]+?)\s*</a>zalbum artistr4   Zpublish_dateZalbum_publish_dateZfreeDownloadPagezDownloading free downloads pageblobc                 S   s   | d d S )NZdigital_itemsr   r:   r>   r:   r:   r;   r@      rA   c                 S   s   | d d S )NZdownload_itemsr   r:   r>   r:   r:   r;   r@      rA   	downloadsZ	thumb_urldownload_formatsnameZfile_extensionc                 s   s   | ]}t |tV  qd S )N)
isinstancestr).0r?   r:   r:   r;   	<genexpr>   rA   z+BandcampIE._real_extract.<locals>.<genexpr>.r'   z
/download/z/statdownload/z.randi  Zencoding_namezDownloading z JSONc                 S   s   | |  d| dd  S )N{}r   )indexrindex)sr:   r:   r;   r@      rA   )Ztransform_sourcer5   	retry_urldescriptionZsize_mbr   )r'   r   rE   Zformat_notefilesizerF   rG    - )defaultzhttps://z.bandcamp.comZalbum_release_dateZalbum_titletag)cls.)r   r   r   r   r$   r#   r    r+   r   r!   r-   r"   rJ   r.   album_artistformatsr/   )"_match_valid_urlr3   _download_webpager<   Z_og_search_thumbnailr   dictgetrO   itemsr   splitappendZ_proto_relative_urlr
   r   r	   r6   r   rP   listallstripr   replaceinttimerandomZ_download_jsonr   Z_html_search_metar   r   r   )#r7   r'   r   r   r8   r2   r   r"   r!   r-   r   ra   
track_infofile_rE   
format_urlr   Zabr_strrH   rI   rJ   r`   r    Zdownload_linkZdownload_webpagerK   inforL   rM   Zdownload_formats_listfrN   Zstat_urlstatrY   r:   r:   r;   _real_extract   s    





"










zBandcampIE._real_extractN)r2   T)	__name__
__module____qualname__
_VALID_URLZ_EMBED_REGEX_TESTSZ_WEBPAGE_TESTSr<   rv   r:   r:   r:   r;   r      s   a
r   c                       s   e Zd ZdZdZddddddd	d
ddgdd
ddddddgddddddddddd
ddd
dddddddgdgdddgdddddd did!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d)d1d2d3d4d5d6dd7gZe fd8d9Zd:d; Z  Z	S )<BandcampAlbumIEzBandcamp:albumzHhttps?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com/album/(?P<id>[^/?#&]+)z9http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1Z 39bc1eded3476e927c724321ddf116cfZ
1353101989r   zBlazo - Intror   i/NZ20110727ZBlazozhttps://blazo.bandcamp.comg   @ՋAZIntroZblazor   zJazz Format Mixtape vol.1g(\U3@)r   r   r   r   r    r   r   r&   r#   r,   r+   r!   r$   r-   r.   r%   r   r"   )r(   r)   Z 1a2c32e2691474643e912cc6cd4bffaaZ38097443z.Blazo - Kero One - Keep It Alive (Blazo remix)i/Nz&Kero One - Keep It Alive (Blazo remix)r   gmf@)r   r   r   r   r    r   r   r!   r,   r"   r-   r   r#   r.   r$   r&   r%   r+   zjazz-format-mixtape-vol-1z$md5:38052a93217f3ffdc033cd5dbbce2989)r   r   r$   rZ   Zplaylistendz!Bandcamp imposes download limits.)r'   playlistr)   paramsr*   zChttp://nightbringer.bandcamp.com/album/hierophany-of-the-open-gravezHierophany of the Open GraveZnightbringerzhierophany-of-the-open-grave)r   r$   r   	   r'   r)   playlist_mincountz0https://jstrecords.bandcamp.com/album/entropy-epz"Entropy" EPZ
jstrecordsz
entropy-epz$md5:0ff22959c943622972596062f2f366a5)r   r$   r   rZ   r1   z6https://insulters.bandcamp.com/album/we-are-the-plaguezwe-are-the-plaguezWE ARE THE PLAGUEZ	insultersz$md5:b3cf845ee41b2b1141dc7bde9237255f)r   r   r$   rZ   r'   r)   Zplaylist_countc                    s$   t |st|rdS t |S )NF)BandcampWeeklyIEsuitabler   super)r_   r'   	__class__r:   r;   r   z  s    
zBandcampAlbumIE.suitablec           
         s      \}}|p|} |} ||}|d}|sHtd fdd|D }|dphi }	d|||	d|	d|d	S )
Nr=   z#The page doesn't contain any tracksc              
      sN   g | ]F}| d r t|d t t| dp<| d| dqS )r   Z
title_linkr"   r   r   )re   Z
url_resultr   r   Zie_keyr   )rQ   tr7   r'   r:   r;   
<listcomp>  s   
z1BandcampAlbumIE._real_extract.<locals>.<listcomp>rI   r}   r   Zabout)_typer$   r   r   rZ   entries)rb   groupsrc   r<   re   r   )
r7   r'   r$   Zalbum_idZplaylist_idr8   r2   rp   r   rI   r:   r   r;   rv     s$    
zBandcampAlbumIE._real_extract)
rw   rx   ry   IE_NAMErz   r{   classmethodr   rv   __classcell__r:   r:   r   r;   r|     s   1=
Zr|   c                   @   sL   e Zd ZdZdZddddddd	d
dddd	ddiddddgZdd ZdS )r   zBandcamp:weeklyz=https?://(?:www\.)?bandcamp\.com/?\?(?:.*?&)?show=(?P<id>\d+)zhttps://bandcamp.com/?show=224Z 61acc9a002bed93986b91168aa3ab433Z224r   z(BC Weekly April 4th 2017 - Magic Momentsz$md5:5d48150916e8e02d030623a48512c874gQŶ@Z20170404Bandcamp WeeklyzMagic Moments)	r   r   r   rZ   r   r,   seriesepisode
episode_idformatzmp3-128)r'   r(   r)   r~   z)https://bandcamp.com/?blah/blah@&show=228T)r'   Zonly_matchingc                 C   s   |  |}| ||}| ||d}|d | }g }|d  D ]D\}}t|sRq@tD ]}	|	|v rV|	}
 qpqVd }
||||
dd q@|dpd}|d}|r|d	| 7 }|||d
p|dt|ddt	|dd|d||d
S )NrK   Zbcw_dataZaudio_streamrD   )rE   r'   r   rF   Zaudio_titler   subtitler\   descZ
short_descZaudio_durationFZpublished_date)
r   r   rZ   r   Zis_liver,   r   r   r   ra   )
	_match_idrc   r<   rf   r   r   rh   re   r	   r   )r7   r'   Zshow_idr8   rK   showra   rE   rr   Z	known_extr   r   r   r:   r:   r;   rv     sD    


zBandcampWeeklyIE._real_extractN)rw   rx   ry   r   rz   r{   rv   r:   r:   r:   r;   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dddddgZdd  Zd!d" Zd#S )$BandcampUserIEzBandcamp:userzEhttps?://(?!www\.)(?P<id>[^.]+)\.bandcamp\.com(?:/music)?/?(?:[#?]|$)z%https://adrianvonziegler.bandcamp.comZadrianvonzieglerzDiscography of adrianvonziegler)r   r      r   zhttp://dotscale.bandcamp.comZdotscalezDiscography of dotscaler   r   z&https://nightcallofficial.bandcamp.comZnightcallofficialz Discography of nightcallofficial   z'https://steviasphere.bandcamp.com/music/   ZsteviaspherezDiscography of steviasphere)r'   r   r)   z,https://coldworldofficial.bandcamp.com/music   Zcoldworldofficialz Discography of coldworldofficialz3https://nuclearwarnowproductions.bandcamp.com/musici  Znuclearwarnowproductionsz'Discography of nuclearwarnowproductionsc              	   c   sR   t d|pt d|E d H  t|tdddhthdtjhddthfE d H  d S )	NzH<li data-item-id=["\'][^>]+>\s*<a href=["\'](?![^"\'/]*?/merch)([^"\']+)z-<div[^>]+trackTitle["\'][^"\']+["\']([^"\']+)z
music-gridT)r   htmlzdata-client-items.page_url)refindallr   r   r   jsonloadsrP   )r7   r8   r:   r:   r;   _yield_items  s    
zBandcampUserIE._yield_itemsc                 C   s8   |  |}| ||}| j| ||d| t|dS )NzDiscography of )getter)r   rc   Zplaylist_from_matchesr   r   )r7   r'   r   r8   r:   r:   r;   rv     s    
zBandcampUserIE._real_extractN)rw   rx   ry   r   rz   r{   r   rv   r:   r:   r:   r;   r     sR   

	/	r   )r   ro   r   rn   commonr   utilsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Zutils.traversalr   r   r   r   r|   r   r   r:   r:   r:   r;   <module>   s   @   E