
    V|h)                         S SK r S SKrSSKJr  SSKJrJrJr   " S S\5      r	 " S S	\	5      r
 " S
 S\
5      r " S S\	5      r " S S\5      rg)    N   )InfoExtractor   )ExtractorErrorstr_or_nonetry_getc                   b   ^  \ rS rSrSrSrS rU 4S jr\S 5       r	   S
U 4S jjr
S rS	rU =r$ )AudiusBaseIE   Nz/v1c                     [        U[        5      (       aG  UR                  S5      nUb  U$ [        U5      S:X  a"  SU;   a  [	        SR                  US   5      SS9e[	        S5      e)Ndatar   messagezAPI error: {}TexpectedUnexpected API response)
isinstancedictgetlenr   format)selfresponseresponse_datas      Q/home/james-whalen/.local/lib/python3.13/site-packages/yt_dlp/extractor/audius.py_get_response_dataAudiusBaseIE._get_response_data   sj    h%%$LL0M($$8}!i8&;$_%;%;HY<O%P.24 4677    c                    > [         TU ]  SSSSS9nU R                  U5      n[        U[        5      (       a  [
        R                  " U5      U l        g[        S5      e)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)	super_download_jsonr   r   listrandomchoice	_API_BASEr   )r   r   hosts	__class__s      r   _select_api_baseAudiusBaseIE._select_api_base   s`    7)$d1; * = ''1eT""#]]51DN@AAr   c                     [         R                  R                  U 5      n [         R                  R                  U5      nSU;   d  SU;   a3  UR                  SS5      R                  SS5      nU R                  X5      $ U $ )ag  
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)urllibparseunquotereplace)urltitlefixed_titles      r   _prepare_urlAudiusBaseIE._prepare_url"   sj     ll""3'$$U+%<5E>--U3;;E5IK;;u22
r   c                   > U R                   c  U R                  5          [        TU ]  U R                    U R                   U 3X#XES9nU R                  U5      $ ! [
         a   nS[        U5      ;   a
  [        SSS9eUeS nAff = f)N)r    r!   expected_statuszFailed to parse JSONz1An error occurred while receiving data. Try againTr   )r'   r*   r"   r#   _API_Vr   strr   )	r   pathitem_idr    r!   r8   r   excr)   s	           r   _api_requestAudiusBaseIE._api_request3   s     >>!!!#		w->>"4;;-v6 . BH &&x00  	%S1$%X.24 4I	s   )A 
B$A??Bc                 *    U R                  SU 3USS9$ )Nz/resolve?url=i  )r8   )r>   )r   r2   r<   s      r   _resolve_urlAudiusBaseIE._resolve_urlD   s(      =!614 ! 6 	6r   )r'   )NzDownloading JSON metadataz Unable to download JSON metadataN)__name__
__module____qualname____firstlineno__r'   r9   r   r*   staticmethodr5   r>   rA   __static_attributes____classcell__)r)   s   @r   r
   r
      sG    IF8
B    5P?%)1"6 6r   r
   c                   x    \ rS rSrSrSrSSSSSS	S
SSSS\\\S.S.SSSSSSSSSS\\\S.S./rSSSS.rS r	Sr
g)AudiusIEI   z_(?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)idr3   extdescriptiondurationtrackartistgenre	thumbnail
view_count
like_countrepost_count)r2   md5	info_dictz%https://audius.co/voltra/radar-103692 491898a0a8de39f20c5d6a8a80ab5132KKdy2RADARi>  voltraTrance)rT   r3   rU   rW   rX   rY   rZ   r[   r\   r]   r^      i  i  )150x150480x480	1000x1000c                    U R                  U5      n[        US 5      nUc4  UR                  S5      nU R                  X5      nU R	                  X5      nOS nU R                  SU 3U5      n[        U[        5      (       d  [        S5      eUR                  S5      nUc  [        S5      eUR                  S5      n/ n[        U[        5      (       aP  UR                  5        H<  u  pSU	0n
U R                  R                  U5      nUb  XS	'   UR                  U
5        M>     UUR                  SU5      U R                   S
U S3SUR                  S5      UR                  S5      UR                  S5      [        US [        5      UR                  S5      UUR                  S5      UR                  S5      UR                  S5      S.$ )Nc                 $    U R                  S5      $ )Ntrack_id)groupxs    r   <lambda>(AudiusIE._real_extract.<locals>.<lambda>|   s    177:+>r   r3   z/tracks/r   rT   zUnable to get ID of the trackartworkr2   
preferencez/v1/tracks/z/streamrO   rV   rW   c                     U S   S   $ )Nusername rn   s    r   rp   rq      s    AfIf4Er   rZ   
play_countfavorite_countr^   )rT   r3   r2   rU   rV   rW   rX   rY   rZ   
thumbnailsr\   r]   r^   )_match_valid_urlr   rm   r5   rA   r>   r   r   r   r   items_ARTWORK_MAPappendr'   r:   )r   r2   mobjrl   r3   
track_dataartworks_datarz   quality_keythumbnail_urlr[   quality_codes               r   _real_extractAudiusIE._real_extractz   s   $$S)4!>?JJw'E##C/C**36JE**XhZ+@(KJ*d++ !:;;>>$' !@AA"y1
mT**.;.A.A.C*=	  $0044[A+.:l+!!), /D ^^GU3nn%[
'B%>>-8"z2^^G,j*EsK^^G,$$..6$..)9:&NN>:
 	
r   rw   N)rC   rD   rE   rF   
_VALID_URLIE_DESCint_TESTSr}   r   rH   rw   r   r   rK   rK   I   s    wJG s5P,P %3!! #		
* ;5  "!3!! #		
)&FR L.
r   rK   c                   4    \ rS rSrSrSrSrSSS.SSS./rS	rg
)AudiusTrackIE   zE(?x)(?:audius:)(?:https?://(?:www\.)?.+/v1/tracks/)?(?P<track_id>\w+)zaudius:trackz3Audius track ID or API link. Prepend with "audius:"zaudius:9RWloT)r2   only_matchingzRaudius:http://discoveryprovider.audius.prod-us-west-2.staked.cloud/v1/tracks/9RWlorw   N)	rC   rD   rE   rF   r   IE_NAMEr   r   rH   rw   r   r   r   r      s3    ]JGCG "!	

 h!	
	Fr   r   c                   @    \ rS rSrSrSrSrSSSSS	.S
S.rS rS r	Sr
g)AudiusPlaylistIE   zVhttps?://(?: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)rT   r3   rV      r2   r`   playlist_countc           	         / nU H  n[        U[        5      (       d  [        S5      e[        UR	                  S5      5      nU(       d  [        S5      eUR                  U R                  SU 3[        R                  5       US95        M     U$ )Nr   rT   z$Unable to get track ID from playlistzaudius:)ievideo_id)	r   r   r   r   r   r~   
url_resultr   ie_key)r   tracksentriesrX   rl   s        r   _build_playlist AudiusPlaylistIE._build_playlist   s    EeT**$%>??"599T?3H$%KLLNN4??($ '')H + > ?  r   c                    U R                  5         U R                  U5      nUR                  S5      nU R                  X5      nU R	                  X5      n[        U[        5      (       a  [        U5      S:w  a  [        S5      eUS   n[        U[        5      (       d  [        S5      eUR                  S5      nUc  [        S5      eU R                  SU S3US	S
S9n[        U[        5      (       d  [        S5      eU R                  U5      nU R                  XUR                  SU5      UR                  S5      5      $ )Nr3   r   r   r   rT   zUnable to get playlist IDz/playlists//tracksz$Downloading playlist tracks metadataz+Unable to download playlist tracks metadatar   playlist_namerV   )r*   r{   rm   r5   rA   r   r$   r   r   r   r   r>   r   playlist_result)	r   r2   r   r3   playlist_responseplaylist_dataplaylist_idplaylist_tracksr   s	            r   r   AudiusPlaylistIE._real_extract   sE   $$S)

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

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

Contact: officialpzl@gmail.compzl)rT   rV   r3      r   c                 j   U R                  5         U R                  U5      n U R                  SU-   U5      nUS   S   nUS   R                  S5      nU R                  SU S3U5      nU R                  U R                  U5      XRU5      $ ! [         a  n[        S[	        U5      -   5      eS nAff = f)Nz/full/users/handle/z!Could not download profile info; r   rT   bior   )r*   	_match_idr>   r   r:   r   r   r   )r   r2   
profile_id_profile_dataeprofile_audius_idprofile_bioapi_calls           r   r   AudiusProfileIE._real_extract  s    ^^C(
	O --.Cj.PR\]M *!,T2#A&**51$$'::,g%NPZ[##D$8$8$BDUcnoo  	O !Ds1v!MNN	Os   B 
B2B--B2rw   N)
rC   rD   rE   rF   r   r   r   r   r   rH   rw   r   r   r   r      s4    G.GJJ'E

 Epr   r   )r%   urllib.parser.   commonr   utilsr   r   r   r
   rK   r   r   r   rw   r   r   <module>r      sZ      ! 8 8>6= >6B_
| _
DH  8F| 8Fvp& pr   