U
    g2                     @   s   d dl Z d dlZddlmZ ddl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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   )classpropertyint_or_nonetraverse_objurljoinc                   @   sb   e Zd ZdZdZddddddd	d
dddddZedd Zdi fddZdi fddZ	dd Z
dS )BrainPOPBaseIEZbrainpop z4The username and password you entered did not match.zPayment method is expired.zYour BrainPOP plan has expired.zTerms not accepted.zAccount not activated.zRThe maximum number of devices permitted are logged in with your account right now.zKYou are trying to access your account from outside of its allowed IP range.zdIndividual accounts are not included in your plan. Try again with your shared username and password.z<That username and password won't work on this BrainPOP site.z1You'll need to join a class before you can login.zXYour account is locked. Reset your password, or ask a teacher or administrator for help.)i  i  i  i  i  i  i  i  i  i  i  i  c                 C   s&   t | jdddd}| dS )Nzhttps:zhttps?:zwww\.z
(?:www\.)?z'/(?P<slug>[^/]+/[^/]+/(?P<id>[^/?#&]+)))reescape_ORIGINreplace)clsroot r   =/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/brainpop.py
_VALID_URL   s    zBrainPOPBaseIE._VALID_URLc                 C   sj   g }| j t| j| d| |d| ddd}||t| j| d| d |D ]}|| qV|S )Nz.m3u8?mp4z-hlsF)Zm3u8_idfatal?)	format_idurl)Z_extract_m3u8_formatsr   _HLS_URLappend
_VIDEO_URLupdate)selfslugr   
display_idtokenextra_fieldsformatsfr   r   r   _assemble_formats$   s       z BrainPOPBaseIE._assemble_formats%sc                 C   s|   g }i dddd}|  D ]Z\}}	tdD ]H\}
}|||  }||r,|| || |||dd|
 i|	| q,q|S )NzAudio description)Zformat_noteZsource_preference)r%   zad_%s)highlowZquality)items	enumerategetextendr$   )r   datar    r   Z
key_formatr!   r"   Zadditional_key_formatsZadditional_key_formatZadditional_key_fieldsZkey_qualityZ	key_indexZfull_key_indexr   r   r   _extract_adaptive_formats1   s$    
 z(BrainPOPBaseIE._extract_adaptive_formatsc              	   C   st   | j dd t||d d| jddddd}t|d	 }|d
krp| d| j|p^|d pld|  d S )Nz"https://api.brainpop.com/api/login)usernamepasswordzapplication/json)zContent-TypeZRefererz
Logging inzUnable to log ini  )r.   headersZnoteZerrnoteZexpected_statusstatus_codei  zUnable to login: messagezGot status code )	_download_jsonjsondumpsencoder   r   Zreport_warning_LOGIN_ERRORSr,   )r   r0   r1   Z	login_resr3   r   r   r   _perform_loginE   s"       zBrainPOPBaseIE._perform_loginN)__name__
__module____qualname__Z_NETRC_MACHINEr   r9   r   r   r$   r/   r:   r   r   r   r   r	      s(   
r	   c                	   @   sV   e Zd Zd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Zdd ZdS )
BrainPOPIEzhttps://www.brainpop.comhttps://svideos.brainpop.comhttps://hls.brainpop.comzhttps://cdn.brainpop.comzThttps://www.brainpop.com/health/conflictresolution/martinlutherkingjr/movie?ref=nullZ 3ead374233ae74c7f1b0029a01c972f0Z1f3259fa457292b4r   zMartin Luther King, Jr.Zmartinlutherkingjrz$md5:f403dbb2bf3ccc7cf4c59d9e43e3c349idexttitler   descriptionr   md5	info_dictz/https://www.brainpop.com/science/space/bigbang/Z 9a1ff0e77444dd9e437354eb669c87ecZacae52cd48c99acfzBig BangZbigbangz$md5:3e53b766b0f116f631b13f4cae185d38Requires loginr   rG   rH   skipc                 C   s  |  |dd\}}| d| d|ddd }t| jd| d|d	d
dddtdp^|d }t|dst|d}d|kr| j|dd n| j||d |d }|d }g i  }	}
|	| ||	dd|d|	dpddd t|di td
 D ].\}}|	| ||	dd|d|d d q|
 D ]F\}}| jd!|d"d d#}|r0|r0|
|g d$t| j|i q0|d% ||	d&|	d'|	|
d(S ))Nr   rB   z5https://api.brainpop.com/api/content/published/bp/en/z/movie?full=1zDownloading movie data JSONzUnable to download movie datar.   z?full=1zDownloading topic data JSONzUnable to download topic dataF)r   )r.   topic)expected_typerL   )accessZallow)rN   reasonZloggedT)Zmetadata_available)Zvideo_idZfeaturer    r
   z%s_v2languageen
   )rP   Zlanguage_preferenceZlocalization)defaultrM   iz^subtitles_(?P<lang>\w+)$zsubtitle metadata)rS   r   Ztopic_idnamesynopsis)rB   r   rD   rE   r"   	subtitles)_match_valid_urlgroupr5   r   dictZraise_login_requiredZraise_no_formatsr-   r/   r,   r*   _search_regex
setdefaultr   r   _CDN_URL)r   r   r   r   
movie_data
topic_datarO   Zmovie_featureZmovie_feature_datar"   rV   langZlocalized_featurerT   r   r   r   _real_extractp   sr    
  
    



    

zBrainPOPIE._real_extractN)	r;   r<   r=   r   r   r   r\   _TESTSr`   r   r   r   r   r>   T   s2   r>   c                   @   s   e Zd Zdd Zdd ZdS )BrainPOPLegacyBaseIEc                 C   s>   |d }|  |||}|d ||d|d|d|dS )NZmoviesZEntryIDrT   rD   rU   )rB   r   rD   	alt_titlerE   r"   )r/   r,   )r   r^   r   r    r]   r"   r   r   r   _parse_js_topic_data   s    z)BrainPOPLegacyBaseIE._parse_js_topic_datac                 C   s^   |  |dd\}}| ||}| jd|d|ddd d d	 }| d
|d}| |||S )Nr   rB   zvar\s+content\s*=\s*zcontent data;)Zend_patterncategoryunitrL   zec_token\s*:\s*[\'"]([^\'"]+)zvideo token)rW   rX   Z_download_webpageZ_search_jsonrZ   rd   )r   r   r   r   Zwebpager^   r    r   r   r   r`      s"       z"BrainPOPLegacyBaseIE._real_extractN)r;   r<   r=   rd   r`   r   r   r   r   rb      s   rb   c                   @   sJ   e Zd ZdZdZdZdZddddd	d
dddddddddddgZdS )BrainPOPJrIEzhttps://jr.brainpop.comzhttps://svideos-jr.brainpop.comzhttps://hls-jr.brainpop.comzhttps://cdn-jr.brainpop.comz7https://jr.brainpop.com/health/feelingsandsel/emotions/Z 04e0561bb21770f305a0ce6cf0d869abZ347r   ZEmotionsZemotions)rB   rC   rD   r   rF   z8https://jr.brainpop.com/science/habitats/arctichabitats/Z b0ed063bbd1910df00220ee29340f5d6Z29zArctic HabitatsZarctichabitatsrI   rJ   Nr;   r<   r=   r   r   r   r\   ra   r   r   r   r   rh      s,   
rh   c                	   @   sN   e Zd Zd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ZdS )BrainPOPELLIEzhttps://ell.brainpop.comz https://svideos-esl.brainpop.comzhttps://hls-esl.brainpop.comzhttps://cdn-esl.brainpop.comz.https://ell.brainpop.com/level1/unit1/lesson1/Z a2012700cfb774acb7ad2e8834eed0d01r   zLesson 1Zlesson1zPersonal Pronouns)rB   rC   rD   r   rc   rF   z.https://ell.brainpop.com/level3/unit6/lesson5/Z be19c8292c87b24aacfb5fda2f3f8363Z101zLesson 5Zlesson5zReview: Unit 6rI   rJ   Nri   r   r   r   r   rj      s0   rj   c                	   @   sR   e Zd ZdZd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ZdS )BrainPOPEspIEu   BrainPOP Españolzhttps://esp.brainpop.comr?   r@   zhttps://cdn.brainpop.com/mxzFhttps://esp.brainpop.com/ciencia/la_diversidad_de_la_vida/ecosistemas/Z cb3f062db2b3c5240ddfcfde7108f8c9Z3893r   ZEcosistemasZecosistemasz$md5:80fc55b07e241f8c8f2aa8d74deaf3c3rA   rF   z>https://esp.brainpop.com/espanol/la_escritura/emily_dickinson/Z 98c1b9559e0e33777209c425cda7dac4Z7146zEmily DickinsonZemily_dickinsonz$md5:2795ad87b1d239c9711c1e92ab5a978brI   rJ   N	r;   r<   r=   ZIE_DESCr   r   r   r\   ra   r   r   r   r   rl      s2   rl   c                	   @   sR   e Zd ZdZd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ZdS )BrainPOPFrIEu   BrainPOP Françaiszhttps://fr.brainpop.comr?   r@   zhttps://cdn.brainpop.com/frzBhttps://fr.brainpop.com/sciencesdelaterre/energie/sourcesdenergie/Z 97e7f48af8af93f8a2be11709f239371Z1651r   u   Sources d'énergieZsourcesdenergiez$md5:7eece350f019a21ef9f64d4088b2d857rA   rF   z0https://fr.brainpop.com/francais/ecrire/plagiat/Z 0cf2b4f89804d0dd4a360a51310d445aZ5803ZPlagiatZplagiatz$md5:4496d87127ace28e8b1eda116e77cd2brI   rJ   Nrm   r   r   r   r   rn     s2   rn   c                   @   s:   e Zd ZdZdZdZdZdZdddd	d
ddddgZdS )BrainPOPIlIEzBrainPOP Hebrewzhttps://il.brainpop.comr?   r@   zhttps://cdn.brainpop.com/hezAhttps://il.brainpop.com/category_9/subcategory_150/subjects_3782/Z 9e4ea9dc60ecd385a6e5ca12ccf31641Z3782r   z$md5:e993632fcda0545d9205602ec314ad67Zsubjects_3782z$md5:4cc084a8012beb01f037724423a4d4edrA   rF   Nrm   r   r   r   r   ro   .  s   ro   )r6   r   commonr   utilsr   r   r   r   r	   r>   rb   rh   rj   rl   rn   ro   r   r   r   r   <module>   s   GM