
    yf*                         d dl Z ddlmZ ddlmZmZ ddlmZmZm	Z	  G d de      Z
 G d	 d
e
      Z G d de      Z G d de
      Z G d de      Zy)    N   )InfoExtractor   )
compat_strcompat_urllib_parse_unquote)ExtractorErrorstr_or_nonetry_getc                   T     e Zd ZdZdZd Z fdZed        Z	 	 	 d fd	Z	d Z
 xZS )	AudiusBaseIENz/v1c                     t        |t              r:|j                  d      }||S t        |      dk(  rd|v rt	        d|d   z  d      t	        d      )Ndatar   messagezAPI error: %sTexpectedUnexpected API response)
isinstancedictgetlenr   )selfresponseresponse_datas      9/usr/lib/python3/dist-packages/yt_dlp/extractor/audius.py_get_response_datazAudiusBaseIE._get_response_data   sa    h%$LL0M($$8}!i8&;$_x	7J%J.24 4677    c                     t         t        |   dddd      }| j                  |      }t	        |t
              rt        j                  |      | _        yt        d      )z2Selecting one of the currently available API hostszhttps://api.audius.co/NzRequesting available API hostsz%Unable to request available API hostsnoteerrnotez!Unable to get available API hosts)
superr   _download_jsonr   r   listrandomchoice	_API_BASEr   )r   r   hosts	__class__s      r   _select_api_basezAudiusBaseIE._select_api_base   s_    t;$d1; < = ''1eT"#]]51DN@AAr   c                     t        |       } t        |      }d|v sd|v r4|j                  dd      j                  dd      }| j                  ||      S | S )a  
        Audius removes forward slashes from the uri, but leaves backslashes.
        The problem is that the current version of Chrome replaces backslashes
        in the address bar with a forward slashes, so if you copy the link from
        there and paste it into youtube-dl, you won't be able to download
        anything from this link, since the Audius API won't be able to resolve
        this url
        /z%2Fz%5C)r   replace)urltitlefixed_titles      r   _prepare_urlzAudiusBaseIE._prepare_url"   sX     *#.+E2%<5E>--U3;;E5IK;;uk22
r   c                    | j                   | j                          	 t        t        |   | j                   | j
                  |||||      }| j                  |      S # t        $ r!}dt        |      v rt        dd      |d }~ww xY w)N)r   r    expected_statuszFailed to parse JSONz1An error occurred while receiving data. Try againTr   )	r&   r)   r!   r   r"   _API_Vr   r   r   )	r   pathitem_idr   r    r2   r   excr(   s	           r   _api_requestzAudiusBaseIE._api_request3   s     >>!!!#		\4? NNDKK>d @ BH &&x00  	%C8$%X.24 4I	s   2A" "	B+BBc                 0    | j                  d|z  |d      S )Nz/resolve?url=%si  )r2   )r7   )r   r-   r5   s      r   _resolve_urlzAudiusBaseIE._resolve_urlD   s'      !2S!8'14 ! 6 	6r   )NzDownloading JSON metadataz Unable to download JSON metadataN)__name__
__module____qualname__r&   r3   r   r)   staticmethodr0   r7   r9   __classcell__)r(   s   @r   r   r      sB    IF8
B    5P?%)1"6r   r   c                   p    e Zd ZdZdZddddddd	dd
ddeeeddddddddddddeeeddgZddddZd Zy)AudiusIEz_(?x)https?://(?:www\.)?(?:audius\.co/(?P<uploader>[\w\d-]+)(?!/album|/playlist)/(?P<title>\S+))z	Audius.coz]https://audius.co/test_acc/t%D0%B5%D0%B5%D0%B5est-1.%5E_%7B%7D/%22%3C%3E.%E2%84%96~%60-198631 92c35d3e754d5a0f17eef396b0d33582xd8gYu/   Tеееest/ 1.!@#$%^&*()_+=[]{};'\":<>,.?/№~`mp3Description   test
Electroniczre:https?://.*\.jpg)idr.   extdescriptiondurationtrackartistgenre	thumbnail
view_count
like_countrepost_count)r-   md5	info_dictz%https://audius.co/voltra/radar-103692 491898a0a8de39f20c5d6a8a80ab5132KKdy2RADARi>  voltraTrance)rH   r.   rI   rK   rL   rM   rN   rO   rP   rQ   rR      i  i  )150x150480x480	1000x1000c                    | j                  |      }t        |d       }|6|j                  d      }| j                  ||      }| j	                  ||      }nd }| j                  d|z  |      }t        |t              st        d      |j                  d      }|t        d      |j                  d      }g }t        |t              rO|j                         D ]<  \  }}	d|	i}
| j                  j                  |      }|||
d	<   |j                  |
       > ||j                  d|      | j                  d
|dd|j                  d      |j                  d      |j                  d      t        |d t              |j                  d      ||j                  d      |j                  d      |j                  d      dS )Nc                 $    | j                  d      S )Ntrack_id)groupxs    r   <lambda>z(AudiusIE._real_extract.<locals>.<lambda>|   s    177:+> r   r.   z
/tracks/%sr   rH   zUnable to get ID of the trackartworkr-   
preferencez/v1/tracks/z/streamrC   rJ   rK   c                     | d   d   S )Nusername rb   s    r   rd   z(AudiusIE._real_extract.<locals>.<lambda>   s    AfIf4E r   rN   
play_countfavorite_countrR   )rH   r.   r-   rI   rJ   rK   rL   rM   rN   
thumbnailsrP   rQ   rR   )_match_valid_urlr
   ra   r0   r9   r7   r   r   r   r   items_ARTWORK_MAPappendr&   r   )r   r-   mobjr`   r.   
track_dataartworks_datarm   quality_keythumbnail_urlrO   quality_codes               r   _real_extractzAudiusIE._real_extractz   s   $$S)4!>?JJw'E##C/C**36JE**<(+BHMJ*d+ !:;;>>$' !@AA"y1
mT*.;.A.A.C -*]=	  $0044[A+.:Il+!!),- ^^GU3/3~~xH%>>-8"z2^^G,j*EzR^^G,$$..6$..)9:&NN>:
 	
r   N)	r:   r;   r<   
_VALID_URLIE_DESCint_TESTSrp   rx   rj   r   r   r@   r@   I   s    wJG s5P,P %3!! #		
* ;5  "!3!! #		
)&FR L.
r   r@   c                   ,    e Zd ZdZdZdZddddddgZy)	AudiusTrackIEzE(?x)(?:audius:)(?:https?://(?:www\.)?.+/v1/tracks/)?(?P<track_id>\w+)zaudius:trackz3Audius track ID or API link. Prepend with "audius:"zaudius:9RWloT)r-   only_matchingzRaudius:http://discoveryprovider.audius.prod-us-west-2.staked.cloud/v1/tracks/9RWloN)r:   r;   r<   ry   IE_NAMErz   r|   rj   r   r   r~   r~      s3    ]JGCG "!	

 h!	
	Fr   r~   c                   8    e Zd ZdZdZdZdddddd	d
Zd Zd Zy)AudiusPlaylistIEzVhttps?://(?:www\.)?audius\.co/(?P<uploader>[\w\d-]+)/(?:album|playlist)/(?P<title>\S+)zaudius:playlistzAudius.co playlistsz7https://audius.co/test_acc/playlist/test-playlist-22910DNvjNztest playlistzTest description

lol)rH   r.   rJ      r-   rT   playlist_countc                    g }|D ]|  }t        |t              st        d      t        |j	                  d            }|st        d      |j                  | j                  d|z  t        j                         |             ~ |S )Nr   rH   z$Unable to get track ID from playlistz	audius:%s)ievideo_id)	r   r   r   r	   r   rq   
url_resultr~   ie_key)r   tracksentriesrL   r`   s        r   _build_playlistz AudiusPlaylistIE._build_playlist   s     	?EeT*$%>??"599T?3H$%KLLNN4??h& '')H + > ?	? r   c                 j   | j                          | j                  |      }|j                  d      }| j                  ||      }| j	                  ||      }t        |t              rt        |      dk7  rt        d      |d   }t        |t              st        d      |j                  d      }|t        d      | j                  d|z  |dd	
      }t        |t              st        d      | j                  |      }| j                  |||j                  d|      |j                  d            S )Nr.   r   r   r   rH   zUnable to get playlist IDz/playlists/%s/tracksz$Downloading playlist tracks metadataz+Unable to download playlist tracks metadatar   playlist_namerJ   )r)   rn   ra   r0   r9   r   r#   r   r   r   r   r7   r   playlist_result)	r   r-   rr   r.   playlist_responseplaylist_dataplaylist_idplaylist_tracksr   s	            r   rx   zAudiusPlaylistIE._real_extract   s>   $$S)

7#U+ --c59+T2c:K6LPQ6Q !:;;)!,-. !:;;#''- !<==++"[0>A , C /40 !:;;&&7##G[$1$5$5ou$M$1$5$5m$DF 	Fr   N)	r:   r;   r<   ry   r   rz   _TESTr   rx   rj   r   r   r   r      s9    jJG#GH$4

 EFr   r   c                   2    e Zd ZdZdZdZdddddd	d
Zd Zy)AudiusProfileIEzaudius:artistzAudius.co profile/artist pagesz6https?://(?:www)?audius\.co/(?P<id>[^\/]+)/?(?:[?#]|$)zhttps://audius.co/pzl/ezRo7z&TAMALE

Contact: officialpzl@gmail.compzl)rH   rJ   r.      r   c                 h   | j                          | j                  |      }	 | j                  d|z   |      }|d   d   }|d   j                  d      }| j                  d|z  |      }| j                  | j                  |      |||      S # t        $ r}t        dt	        |      z         d }~ww xY w)Nz/full/users/handle/z!Could not download profile info; r   rH   bioz/full/users/handle/%s/tracks)r)   	_match_idr7   r   strr   r   r   )r   r-   
profile_id_profile_dataeprofile_audius_idprofile_bioapi_calls           r   rx   zAudiusProfileIE._real_extract  s    ^^C(
	O --.Cj.PR\]M *!,T2#A&**51$$%Cj%PR\]##D$8$8$BDUWacnoo  	O !Ds1v!MNN	Os   B 	B1B,,B1N)r:   r;   r<   r   rz   ry   r   rx   rj   r   r   r   r      s4    G.GJJ'E

 Epr   r   )r$   commonr   compatr   r   utilsr   r	   r
   r   r@   r~   r   r   rj   r   r   <module>r      sZ     ! < 8 8>6= >6B_
| _
DH  8F| 8Fvp& pr   