o
    iU                     @   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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   )ExtractorError
clean_htmlextract_attributesfloat_or_noneformat_fieldint_or_nonejoin_nonemptyparse_filesizeparse_qsstr_or_nonestrftime_or_nonetry_getunified_timestampupdate_url_queryurl_or_noneurljoin)find_elementfind_elementstraverse_objc                   @   sX  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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/d0d1g d2dgdgd3d4d5d6i dd7dddd8dd9dddd:dd;d!d<d#d=d$d>d&d?d(d@d*d7d+dAd-dBd/dCd1dDgd9gd9gd3dEdFdGi ddHddddIddJddddKddLd!dMd#dNd$dOd&dPd(dQd*dHd+dRd-dSd/dTd1dUgdJgdVgd3dEgZdWi ddXddddYd+dZd[d\gd]d\gd$d^d#d_d!d`ddddad&dbd*dXd(dQddcdd\d/ddd-deid4gZdmdhdiZdjdk ZdlS )n
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-song c557841d5e50261777a6585648adf439
1812978515mp3uL   youtube-dl "'/\ä↭ - youtube-dl "'/\ä↭ - youtube-dl test song "'/\ä↭gOn#@u   youtube-dl "'/\ä↭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-battler   
2650410135r   m4ar    zBen Prunty - Lanius (Battle)r"   z
Ben Pruntyr$   r%   iK=Sr#   20140403release_timestampi<Srelease_dater!   gy&1Np@r&   zLanius (Battle)track_numberr   r'   albumz FTL: Advanced Edition Soundtrackr(   zhttps://benprunty.bandcamp.comr)   	benpruntytags)
soundtrack	chiptunes	cinematic
electroniczvideo game music
California)r*   r+   )r,   r.   z5https://relapsealumni.bandcamp.com/track/hail-to-fire fec12ff55e804bb7f7ebeb77a800c8b7
2584466013zMastodon - Hail to FireMastodoni3N20111122i*$@20040207g(\2^@zHail to Fire   zCall of the Mastodonz"https://relapsealumni.bandcamp.comrelapsealumniPhiladelphia)r,   r-   r.   z/https://diskotopia.bandcamp.com/track/safehouse 19c5337bca1428afa54129f86a2f6a69
1978174799zsubmerse - submerse - Safehousesubmersei!BX20161203i QGX20161207g'1Bc@zsubmerse - Safehouse   z"DSK F/W 2016-2017 Free Compilationzhttps://diskotopia.bandcamp.com
diskotopiaJapan
Diskotopiaz>https://www.punknews.org/article/85809/stay-inside-super-sonic
2475540375zStay Inside - Super SonicLungerr+   zStay Insider*   g/$d@20251003g   7Ag  @AzSuper Sonic20250609
stayinsidezhttps://stayinside.bandcamp.comtralbumTc                 C   s*   | j | jd| d||d dd||dS )Nzdata-z=(["\'])({.+?})\1z datar   )groupfatal)_parse_json_html_search_regex)selfwebpagevideo_idattrrW    r^   M/home/ubuntu/.local/lib/python3.10/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rb|	 D ]#\}}t
|sGq>|dd\}}||| |d|d	|t|d
 q>|d}t|dpq|d}t|d}	t|d}
| ||dd}|dpi }|dp|dp|d}| jd|ddd}t|dp|d}|d}|rt|d }| ||d}| ||d}t|dd dd ft}|r|d}t|tr|s|d}|s|d}|s|d}i }|d}t|tr2|d D ]!}|d |d!}}td"d# ||fD r0|d$||< q|	 D ]n\}}|d%}|sDq6t|d&d'd(tt d) t  i} |d*p`|}| j| |d+| d,d-d dd.}!|!svq6t
|!d/}"|"sq6|| |"d||||d0t|d1d	|dd2 d3 q6|r| d4| n|}|
st| jd|d d5}
i d|d|d6|d|d7|d8d9| d:d;|d<t|d=d|
d>|d?|	d|d|d@|dAdB|dC|dDt|t dEdFhdGt!hfS )HNr   r"   c                 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acodecabrr    r'   	track_numr!   embedFcurrentartistzO<h3 class="albumTitle">[\S\s]*?by\s*<span>\s*<a href="[^>]+">\s*([^>]+?)\s*</a>zalbum artistrV   publish_datealbum_publish_datefreeDownloadPagezDownloading free downloads pageblobc                 S   ra   )Ndigital_itemsr   r^   rc   r^   r^   r_   re      rf   c                 S   ra   )Ndownload_itemsr   r^   rc   r^   r^   r_   re      rf   	downloads	thumb_urldownload_formatsnamefile_extensionc                 s   s    | ]}t |tV  qd S )N)
isinstancestr).0rd   r^   r^   r_   	<genexpr>   s    z+BandcampIE._real_extract.<locals>.<genexpr>.r,   z
/download/z/statdownload/z.randi  encoding_namezDownloading z JSONc                 S   s   | |  d| dd  S )N{}r   )indexrindex)sr^   r^   r_   re      s    )transform_sourcerW   	retry_urldescriptionsize_mbr   )r,   r   rj   format_notefilesizerk   rl   z - defaultr$   r)   r(   zhttps://z.bandcamp.comr%   r3   album_release_dater&   r5   r6   album_titlealbum_artistformatsr8   tag)cls.)"_match_valid_urlrU   _download_webpager`   _og_search_thumbnailr   dictgetr}   itemsr   splitappend_proto_relative_urlr
   r   r   rY   r   r~   listallstripr   replaceinttimerandom_download_jsonr   _html_search_metar   r   r   )#rZ   r,   r    r"   r[   rT   r$   r'   r&   r5   r!   r   
track_infofile_rj   
format_urlr   abr_strro   rp   rq   r   r%   download_linkdownload_webpageru   inforx   rz   download_formats_listfr{   stat_urlstatr   r^   r^   r_   _real_extract   s  















	

zBandcampIE._real_extractN)rT   T)	__name__
__module____qualname__
_VALID_URL_EMBED_REGEX_TESTS_WEBPAGE_TESTSr`   r   r^   r^   r^   r_   r      st   	
	
	
a	

r   c                	       s~  e Zd ZdZdZddi dddd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d d!dgd"d#d$did%d&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gd!dgddid%gd d-dd.d/d0d+id1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@d9dAdBdCdDdEdFd+dGgZe fdHdIZdJdK Z  Z	S )LBandcampAlbumIEzBandcamp:albumzHhttps?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com/album/(?P<id>[^/?#&]+)z9http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1 39bc1eded3476e927c724321ddf116cfr   
1353101989r   r   r    zBlazo - Intror$   r   r%   i/Nr#   20110727r"   Blazor+   r(   zhttps://blazo.bandcamp.comr4   r3   g   @ՋAr&   Intror)   blazor5   r   r6   zJazz Format Mixtape vol.1r*   r!   g(\U3@r'   )r-   r.    1a2c32e2691474643e912cc6cd4bffaa38097443z.Blazo - Kero One - Keep It Alive (Blazo remix)i/Nz&Kero One - Keep It Alive (Blazo remix)r   gmf@zjazz-format-mixtape-vol-1z$md5:38052a93217f3ffdc033cd5dbbce2989)r    r   r)   r   playlistendz!Bandcamp imposes download limits.)r,   playlistr.   paramsr/   zChttp://nightbringer.bandcamp.com/album/hierophany-of-the-open-gravezHierophany of the Open Gravenightbringerzhierophany-of-the-open-grave)r    r)   r   	   r,   r.   playlist_mincountz0https://jstrecords.bandcamp.com/album/entropy-epz"Entropy" EP
jstrecordsz
entropy-epz$md5:0ff22959c943622972596062f2f366a5)r    r)   r   r   rK   z6https://insulters.bandcamp.com/album/we-are-the-plaguezwe-are-the-plaguezWE ARE THE PLAGUE	insultersz$md5:b3cf845ee41b2b1141dc7bde9237255f)r   r    r)   r   r,   r.   playlist_countc                    s$   t |s
t|rdS t |S )NF)BandcampWeeklyIEsuitabler   super)r   r,   	__class__r^   r_   r   |  s   
zBandcampAlbumIE.suitablec           
         s      \}}|p|} |} ||}|d}|s$td fdd|D }|dp4i }	d|||	d|	d|d	S )
Nrb   z#The page doesn't contain any tracksc              
      sN   g | ]#}| d r t|d t t| dp| d| dqS )r!   
title_linkr'   r   r    )r   
url_resultr   r   ie_keyr   )r   trZ   r,   r^   r_   
<listcomp>  s    z1BandcampAlbumIE._real_extract.<locals>.<listcomp>rp   r   r    about)_typer)   r   r    r   entries)r   groupsr   r`   r   r   )
rZ   r,   r)   album_idplaylist_idr[   rT   r   r   rp   r^   r   r_   r     s$   
zBandcampAlbumIE._real_extract)
r   r   r   IE_NAMEr   r   classmethodr   r   __classcell__r^   r^   r   r_   r     s    	
	
1=
Zr   c                   @   sN   e Zd ZdZdZd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:weeklyzEhttps?://(?:www\.)?bandcamp\.com/radio/?\?(?:[^#]+&)?show=(?P<id>\d+)z#https://bandcamp.com/radio?show=224 61acc9a002bed93986b91168aa3ab433224r   zBandcamp Weekly, 2017-04-04z$md5:5d48150916e8e02d030623a48512c874z'https://f4.bcbits.com/img/9982549_0.jpgzBandcamp Weeklyi X20170404gQŶ@)
r   r   r    r   r$   series
episode_idr3   r4   r!   formatzmp3-128)r,   r-   r.   r   z,https://bandcamp.com/radio/?foo=bar&show=224T)r,   only_matchingc                    s8  |  | | jd dtd i ddidd }|d }t|thd	d
f}|p*dd\}}}| j| dd}t| j	| ddddd fddt
fpNi }	|dpX|	d}
t|dpft|	d}  t|
t|ddd|
t|	ddd d|	dp|	dt|d||||pd|pd d t|d!gd"	S )#Nz/https://bandcamp.com/api/bcradio_api/1/get_showzDownloading radio show JSONr   zContent-Typezapplication/json)dataheadersradioShowAudio	streamUrlenc rh   FrV   ru   appDatashowsc                    s   t |d  kS )NshowId)r~   )_vshow_idr^   r_   re     s    z0BandcampWeeklyIE._real_extract.<locals>.<lambda>r    datez%Y-%m-%dz, )delimimageIdz"https://f4.bcbits.com/img/%s_0.jpgr   desc
short_descr!   r   ri   )r,   rj   r   rl   rk   rm   )	r   r   r    r   r$   r   r!   r3   r   )	_match_idr   jsondumpsencoder   r   	partitionr   r`   anyr   r   r   r   r	   r   r
   )rZ   r,   
audio_data
stream_urlrj   encodingr   bitrate_strr[   metadataseries_titler3   r^   r   r_   r     sJ   
zBandcampWeeklyIE._real_extractN)r   r   r   r   r   r   r   r^   r^   r^   r_   r     s0    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.comadrianvonzieglerzDiscography of adrianvonziegler)r   r       r   zhttp://dotscale.bandcamp.comdotscalezDiscography of dotscaler   r   z&https://nightcallofficial.bandcamp.comnightcallofficialz Discography of nightcallofficial   z'https://steviasphere.bandcamp.com/music/   steviaspherezDiscography of steviasphere)r,   r   r.   z,https://coldworldofficial.bandcamp.com/music   coldworldofficialz Discography of coldworldofficialz3https://nuclearwarnowproductions.bandcamp.com/musici  nuclearwarnowproductionsz'Discography of nuclearwarnowproductionsc              	   c   sT    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   r   loadsr~   )rZ   r[   r^   r^   r_   _yield_items  s   
zBandcampUserIE._yield_itemsc                 C   s8   |  |}| ||}| j| ||d| t|dS )NzDiscography of )getter)r   r   playlist_from_matchesr  r   )rZ   r,   r"   r[   r^   r^   r_   r     s   
zBandcampUserIE._real_extractN)r   r   r   r   r   r   r  r   r^   r^   r^   r_   r	    sT    

	/	r	  )r   r   r  r   commonr   utilsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   utils.traversalr   r   r   r   r   r   r	  r^   r^   r^   r_   <module>   s    H   B