5 from .common
import InfoExtractor
13 class AppleTrailersIE(InfoExtractor
):
14 IE_NAME
= 'appletrailers'
15 _VALID_URL
= r
'https?://(?:www\.|movie)?trailers\.apple\.com/(?:trailers|ca)/(?P<company>[^/]+)/(?P<movie>[^/]+)'
17 'url': 'http://trailers.apple.com/trailers/wb/manofsteel/',
20 'title': 'Man of Steel',
24 'md5': 'd97a8e575432dbcb81b7c3acb741f8a8',
26 'id': 'manofsteel-trailer4',
30 'upload_date': '20130523',
35 'md5': 'b8017b7131b721fb4e8d6f49e1df908c',
37 'id': 'manofsteel-trailer3',
41 'upload_date': '20130417',
46 'md5': 'd0f1e1150989b9924679b441f3404d48',
48 'id': 'manofsteel-trailer',
52 'upload_date': '20121212',
57 'md5': '5fe08795b943eb2e757fa95cb6def1cb',
59 'id': 'manofsteel-teaser',
63 'upload_date': '20120721',
69 'url': 'http://trailers.apple.com/trailers/magnolia/blackthorn/',
72 'title': 'Blackthorn',
74 'playlist_mincount': 2,
75 'expected_warnings': ['Unable to download JSON metadata'],
77 # json data only available from http://trailers.apple.com/trailers/feeds/data/15881.json
78 'url': 'http://trailers.apple.com/trailers/fox/kungfupanda3/',
81 'title': 'Kung Fu Panda 3',
83 'playlist_mincount': 4,
85 'url': 'http://trailers.apple.com/ca/metropole/autrui/',
86 'only_matching': True,
88 'url': 'http://movietrailers.apple.com/trailers/focus_features/kuboandthetwostrings/',
89 'only_matching': True,
92 _JSON_RE
= r
'iTunes.playURL\((.*?)\);'
94 def _real_extract(self
, url
):
95 mobj
= self
._match
_valid
_url
(url
)
96 movie
= mobj
.group('movie')
97 uploader_id
= mobj
.group('company')
99 webpage
= self
._download
_webpage
(url
, movie
)
100 film_id
= self
._search
_regex
(r
"FilmId\s*=\s*'(\d+)'", webpage
, 'film id')
101 film_data
= self
._download
_json
(
102 f
'http://trailers.apple.com/trailers/feeds/data/{film_id}.json',
103 film_id
, fatal
=False)
107 for clip
in film_data
.get('clips', []):
108 clip_title
= clip
['title']
111 for version
, version_data
in clip
.get('versions', {}).items():
112 for size
, size_data
in version_data
.get('sizes', {}).items():
113 src
= size_data
.get('src')
117 'format_id': f
'{version}-{size}',
118 'url': re
.sub(r
'_(\d+p\.mov)', r
'_h\1', src
),
119 'width': int_or_none(size_data
.get('width')),
120 'height': int_or_none(size_data
.get('height')),
121 'language': version
[:2],
125 'id': movie
+ '-' + re
.sub(r
'[^a-zA-Z0-9]', '', clip_title
).lower(),
128 'thumbnail': clip
.get('screen') or clip
.get('thumb'),
129 'duration': parse_duration(clip
.get('runtime') or clip
.get('faded')),
130 'upload_date': unified_strdate(clip
.get('posted')),
131 'uploader_id': uploader_id
,
134 page_data
= film_data
.get('page', {})
135 return self
.playlist_result(entries
, film_id
, page_data
.get('movie_title'))
137 playlist_url
= urllib
.parse
.urljoin(url
, 'includes/playlists/itunes.inc')
140 s
= re
.sub(r
'(?s)<script[^<]*?>.*?</script>', '', s
)
141 s
= re
.sub(r
'<img ([^<]*?)/?>', r
'<img \1/>', s
)
142 # The ' in the onClick attributes are not escaped, it couldn't be parsed
143 # like: http://trailers.apple.com/trailers/wb/gravity/
146 return 'iTunes.playURL({});'.format(m
.group(1).replace('\'', '''))
147 s
= re
.sub(self
._JSON
_RE
, _clean_json
, s
)
148 return f
'<html>{s}</html>'
149 doc
= self
._download
_xml
(playlist_url
, movie
, transform_source
=fix_html
)
152 for li
in doc
.findall('./div/ul/li'):
153 on_click
= li
.find('.//a').attrib
['onClick']
154 trailer_info_json
= self
._search
_regex
(self
._JSON
_RE
,
155 on_click
, 'trailer info')
156 trailer_info
= json
.loads(trailer_info_json
)
157 first_url
= trailer_info
.get('url')
160 title
= trailer_info
['title']
161 video_id
= movie
+ '-' + re
.sub(r
'[^a-zA-Z0-9]', '', title
).lower()
162 thumbnail
= li
.find('.//img').attrib
['src']
163 upload_date
= trailer_info
['posted'].replace('-', '')
165 runtime
= trailer_info
['runtime']
166 m
= re
.search(r
'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime
)
169 duration
= 60 * int(m
.group('minutes')) + int(m
.group('seconds'))
171 trailer_id
= first_url
.split('/')[-1].rpartition('_')[0].lower()
172 settings_json_url
= urllib
.parse
.urljoin(url
, f
'includes/settings/{trailer_id}.json')
173 settings
= self
._download
_json
(settings_json_url
, trailer_id
, 'Downloading settings json')
176 for fmt
in settings
['metadata']['sizes']:
177 # The src is a file pointing to the real video file
178 format_url
= re
.sub(r
'_(\d*p\.mov)', r
'_h\1', fmt
['src'])
181 'format': fmt
['type'],
182 'width': int_or_none(fmt
['width']),
183 'height': int_or_none(fmt
['height']),
191 'duration': duration
,
192 'thumbnail': thumbnail
,
193 'upload_date': upload_date
,
194 'uploader_id': uploader_id
,
196 'User-Agent': 'QuickTime compatible (yt-dlp)',
207 class AppleTrailersSectionIE(InfoExtractor
):
208 IE_NAME
= 'appletrailers:section'
211 'feed_path': 'just_added',
212 'title': 'Just Added',
215 'feed_path': 'exclusive',
216 'title': 'Exclusive',
219 'feed_path': 'just_hd',
223 'feed_path': 'most_pop',
224 'title': 'Most Popular',
227 'feed_path': 'studios',
228 'title': 'Movie Studios',
231 _VALID_URL
= r
'https?://(?:www\.)?trailers\.apple\.com/#section=(?P<id>{})'.format('|'.join(_SECTIONS
))
233 'url': 'http://trailers.apple.com/#section=justadded',
235 'title': 'Just Added',
238 'playlist_mincount': 80,
240 'url': 'http://trailers.apple.com/#section=exclusive',
242 'title': 'Exclusive',
245 'playlist_mincount': 80,
247 'url': 'http://trailers.apple.com/#section=justhd',
252 'playlist_mincount': 80,
254 'url': 'http://trailers.apple.com/#section=mostpopular',
256 'title': 'Most Popular',
259 'playlist_mincount': 30,
261 'url': 'http://trailers.apple.com/#section=moviestudios',
263 'title': 'Movie Studios',
264 'id': 'moviestudios',
266 'playlist_mincount': 80,
269 def _real_extract(self
, url
):
270 section
= self
._match
_id
(url
)
271 section_data
= self
._download
_json
(
272 'http://trailers.apple.com/trailers/home/feeds/{}.json'.format(self
._SECTIONS
[section
]['feed_path']),
275 self
.url_result('http://trailers.apple.com' + e
['location'])
276 for e
in section_data
]
277 return self
.playlist_result(entries
, section
, self
._SECTIONS
[section
]['title'])