U
    gI                     @   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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dS )    N   )InfoExtractor   )KNOWN_EXTENSIONSExtractorErrorextract_attributesfloat_or_noneget_element_html_by_idint_or_noneparse_filesizestr_or_nonetry_getunified_strdateunified_timestampupdate_url_queryurl_or_noneurljoin)traverse_objc                   @   s   e Zd ZdZdg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d d!d"d#d$d%d&d'dd(dd)d*d+d,d-d.d/d0d'd)d)d1d2d3d4d5d6d7d8dd9dd:d;d<d=d>d?d@dAd8d:dBdCdDdEd4d5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-songZ c557841d5e50261777a6585648adf439Z
1812978515mp3uL   youtube-dl "'/\ä↭ - youtube-dl "'/\ä↭ - youtube-dl test song "'/\ä↭gOn#@u   youtube-dl "'/\ä↭Z20121129iѷPu5   youtube-dl "'/\ä↭ - youtube-dl test song "'/\ä↭zhttps://youtube-dl.bandcamp.comz
youtube-dlz+https://f4.bcbits.com/img/a3216802731_5.jpg)idexttitledurationuploaderupload_date	timestamptrackalbum_artisttrack_idartistuploader_urluploader_id	thumbnailz@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
re:[fa]laczBen Prunty - Lanius (Battle)zre:^https?://.*\.jpg$z
Ben PruntyiK=SZ20140403i<Sgy&1Np@zLanius (Battle)r   z FTL: Advanced Edition Soundtrackzhttps://benprunty.bandcamp.comZ	benprunty)r   r   acodecr   r#   r   r   r   release_timestamprelease_dater   r   track_numberr   r    r   albumr!   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relapsealumni)r   r   r   r#   r   r   r   r)   r*   r   r   r+   r   r    r   r,   r!   r"   )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
Diskotopiaz"DSK F/W 2016-2017 Free Compilationzhttps://diskotopia.bandcamp.comZ
diskotopia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attrr2    r7   =/tmp/pip-unpacked-wheel-q5ljy6pj/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||d:S );Nr   r   c                 S   s   | d d S )N	trackinfor   r7   xr7   r7   r8   <lambda>       z*BandcampIE._real_extract.<locals>.<lambda>file-r   zhttp:none)	format_idr$   r   vcodecr(   Zabrr   r   Z	track_numr   embedFcurrentr    zO<h3 class="albumTitle">[\S\s]*?by\s*<span>\s*<a href="[^>]+">\s*([^>]+?)\s*</a>zalbum artistr1   Zpublish_dateZalbum_publish_dateZfreeDownloadPagezDownloading free downloads pageblobc                 S   s   | d d S )NZdigital_itemsr   r7   r;   r7   r7   r8   r=      r>   c                 S   s   | d d S )NZdownload_itemsr   r7   r;   r7   r7   r8   r=      r>   	downloadsZ	thumb_urldownload_formatsnameZfile_extensionc                 s   s   | ]}t |tV  qd S )N)
isinstancestr).0r<   r7   r7   r8   	<genexpr>   s     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)sr7   r7   r8   r=      r>   )Ztransform_sourcer2   	retry_urldescriptionZsize_mbr   )r$   r   rB   Zformat_notefilesizerC   r(    - )defaultzhttps://z.bandcamp.comZalbum_release_dateZalbum_title)r   r   r#   r   r"   r!   r   r)   r   r   r+   r   r    r,   r   formats)_match_valid_urlr0   _download_webpager9   Z_og_search_thumbnailr   dictgetrJ   itemsr   splitappendZ_proto_relative_urlr
   r   r   r3   r   rK   listallstripr   replaceinttimerandomZ_download_jsonr   Z_html_search_meta)#r4   r$   r   r   r5   r/   r#   r   r   r+   r   rY   
track_infofile_rB   
format_urlr   Zabr_strrD   rE   r    r   r   Zdownload_linkZdownload_webpagerF   inforG   rH   Zdownload_formats_listfrI   Zstat_urlstatrT   r7   r7   r8   _real_extract   s   





"  
   






   


  
zBandcampIE._real_extractN)r/   T)__name__
__module____qualname__
_VALID_URLZ_EMBED_REGEX_TESTSr9   rn   r7   r7   r7   r8   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gdd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-gZe fd.d/Zd0d1 Z  Z	S )2BandcampAlbumIEzBandcamp:albumzHhttps?://(?:(?P<subdomain>[^.]+)\.)?bandcamp\.com/album/(?P<id>[^/?#&]+)z9http://blazo.bandcamp.com/album/jazz-format-mixtape-vol-1Z 39bc1eded3476e927c724321ddf116cfZ
1353101989r   zBlazo - Introi/NZ20110727ZBlazo)r   r   r   r   r   r   )r%   r&   Z 1a2c32e2691474643e912cc6cd4bffaaZ38097443z.Blazo - Kero One - Keep It Alive (Blazo remix)i/NzJazz Format Mixtape vol.1zjazz-format-mixtape-vol-1Zblazo)r   r   r"   Zplaylistendr   z!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   rU   r.   z6https://insulters.bandcamp.com/album/we-are-the-plaguezwe-are-the-plaguezWE ARE THE PLAGUEZ	insultersz$md5:b3cf845ee41b2b1141dc7bde9237255f)r   r   r"   rU   r$   r&   Zplaylist_countc                    s$   t |st|rdS t |S )NF)BandcampWeeklyIEsuitabler   super)clsr$   	__class__r7   r8   r|   G  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   )r]   Z
url_resultr   r   Zie_keyr   )rL   tr4   r$   r7   r8   
<listcomp>V  s   
  z1BandcampAlbumIE._real_extract.<locals>.<listcomp>rE   ru   r   Zabout)_typer"   r   r   rU   entries)rZ   groupsr[   r9   r]   r   )
r4   r$   r"   Zalbum_idZplaylist_idr5   r/   rh   r   rE   r7   r   r8   rn   M  s$    
zBandcampAlbumIE._real_extract)
ro   rp   rq   IE_NAMErr   rs   classmethodr|   rn   __classcell__r7   r7   r   r8   rt     st    $
Art   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 b00df799c733cf7e0c567ed187dea0fdZ224Zopusz(BC Weekly April 4th 2017 - Magic Momentsz$md5:5d48150916e8e02d030623a48512c874gQŶ@Z20170404Bandcamp WeeklyzMagic Moments)	r   r   r   rU   r   r*   seriesepisode
episode_idformatzopus-lo)r$   r%   r&   rv   z)https://bandcamp.com/?blah/blah@&show=228T)r$   Zonly_matchingc                 C   s   |  |}| ||}| ||d}|d | }g }|d  D ]D\}}t|sRq@tD ]}	|	|krV|	}
 qpqVd }
||||
dd q@|dpd}|d}|r|d	| 7 }|||d
p|dt|ddt	|dd|d||d
S )NrF   Zbcw_dataZaudio_streamrA   )rB   r$   r   rC   Zaudio_titler   subtitlerW   descZ
short_descZaudio_durationFZpublished_date)
r   r   rU   r   Zis_liver*   r   r   r   rY   )
	_match_idr[   r9   r^   r   r   r`   r]   r   r   )r4   r$   Zshow_idr5   rF   showrY   rB   rj   Z	known_extr   r   r   r7   r7   r8   rn     sD    


zBandcampWeeklyIE._real_extractN)ro   rp   rq   r   rr   rs   rn   r7   r7   r7   r8   r{   i  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      rx   zhttp://dotscale.bandcamp.comZdotscalezDiscography of dotscaler   rz   z&https://nightcallofficial.bandcamp.comZnightcallofficialz Discography of nightcallofficial   z'https://steviasphere.bandcamp.com/music/   ZsteviaspherezDiscography of steviasphere)r$   ry   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t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-gridzdata-client-items.page_url)
refindallr   	functoolspartialr	   r   jsonloadsrK   )r4   r5   r7   r7   r8   _yield_items  s    
     zBandcampUserIE._yield_itemsc                 C   s<   |  |}| ||}| j| ||d| tt|dS )NzDiscography of )getter)r   r[   Zplaylist_from_matchesr   r   r   r   )r4   r$   r   r5   r7   r7   r8   rn     s    
  
zBandcampUserIE._real_extractN)ro   rp   rq   r   rr   rs   r   rn   r7   r7   r7   r8   r     sR   

	/	r   )r   r   rg   r   rf   commonr   utilsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Zutils.traversalr   r   rt   r{   r   r7   r7   r7   r8   <module>   s   @ hgE