U
    Mf`H                     @   s   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 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float_or_noneint_or_noneparse_filesizestr_or_nonetry_getunified_strdateunified_timestampupdate_url_queryurl_or_noneurljoinc                   @   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attrr/    r4   =/tmp/pip-unpacked-wheel-zk9bgjdq/yt_dlp/extractor/bandcamp.py_extract_data_attrw   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   r4   xr4   r4   r5   <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 artistr.   Zpublish_dateZalbum_publish_dateZfreeDownloadPagezDownloading free downloads pageblobc                 S   s   | d d S )NZdigital_itemsr   r4   r8   r4   r4   r5   r:      r;   c                 S   s   | d d S )NZdownload_itemsr   r4   r8   r4   r4   r5   r:      r;   	downloadsZ	thumb_urldownload_formatsnameZfile_extensionc                 s   s   | ]}t |tV  qd S N)
isinstancestr).0r9   r4   r4   r5   	<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)sr4   r4   r5   r:      r;   )Ztransform_sourcer/   	retry_urldescriptionZsize_mbr   )r!   r   r?   Zformat_notefilesizer@   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_urlr-   _download_webpager6   Z_og_search_thumbnailr   dictgetrH   itemsr   splitappendZ_proto_relative_urlr   r
   r   r0   r   rI   listallstripr   replaceinttimerandomZ_download_jsonr	   Z_html_search_meta)#r1   r!   r   r   r2   r,   r    r   r   r(   r   rW   
track_infofile_r?   
format_urlr   Zabr_strrA   rB   r   r   r   Zdownload_linkZdownload_webpagerC   inforD   rE   Zdownload_formats_listfrF   Zstat_urlstatrR   r4   r4   r5   _real_extract|   s   





"  
   






   


  
zBandcampIE._real_extractN)r,   T)__name__
__module____qualname__
_VALID_URLZ_EMBED_REGEX_TESTSr6   rl   r4   r4   r4   r5   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   rS   r+   z6https://insulters.bandcamp.com/album/we-are-the-plaguezwe-are-the-plaguezWE ARE THE PLAGUEZ	insultersz$md5:b3cf845ee41b2b1141dc7bde9237255f)r   r   r   rS   r!   r#   Zplaylist_countc                    s$   t |st|rdS t |S )NF)BandcampWeeklyIEsuitabler   super)clsr!   	__class__r4   r5   rz   B  s    
zBandcampAlbumIE.suitablec           
         s      \}}|p|} |} ||}|d}|sHtd fdd|D }|dphi }	d|||	d|	d|d	S )
Nr7   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
   )rJ   tr1   r!   r4   r5   
<listcomp>Q  s   
  z1BandcampAlbumIE._real_extract.<locals>.<listcomp>rB   rs   r   Zabout)_typer   r   r   rS   entries)rX   groupsrY   r6   r[   r   )
r1   r!   r   Zalbum_idZplaylist_idr2   r,   rf   r   rB   r4   r   r5   rl   H  s$    
zBandcampAlbumIE._real_extract)
rm   rn   ro   IE_NAMErp   rq   classmethodrz   rl   __classcell__r4   r4   r}   r5   rr      st    $
Arr   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 )ry   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   rS   r   r'   seriesepisode
episode_idformatzopus-lo)r!   r"   r#   rt   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 )NrC   Zbcw_dataZaudio_streamr>   )r?   r!   r   r@   Zaudio_titler   subtitlerU   descZ
short_descZaudio_durationFZpublished_date)
r   r   rS   r   Zis_liver'   r   r   r   rW   )
	_match_idrY   r6   r\   r   r   r^   r[   r   r   )r1   r!   Zshow_idr2   rC   showrW   r?   rh   Z	known_extr   r   r   r4   r4   r5   rl   }  sD    


zBandcampWeeklyIE._real_extractNrm   rn   ro   r   rp   rq   rl   r4   r4   r4   r5   ry   d  s.    ry   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!S )"BandcampUserIEzBandcamp:userzEhttps?://(?!www\.)(?P<id>[^.]+)\.bandcamp\.com(?:/music)?/?(?:[#?]|$)z%https://adrianvonziegler.bandcamp.comZadrianvonzieglerzDiscography of adrianvonziegler)r   r      rv   zhttp://dotscale.bandcamp.comZdotscalezDiscography of dotscaler   rx   z&https://nightcallofficial.bandcamp.comZnightcallofficialz Discography of nightcallofficial   z'https://steviasphere.bandcamp.com/music/   ZsteviaspherezDiscography of steviasphere)r!   rw   r#   z,https://coldworldofficial.bandcamp.com/music
   Zcoldworldofficialz Discography of coldworldofficialz3https://nuclearwarnowproductions.bandcamp.com/musici  Znuclearwarnowproductionsz'Discography of nuclearwarnowproductionsc                    sN   |   }|  |}td|p,td|}| j||d|  fdddS )NzH<li data-item-id=["\'][^>]+>\s*<a href=["\'](?![^"\'/]*?/merch)([^"\']+)z-<div[^>]+trackTitle["\'][^"\']+["\']([^"\']+)zDiscography of c                    s
   t  | S rG   )r   r8   r!   r4   r5   r:     r;   z.BandcampUserIE._real_extract.<locals>.<lambda>)getter)r   rY   refindallZplaylist_from_matches)r1   r!   r   r2   Zdiscography_datar4   r   r5   rl     s    

   
zBandcampUserIE._real_extractNr   r4   r4   r4   r5   r     sP   

	/r   )re   r   rd   commonr   utilsr   r   r   r   r	   r
   r   r   r   r   r   r   r   rr   ry   r   r4   r4   r4   r5   <module>   s   8 hgE