U
    $PfZ(                     @   sl   d dl m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 G dd	 d	eZG d
d deZdS )    )unicode_literalsN   )InfoExtractor   )compat_urlparse)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           '         sP  t  j|}|d}|d} ||} d|d} jd| |dd}|rg }|dg D ]}	|	d	 }
g }|	d
i  D ]v\}}|di  D ]\\}}|d}|sq|	d||f t 
dd|t|dt|d|d d d qq | |	|d t 
dd|
  ||
|	dp>|	dt|	dpT|	dt|	d|d qf|di } |||dS t|d} fd d!} j|||d"}g }|d#D ]|}|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|d1|" }# |#|"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 idz5http://trailers.apple.com/trailers/feeds/data/%s.jsonF)fatalZclipsr   versionsZsizessrcz%s-%sz_(\d+p\.mov)z_h\1widthheightr   )Z	format_idr   r!   r"   language-z[^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|  } | S )Nz(?s)<script[^<]*?>.*?</script>r%   z<img ([^<]*?)/?>z	<img \1/>c                 S   s   d|  ddd S )NziTunes.playURL(%s);r   'z&#39;)groupreplace)m r.   F/tmp/pip-unpacked-wheel-7zdooeg3/youtube_dl/extractor/appletrailers.py_clean_json   s    zDAppleTrailersIE._real_extract.<locals>.fix_html.<locals>._clean_jsonz<html>%s</html>)resub_JSON_RE)sr0   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/%s.jsonzDownloading settings jsonmetadataz_(\d*p\.mov)type)r   formatr!   r"   videoz
User-Agentz!QuickTime compatible (youtube-dl))	_typer   r'   r   r   r(   r   r   Zhttp_headersr   )rB   r   entries)r1   match
_VALID_URLr+   Z_download_webpageZ_search_regex_download_jsongetitemsappendr2   r   Z_sort_formatslowerr   r	   playlist_resultr   urljoinZ_download_xmlfindallfindattribr3   jsonloadsr,   searchintsplit
rpartition)'r6   r   Zmobjr   r   ZwebpageZfilm_idZ	film_datarC   ZclipZ
clip_titler'   versionZversion_datasizeZ	size_datar    Z	page_dataZplaylist_urlr7   docr   liZon_clickZtrailer_info_jsonZtrailer_infoZ	first_urlr   Zvideo_idr(   r   r&   r-   r   Z
trailer_idZsettings_json_urlsettingsr@   Z
format_urlr.   r5   r/   _real_extract`   s    

 





 

 


 zAppleTrailersIE._real_extractN)__name__
__module____qualname__IE_NAMErE   _TESTSr3   r[   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>%s)|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                    sN     |} d j| d  |} fdd|D } || j| d S )Nz5http://trailers.apple.com/trailers/home/feeds/%s.jsonrb   c                    s   g | ]}  d |d  qS )zhttp://trailers.apple.comlocation)Z
url_result).0er5   r.   r/   
<listcomp>  s   z8AppleTrailersSectionIE._real_extract.<locals>.<listcomp>r   )Z	_match_idrF   	_SECTIONSrK   )r6   r   sectionZsection_datarC   r.   r5   r/   r[     s    

z$AppleTrailersSectionIE._real_extractN)	r\   r]   r^   r_   ro   joinrE   r`   r[   r.   r.   r.   r/   ra      sd   %ra   )
__future__r   r1   rP   commonr   compatr   utilsr   r   r	   r
   ra   r.   r.   r.   r/   <module>   s    G