U
    ge1                     @   sh   d dl mZ ddl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dS )   )InfoExtractor   )int_or_nonejoin_nonemptymerge_dictsparse_counturl_or_none)traverse_objc                   @   s*   e Zd ZdZdgZd	ddZd
ddZdS )	NFBBaseIEz(https?://(?:www\.)?(?P<site>nfb|onf)\.caCAFc                 C   s   | j d|d||dpi S )NzepisodesData\s*:zepisode datafatal)_search_json)selfwebpagevideo_idr    r   8/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/nfb.py_extract_ep_data   s        zNFBBaseIE._extract_ep_dataNc                    s   t | fdddthfdthfddthfddthfddthfdd	thfdd	thfdd
thfdfdd}| tdd|ddt| jd|p dd ddS )Nc                    s    |d kS N	embed_urlr   _vr   r   r   <lambda>       z,NFBBaseIE._extract_ep_info.<locals>.<lambda>descriptionZthumbnail_urlZ
data_layerZepisodeMakerZepisodeYearZepisodeTitleZseasonTitleZseriesTitle)r   	thumbnailuploaderrelease_yearepisodeseasonseason_numberseriesF)get_allr$   r!   z - )	from_dictdelimz![/-]e(?:pisode)?-?(\d+)(?:[/-]|$)zepisode numberdefault)idtitleepisode_number)r	   strr   r   r   r   Z_search_regex)r   datar   sluginfor   r   r   _extract_ep_info   s,    





	   zNFBBaseIE._extract_ep_info)F)N)__name__
__module____qualname___VALID_URL_BASEZ_GEO_COUNTRIESr   r1   r   r   r   r   r
      s   
r
   c                   @   s  e Zd ZdZdZej dej dgZddddd	d
dddddddidddddddd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idd(d)d*dd+d,d-d!d.d#d/d%d&dd'ddidd0d1d2dd3d4d5d6d.d#d7d#d8dd'ddidd9d:d;dd<d=d>d6d"d#d?d#d8dd'ddidd@dAd;dd<d=d>d6d"d#d?d#d8dd'ddiddBdCd2dd3d4d5d6d.d#d7d#d8dd'ddiddDdEdFddGdHdIdJdKdLdMdNdOdd'ddiddPdQdRddSdTdUdVdWddXddidg
ZdYdZ Z	d[S )\NFBIEZnfbz$nfb.ca and onf.ca films and episodesz /(?P<type>film)/(?P<id>[^/?#&]+)z?/(?P<type>series?)/(?P<id>[^/?#&]+/s(?:ea|ai)son\d+/episode\d+)zNFB filmz%https://www.nfb.ca/film/trafficopter/Ztrafficoptermp4ZTrafficopterz$md5:060228455eb85cf88785c41656776bc0zre:^https?://.*\.jpg$zBarrie Howellsi  g     @)r*   extr+   r   r   r   r    durationZskip_downloadZm3u8)Znoteurl	info_dictparamszONF filmz&https://www.onf.ca/film/mal-du-siecle/zmal-du-siecleu   Le mal du sièclez$md5:1abf774d77569ebe603419f2d344102bzCatherine Lepagei  g     r@zNFB episode with English titlez\https://www.nfb.ca/series/true-north-inside-the-rise-of-toronto-basketball/season1/episode9/z/true-north-episode9-true-north-finale-making-itzETrue North: Inside the Rise of Toronto Basketball - Finale: Making Itz_We catch up with each player in the midst of their journey as they reflect on their road ahead.z1True North: Inside the Rise of Toronto Basketballi  zSeason 1r   zFinale: Making It	   zRyan Sidhoo)r*   r8   r+   r   r$   r    r"   r#   r!   r,   r   r   zONF episode with French titlez[https://www.onf.ca/serie/direction-nord-la-montee-du-basketball-a-toronto/saison1/episode9/zdirection-nord-episode-9uJ   Direction nord – La montée du basketball à Toronto - Finale : Réussirz$md5:349a57419b71432b97bf6083d92b029du6   Direction nord – La montée du basketball à TorontozSaison 1u   Finale : Réussirz0NFB episode with French title (needs geo-bypass)z:https://www.nfb.ca/series/etoile-du-nord/saison1/episode1/z%etoile-du-nord-episode-1-lobservationu   Étoile du Nord - L'observationz$md5:161a4617260dee3de70f509b2c9dd21bu   Étoile du Nordi  zL'observationu   Patrick Bosséz1ONF episode with English title (needs geo-bypass)z5https://www.onf.ca/serie/north-star/season1/episode1/z north-star-episode-1-observationzNorth Star - Observationz$md5:c727f370839d8a817392b9e3f23655c7z
North StarZObservationz@NFB episode with /film/ URL and English title (needs geo-bypass)z9https://www.nfb.ca/film/north-star-episode-1-observation/z?ONF episode with /film/ URL and French title (needs geo-bypass)z>https://www.onf.ca/film/etoile-du-nord-episode-1-lobservation/z<Season 2 episode w/o episode num in id, extract from json ldzChttps://www.onf.ca/film/liste-des-choses-qui-existent-saison-2-oursz+liste-des-choses-qui-existent-saison-2-oursz4La liste des choses qui existent - L'ours en peluchez$md5:d5e8d8fc5f3a7385a9cf0f509b37e28az La liste des choses qui existenti  zSaison 2r   zL'ours en peluche   zFrancis PapillonzNFB film /embed/player/ pagez/https://www.nfb.ca/film/afterlife/embed/player/Z	afterlifeZ	Afterlifez$md5:84951394f594f1fb1e62d9c43242fdf5i  g     @z@z
Ishu Patel)r*   r8   r+   r   r    r9   r   r   c                 C   s~  |  |ddd\}}}| d| d| d| d|\}}|  |jdd\}}| d|d|}| |d	 d
 }| j|d |ddd\}	}
t|d }r| j||ddddd\}}|D ]}d|d< q|		| | j
||
d |dkrJ|| jd|dd d| jd|dd dt|d| jd|dd dt| jd |d!d dd"n| | ||||}t|	|
d#|| j||i dS )$Nsitetyper*   https://www..ca//z$window\.PLAYER_OPTIONS\[[^\]]+\]\s*=zplayer dataoverlayr:   sourcer7   Zhls)m3u8_idZdvSourceZdvF)rF   Z
preferencer   zdescribed videoZformat_note)targetZfilmz0["\']nfb_version_title["\']\s*:\s*["\']([^"\']+)r+   r(   z;<[^>]+\bid=["\']tabSynopsis["\'][^>]*>\s*<p[^>]*>\s*([^<]+)r   Zposterz0<[^>]+\bitemprop=["\']director["\'][^>]*>([^<]+)r   z/["\']nfb_version_year["\']\s*:\s*["\']([^"\']+)r    )r*   r+   r   r   r   r    )formats	subtitles)_match_valid_urlgroupZ_download_webpage_handler:   r   Z	_match_idZ#_extract_m3u8_formats_and_subtitlesr   getextendZ_merge_subtitlesZ_html_search_regexr   r1   r   r   Z_search_json_ld)r   r:   r?   type_r/   r   ZurlhZplayer_datar   rI   rJ   Z	dv_sourceZfmtssubsfmtr0   r   r   r   _real_extract   s    $      
     


          zNFBIE._real_extractN)
r2   r3   r4   IE_NAMEIE_DESCr
   r5   
_VALID_URL_TESTSrR   r   r   r   r   r6   )   sN  




  +r6   c                   @   sP   e Zd ZdZdZej dZddddidd	d
ddidgZdd Z	dd Z
dS )NFBSeriesIEz
nfb:seriesznfb.ca and onf.ca seriesz//(?P<type>series?)/(?P<id>[^/?#&]+)/?(?:[?#]|$)zKhttps://www.nfb.ca/series/true-north-inside-the-rise-of-toronto-basketball/r=   r*   z0true-north-inside-the-rise-of-toronto-basketball)r:   Zplaylist_mincountr;   z@https://www.onf.ca/serie/la-liste-des-choses-qui-existent-serie/   z&la-liste-des-choses-qui-existent-seriec              	   c   sL   t |dd D ]8}t|d }| j|d tf| |g|dV  qd S )Nc                 S   s   t |d S r   )r6   Zsuitabler   r   r   r   r     r   z&NFBSeriesIE._entries.<locals>.<lambda>r       r*   )r	   r6   rK   Z
url_resultr1   rL   )r   episodesr!   Zmobjr   r   r   _entries  s     zNFBSeriesIE._entriesc                 C   sr   |  |ddd\}}}|dkr&dnd}| d| d| d	| d	| d
	|}| j||dd}| | ||S )Nr?   r@   r*   ZserieZsaisonr"   rA   rB   rC   z
1/episode1Tr   )rK   rL   Z_download_webpager   Zplaylist_resultr[   )r   r:   r?   rO   Z	series_idZseason_pathr   rZ   r   r   r   rR     s     zNFBSeriesIE._real_extractN)r2   r3   r4   rS   rT   r
   r5   rU   rV   r[   rR   r   r   r   r   rW     s$     rW   N)commonr   utilsr   r   r   r   r   Zutils.traversalr	   r
   r6   rW   r   r   r   r   <module>   s    ^