U
    g'                     @   s\   d dl Z d dlZd dlZddlmZ ddlmZmZm	Z	 G dd deZ
G dd	 d	eZdS )
    N   )InfoExtractor   )int_or_noneparse_durationunified_strdatec                   @   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ddddgdd d!d"dd#d$gd%d&d'd(dd)d*d+d,d-d.d,d-gZd/Zd0d1 Zd2S )3AppleTrailersIEZappletrailerszahttps?://(?:www\.|movie)?trailers\.apple\.com/(?:trailers|ca)/(?P<company>[^/]+)/(?P<movie>[^/]+)z1http://trailers.apple.com/trailers/wb/manofsteel/Z5111zMan of Steel)idtitleZ d97a8e575432dbcb81b7c3acb741f8a8zmanofsteel-trailer4Zmovo   z	Trailer 4Z20130523wb)r	   extdurationr
   upload_dateuploader_id)md5	info_dictZ b8017b7131b721fb4e8d6f49e1df908czmanofsteel-trailer3   z	Trailer 3Z20130417Z d0f1e1150989b9924679b441f3404d48zmanofsteel-trailer   ZTrailerZ20121212Z 5fe08795b943eb2e757fa95cb6def1cbzmanofsteel-teaser]   ZTeaserZ20120721)urlr   playlistz7http://trailers.apple.com/trailers/magnolia/blackthorn/Z4489Z
Blackthornr   z Unable to download JSON metadata)r   r   playlist_mincountZexpected_warningsz4http://trailers.apple.com/trailers/fox/kungfupanda3/Z15881zKung Fu Panda 3   r   r   r   z.http://trailers.apple.com/ca/metropole/autrui/T)r   Zonly_matchingzLhttp://movietrailers.apple.com/trailers/focus_features/kuboandthetwostrings/ziTunes.playURL\((.*?)\);c           '         sD    |}|d}|d} ||} d|d} jd| d|dd}|rg }|d	g D ]}	|	d
 }
g }|	di  D ]x\}}|di  D ]^\}}|d}|sq|| d| t	dd|t
|dt
|d|d d d qq||d t	dd|
  ||
|	dp4|	dt|	dpJ|	dt|	d|d qf|di } |||dS tj|d} fd d!} j|||d"}g }|d#D ]x}|d$jd% }  j|d&}t|}|d'}|s q|d
 }|d t	dd|  }|d(jd }|d dd}|d }td)|} d }!| rd*t| d+ t| d, }!|d-d. d/d0  }"tj|d1|" d}# |#|"d2}$g }|$d3 d D ]@}%t	d4d|%d }&||&|%d5 t
|%d t
|%d d6 q|d7||||!|||d8d9id:	 qd;||d<S )=NmovieZcompanyzFilmId\s*=\s*'(\d+)'zfilm idz.http://trailers.apple.com/trailers/feeds/data/z.jsonF)fatalZclipsr
   versionsZsizessrc-z_(\d+p\.mov)z_h\1widthheightr   )Z	format_idr   r    r!   languagez[^a-zA-Z0-9] ZscreenZthumbruntimeZfadedZposted)r	   formatsr
   	thumbnailr   r   r   pageZmovie_titlezincludes/playlists/itunes.incc                    s@   t dd| } t dd| } dd }t  j|| } d|  dS )	Nz(?s)<script[^<]*?>.*?</script>r#   z<img ([^<]*?)/?>z	<img \1/>c                 S   s   d | dddS )NziTunes.playURL({});r   'z&#39;)formatgroupreplace)m r-   B/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/appletrailers.py_clean_json   s    zDAppleTrailersIE._real_extract.<locals>.fix_html.<locals>._clean_jsonz<html>z</html>)resub_JSON_RE)sr/   selfr-   r.   fix_html   s
    z/AppleTrailersIE._real_extract.<locals>.fix_html)Ztransform_sourcez./div/ul/liz.//aZonClickztrailer infor   z.//imgz+(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})<   minutesseconds/_r   zincludes/settings/zDownloading settings jsonmetadataz_(\d*p\.mov)type)r   r)   r    r!   videoz
User-AgentzQuickTime compatible (yt-dlp))	_typer	   r%   r
   r   r&   r   r   Zhttp_headersr   )r@   r	   entries)Z_match_valid_urlr*   Z_download_webpageZ_search_regex_download_jsongetitemsappendr0   r1   r   lowerr   r   playlist_resulturllibparseurljoinZ_download_xmlfindallfindattribr2   jsonloadsr+   searchintsplit
rpartition)'r5   r   Zmobjr   r   ZwebpageZfilm_idZ	film_datarA   ZclipZ
clip_titler%   versionZversion_datasizeZ	size_datar   Z	page_dataZplaylist_urlr6   docr   liZon_clickZtrailer_info_jsonZtrailer_infoZ	first_urlr
   Zvideo_idr&   r   r$   r,   r   Z
trailer_idZsettings_json_urlsettingsfmtZ
format_urlr-   r4   r.   _real_extract^   s    



 




 

 

 zAppleTrailersIE._real_extractN)__name__
__module____qualname__IE_NAME
_VALID_URL_TESTSr2   rZ   r-   r-   r-   r.   r      s   5
	Lr   c                   @   s   e Zd ZdZdddddddddd	d
dddddZdde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gZdd Z	dS ) AppleTrailersSectionIEzappletrailers:sectionZ
just_addedz
Just Added)	feed_pathr
   	exclusiveZ	ExclusiveZjust_hdzJust HDZmost_popzMost PopularZstudioszMovie Studios)	justaddedrc   justhdmostpopularmoviestudiosz;https?://(?:www\.)?trailers\.apple\.com/#section=(?P<id>{})|z,http://trailers.apple.com/#section=justaddedrd   )r
   r	   P   r   z,http://trailers.apple.com/#section=exclusivez)http://trailers.apple.com/#section=justhdre   z.http://trailers.apple.com/#section=mostpopularrf      z/http://trailers.apple.com/#section=moviestudiosrg   c                    sP     |} d j| d |} fdd|D } || j| d S )Nz5http://trailers.apple.com/trailers/home/feeds/{}.jsonrb   c                    s   g | ]}  d |d  qS )zhttp://trailers.apple.comlocation)Z
url_result).0er4   r-   r.   
<listcomp>  s   z8AppleTrailersSectionIE._real_extract.<locals>.<listcomp>r
   )Z	_match_idrB   r)   	_SECTIONSrG   )r5   r   sectionZsection_datarA   r-   r4   r.   rZ     s    

z$AppleTrailersSectionIE._real_extractN)
r[   r\   r]   r^   ro   r)   joinr_   r`   rZ   r-   r-   r-   r.   ra      sd   %ra   )rN   r0   urllib.parserH   commonr   utilsr   r   r   r   ra   r-   r-   r-   r.   <module>   s    C