o
    i1                     @   s   d dl Z d dlZd dlZddlmZ ddlmZ ddl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G dd deZdS )    N   )InfoExtractor   )
compat_ord)ExtractorErrorint_or_noneparse_iso8601strip_or_nonetry_geturl_or_none)traverse_objc                   @   s   e Zd ZdddZdS )MixcloudBaseIENc              
   C   sB   |d }| j d|dd|||rd| dnd|f idd	d
 | S )NLookupz https://app.mixcloud.com/graphqlqueryz1{
  %s(lookup: {username: "%s"%s}) {
    %s
  }
}z	, slug: "" T)r   impersonatedata)_download_json)selfobject_typeobject_fields
display_idusernameslug
lookup_key r   M/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/mixcloud.py	_call_api   s   zMixcloudBaseIE._call_apiN)__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   @   s*  e Zd ZdZdZdi dddddd	d
ddddddddedddddddddddddgded ed!ed"eid#d$id%d&i dd'dd(dd)d
d*dd+dd,dd-dedd.dd/dd0dd1dg d2ded ed!ed#d3id%d4d5d6gZd7Ze	d8d9 Z
d:d; Zd<S )=
MixcloudIEzihttps?://(?:(?:www|beta|m)\.)?mixcloud\.com/([^/]+)/(?!stream|uploads|favorites|listens|playlists)([^/]+)mixcloudz-http://www.mixcloud.com/dholbach/cryptkeeper/iddholbach_cryptkeeperextm4atitleCryptkeeperdescriptionzvAfter quite a long silence from myself, finally another Drum'n'Bass mix with my favourite current dance floor bangers.uploaderdholbachuploader_id	thumbnailzre:https?://.*\.jpg
view_count	timestampiXNupload_date20111115uploader_urlz"https://www.mixcloud.com/dholbach/artistzPSubmorphics & Chino , Telekinesis, Porter Robinson, Enei, Breakage ft Jess Millsdurationi  tagszliquid drum and basszdrum and basscomment_countrepost_count
like_countartistsskip_downloadm3u8)url	info_dictparamszEhttp://www.mixcloud.com/gillespeterson/caribou-7-inch-vinyl-mix-chat/z,gillespeterson_caribou-7-inch-vinyl-mix-chatmp3zCaribou 7 inch Vinyl Mix & Chatz$md5:2b8aec6adce69f9d41724647c65875e8zGilles Peterson Worldwidegillespetersonzre:https?://.*i1T20150203z(https://www.mixcloud.com/gillespeterson/i  )jazzsoulzworld musicfunkz404 playback error on sitezOhttps://beta.mixcloud.com/RedLightRadio/nosedrip-15-red-light-radio-01-18-2016/T)r>   only_matching5IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUDc                 C   s    d dd t|t| D S )zDEncrypt/Decrypt XOR cipher. Both ways are possible because it's XOR.r   c                 S   s$   g | ]\}}t t|t|A qS r   )chrr   ).0chkr   r   r   
<listcomp>X   s    z2MixcloudIE._decrypt_xor_cipher.<locals>.<listcomp>)joinzip	itertoolscycle)key
ciphertextr   r   r   _decrypt_xor_cipherU   s   
zMixcloudIE._decrypt_xor_cipherc                 C   sf  |  | \}}tj|tj|}}| d| }| dd|||}|s.tddd|d}|dkr=td	dd|d
krGtddd|rOtddd|d }g }dD ]D}	||	}
|
saqW| | j	t
|
}|	dkr~|| j||ddddd qW|	dkr|| j||ddd qW|d|dddid qW|s|dr| jdd g }t|dd d!d"thfD ]'}t|d#}|sq|d$|it|d%d&thfd%d'thfd(thfd) q|||d*t|d+thfd,thfd-d.thfd/thfd0thfd1d&thfd1d'thfd1d.thfd2thfd3d4thfd5d4thfdd4thfd6d!d7d+thtttfd8d!thtttfd9S ):N_	cloudcasta  audioLength
    comments(first: 100) {
      edges {
        node {
          comment
          created
          user {
            displayName
            username
          }
        }
      }
      totalCount
    }
    description
    favorites {
      totalCount
    }
    featuringArtistList
    isExclusive
    name
    owner {
      displayName
      url
      username
    }
    picture(width: 1024, height: 1024) {
        url
    }
    plays
    publishDate
    reposts {
      totalCount
    }
    streamInfo {
      dashUrl
      hlsUrl
      url
    }
    tags {
      tag {
        name
      }
    }
    restrictedReason
    idzTrack not foundT)expectedrestrictedReason	tracklistz?Track unavailable in your country due to licensing restrictionsrepeat_playz/You have reached your play limit for this trackzTrack is restricted
streamInfo)r>   hlsUrldashUrlr\   mp4m3u8_nativehlsF)entry_protocolm3u8_idfatalr]   dash)mpd_idrc   httpnonehttp_chunk_sizei  P )	format_idr>   vcodecdownloader_optionsisExclusive)metadata_availablecommentsedges.nodecommenttextuserdisplayNamer   created)author	author_idr1   )r%   formatsrn   namer+   picturer>   publishDateaudioLengthownerplays	favorites
totalCountrepostsr7   tagfeaturingArtistList)r)   r+   r/   r1   r6   r,   r.   r4   r0   r:   r9   r8   r7   r;   )_match_valid_urlgroupsurllibparseunquoter   r   getrT   _DECRYPTION_KEYbase64	b64decodeextend_extract_m3u8_formats_extract_mpd_formatsappendraise_login_requiredr   dictr	   strr   r   r   filterall)r   r>   r   r   track_idrV   reasonstream_inforx   url_key
format_url	decryptedrn   rp   rr   r   r   r   _real_extract\   s   -/















zMixcloudIE._real_extractN)r    r!   r"   
_VALID_URLIE_NAMEintlist_TESTSr   staticmethodrT   r   r   r   r   r   r#      s    	
	
0
r#   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )MixcloudPlaylistBaseIEc                 C      |S r   r   r   rp   r   r   r   _get_cloudcast      z%MixcloudPlaylistBaseIE._get_cloudcastc                 C   r   r   r   r   r)   r   r   r   r   _get_playlist_title   r   z*MixcloudPlaylistBaseIE._get_playlist_titlec              	   C   sv  |  | \}}tj|}|sd}ntj|}| d| }| jdk}|r*dn|}d}d}g }	|r| | jd| j| j||| j	f |||rI|nd }
|

|pRi }|
dg D ]=}| |
d	pci }|
d
}|smqYt|dd t}t|dd t}|r|r| d| nd }|	| |t | qY|d }|d }d|d }|s4| |	|| |
| j ||

| jS )NuploadsrU   playlistitemsr   Tz%s
    %s
    %s(first: 100%s) {
      edges {
        node {
          %s
        }
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }ro   rp   r>   c                 S   s   | d S )Nr   r   xr   r   r   <lambda>  s    z6MixcloudPlaylistBaseIE._real_extract.<locals>.<lambda>c                 S   s   | d d S )Nr}   r   r   r   r   r   r   r     s    pageInfohasNextPagez, after: "{}"	endCursor)r   r   r   r   r   
_ROOT_TYPEr   
_TITLE_KEY_DESCRIPTION_KEY_NODE_TEMPLATEr   r   r
   r   r   
url_resultr#   ie_keyformatplaylist_resultr   )r   r>   r   r   playlist_idis_playlist_typeplaylist_typelist_filterhas_next_pageentriesr   r   edgerV   cloudcast_url	item_slugowner_usernamevideo_id	page_infor   r   r   r      sN   


!
z$MixcloudPlaylistBaseIE._real_extractN)r    r!   r"   r   r   r   r   r   r   r   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dd	ddddddddddddddd	gZdZdZdZdZ	dd  Z
d!S )"MixcloudUserIEz]https?://(?:www\.)?mixcloud\.com/(?P<id>[^/]+)/(?P<type>uploads|favorites|listens|stream)?/?$zmixcloud:userz!http://www.mixcloud.com/dholbach/dholbach_uploadszdholbach (uploads)z$md5:a3f468a60ac8c3e1f8616380fc469b2b)r%   r)   r+   $   r>   r?   playlist_mincountz)http://www.mixcloud.com/dholbach/uploads/z+http://www.mixcloud.com/dholbach/favorites/dholbach_favoriteszdholbach (favorites)i  z)http://www.mixcloud.com/dholbach/listens/dholbach_listenszDaniel Holbach (listens)z$md5:b60d776f0bab534c5dabe0a34e47a789iW  z
Large list)r>   r?   r   skipz)https://www.mixcloud.com/FirstEar/stream/FirstEar_streamzFirst Ear (stream)zwe maraud for earsi  rt   biogrs   z/slug
          url
          owner { username }c                 C   s   | d| dS )Nz ()r   r   r   r   r   r   W  s   z"MixcloudUserIE._get_playlist_titleN)r    r!   r"   r   r   r   r   r   r   r   r   r   r   r   r   r     sZ    		1r   c                   @   s@   e Zd ZdZdZddddddgZd	Zd
ZdZdZ	dd Z
dS )MixcloudPlaylistIEzQhttps?://(?:www\.)?mixcloud\.com/(?P<user>[^/]+)/playlists/(?P<playlist>[^/]+)/?$zmixcloud:playlistzBhttps://www.mixcloud.com/maxvibes/playlists/jazzcat-on-ness-radio/zmaxvibes_jazzcat-on-ness-radiozNess Radio sessions)r%   r)   :   r   ry   r+   r   zWcloudcast {
            slug
            url
            owner { username }
          }c                 C   s   | dpi S )NrV   )r   r   r   r   r   r   p  s   z!MixcloudPlaylistIE._get_cloudcastN)r    r!   r"   r   r   r   r   r   r   r   r   r   r   r   r   r   [  s    r   )r   rP   urllib.parser   commonr   compatr   utilsr   r   r   r	   r
   r   utils.traversalr   r   r#   r   r   r   r   r   r   r   <module>   s      @=@