]>
Commit | Line | Data |
---|---|---|
3798eadc PH |
1 | from __future__ import unicode_literals |
2 | ||
44586389 | 3 | import re |
84353056 | 4 | import json |
44586389 PH |
5 | |
6 | from .common import InfoExtractor | |
1cc79574 | 7 | from ..compat import compat_urlparse |
44586389 | 8 | from ..utils import ( |
9572013d | 9 | int_or_none, |
494172d2 RA |
10 | parse_duration, |
11 | unified_strdate, | |
44586389 PH |
12 | ) |
13 | ||
14 | ||
15 | class AppleTrailersIE(InfoExtractor): | |
60427f63 | 16 | IE_NAME = 'appletrailers' |
958759f4 | 17 | _VALID_URL = r'https?://(?:www\.|movie)?trailers\.apple\.com/(?:trailers|ca)/(?P<company>[^/]+)/(?P<movie>[^/]+)' |
35b79823 | 18 | _TESTS = [{ |
e759a001 | 19 | 'url': 'http://trailers.apple.com/trailers/wb/manofsteel/', |
11e611a7 | 20 | 'info_dict': { |
494172d2 RA |
21 | 'id': '5111', |
22 | 'title': 'Man of Steel', | |
11e611a7 | 23 | }, |
e759a001 | 24 | 'playlist': [ |
44586389 | 25 | { |
e759a001 S |
26 | 'md5': 'd97a8e575432dbcb81b7c3acb741f8a8', |
27 | 'info_dict': { | |
28 | 'id': 'manofsteel-trailer4', | |
29 | 'ext': 'mov', | |
30 | 'duration': 111, | |
31 | 'title': 'Trailer 4', | |
32 | 'upload_date': '20130523', | |
33 | 'uploader_id': 'wb', | |
44586389 PH |
34 | }, |
35 | }, | |
36 | { | |
e759a001 S |
37 | 'md5': 'b8017b7131b721fb4e8d6f49e1df908c', |
38 | 'info_dict': { | |
39 | 'id': 'manofsteel-trailer3', | |
40 | 'ext': 'mov', | |
41 | 'duration': 182, | |
42 | 'title': 'Trailer 3', | |
43 | 'upload_date': '20130417', | |
44 | 'uploader_id': 'wb', | |
44586389 PH |
45 | }, |
46 | }, | |
47 | { | |
e759a001 S |
48 | 'md5': 'd0f1e1150989b9924679b441f3404d48', |
49 | 'info_dict': { | |
50 | 'id': 'manofsteel-trailer', | |
51 | 'ext': 'mov', | |
52 | 'duration': 148, | |
53 | 'title': 'Trailer', | |
54 | 'upload_date': '20121212', | |
55 | 'uploader_id': 'wb', | |
44586389 PH |
56 | }, |
57 | }, | |
58 | { | |
e759a001 S |
59 | 'md5': '5fe08795b943eb2e757fa95cb6def1cb', |
60 | 'info_dict': { | |
61 | 'id': 'manofsteel-teaser', | |
62 | 'ext': 'mov', | |
63 | 'duration': 93, | |
64 | 'title': 'Teaser', | |
65 | 'upload_date': '20120721', | |
66 | 'uploader_id': 'wb', | |
44586389 | 67 | }, |
28acf550 | 68 | }, |
44586389 | 69 | ] |
261b4c23 | 70 | }, { |
71 | 'url': 'http://trailers.apple.com/trailers/magnolia/blackthorn/', | |
72 | 'info_dict': { | |
76c19510 TF |
73 | 'id': '4489', |
74 | 'title': 'Blackthorn', | |
261b4c23 | 75 | }, |
76 | 'playlist_mincount': 2, | |
494172d2 RA |
77 | 'expected_warnings': ['Unable to download JSON metadata'], |
78 | }, { | |
79 | # json data only available from http://trailers.apple.com/trailers/feeds/data/15881.json | |
80 | 'url': 'http://trailers.apple.com/trailers/fox/kungfupanda3/', | |
81 | 'info_dict': { | |
82 | 'id': '15881', | |
83 | 'title': 'Kung Fu Panda 3', | |
84 | }, | |
85 | 'playlist_mincount': 4, | |
35b79823 S |
86 | }, { |
87 | 'url': 'http://trailers.apple.com/ca/metropole/autrui/', | |
88 | 'only_matching': True, | |
958759f4 YCH |
89 | }, { |
90 | 'url': 'http://movietrailers.apple.com/trailers/focus_features/kuboandthetwostrings/', | |
91 | 'only_matching': True, | |
35b79823 | 92 | }] |
44586389 | 93 | |
84353056 JMF |
94 | _JSON_RE = r'iTunes.playURL\((.*?)\);' |
95 | ||
44586389 PH |
96 | def _real_extract(self, url): |
97 | mobj = re.match(self._VALID_URL, url) | |
98 | movie = mobj.group('movie') | |
99 | uploader_id = mobj.group('company') | |
100 | ||
494172d2 RA |
101 | webpage = self._download_webpage(url, movie) |
102 | film_id = self._search_regex(r"FilmId\s*=\s*'(\d+)'", webpage, 'film id') | |
103 | film_data = self._download_json( | |
104 | 'http://trailers.apple.com/trailers/feeds/data/%s.json' % film_id, | |
105 | film_id, fatal=False) | |
106 | ||
107 | if film_data: | |
108 | entries = [] | |
109 | for clip in film_data.get('clips', []): | |
110 | clip_title = clip['title'] | |
111 | ||
112 | formats = [] | |
113 | for version, version_data in clip.get('versions', {}).items(): | |
114 | for size, size_data in version_data.get('sizes', {}).items(): | |
115 | src = size_data.get('src') | |
116 | if not src: | |
117 | continue | |
118 | formats.append({ | |
119 | 'format_id': '%s-%s' % (version, size), | |
120 | 'url': re.sub(r'_(\d+p.mov)', r'_h\1', src), | |
121 | 'width': int_or_none(size_data.get('width')), | |
122 | 'height': int_or_none(size_data.get('height')), | |
123 | 'language': version[:2], | |
124 | }) | |
125 | self._sort_formats(formats) | |
126 | ||
127 | entries.append({ | |
128 | 'id': movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', clip_title).lower(), | |
129 | 'formats': formats, | |
130 | 'title': clip_title, | |
fee70322 | 131 | 'thumbnail': clip.get('screen') or clip.get('thumb'), |
494172d2 RA |
132 | 'duration': parse_duration(clip.get('runtime') or clip.get('faded')), |
133 | 'upload_date': unified_strdate(clip.get('posted')), | |
134 | 'uploader_id': uploader_id, | |
135 | }) | |
136 | ||
137 | page_data = film_data.get('page', {}) | |
138 | return self.playlist_result(entries, film_id, page_data.get('movie_title')) | |
139 | ||
28acf550 | 140 | playlist_url = compat_urlparse.urljoin(url, 'includes/playlists/itunes.inc') |
5f6a1245 | 141 | |
18258362 | 142 | def fix_html(s): |
28acf550 | 143 | s = re.sub(r'(?s)<script[^<]*?>.*?</script>', '', s) |
7fe37d8a | 144 | s = re.sub(r'<img ([^<]*?)/?>', r'<img \1/>', s) |
18258362 JMF |
145 | # The ' in the onClick attributes are not escaped, it couldn't be parsed |
146 | # like: http://trailers.apple.com/trailers/wb/gravity/ | |
5f6a1245 | 147 | |
18258362 | 148 | def _clean_json(m): |
28acf550 | 149 | return 'iTunes.playURL(%s);' % m.group(1).replace('\'', ''') |
18258362 | 150 | s = re.sub(self._JSON_RE, _clean_json, s) |
e91cdcae | 151 | s = '<html>%s</html>' % s |
18258362 JMF |
152 | return s |
153 | doc = self._download_xml(playlist_url, movie, transform_source=fix_html) | |
44586389 | 154 | |
44586389 PH |
155 | playlist = [] |
156 | for li in doc.findall('./div/ul/li'): | |
84353056 JMF |
157 | on_click = li.find('.//a').attrib['onClick'] |
158 | trailer_info_json = self._search_regex(self._JSON_RE, | |
9e1a5b84 | 159 | on_click, 'trailer info') |
84353056 | 160 | trailer_info = json.loads(trailer_info_json) |
261b4c23 | 161 | first_url = trailer_info.get('url') |
162 | if not first_url: | |
163 | continue | |
84353056 | 164 | title = trailer_info['title'] |
44586389 PH |
165 | video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower() |
166 | thumbnail = li.find('.//img').attrib['src'] | |
84353056 | 167 | upload_date = trailer_info['posted'].replace('-', '') |
44586389 | 168 | |
84353056 JMF |
169 | runtime = trailer_info['runtime'] |
170 | m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime) | |
44586389 PH |
171 | duration = None |
172 | if m: | |
173 | duration = 60 * int(m.group('minutes')) + int(m.group('seconds')) | |
174 | ||
bb4aa62c | 175 | trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower() |
84353056 | 176 | settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id) |
28acf550 | 177 | settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json') |
44586389 | 178 | |
84353056 JMF |
179 | formats = [] |
180 | for format in settings['metadata']['sizes']: | |
181 | # The src is a file pointing to the real video file | |
182 | format_url = re.sub(r'_(\d*p.mov)', r'_h\1', format['src']) | |
183 | formats.append({ | |
184 | 'url': format_url, | |
84353056 | 185 | 'format': format['type'], |
9572013d PH |
186 | 'width': int_or_none(format['width']), |
187 | 'height': int_or_none(format['height']), | |
84353056 | 188 | }) |
7b8af563 PH |
189 | |
190 | self._sort_formats(formats) | |
44586389 | 191 | |
fb7abb31 | 192 | playlist.append({ |
44586389 PH |
193 | '_type': 'video', |
194 | 'id': video_id, | |
44586389 PH |
195 | 'formats': formats, |
196 | 'title': title, | |
197 | 'duration': duration, | |
198 | 'thumbnail': thumbnail, | |
199 | 'upload_date': upload_date, | |
200 | 'uploader_id': uploader_id, | |
e1554a40 JMF |
201 | 'http_headers': { |
202 | 'User-Agent': 'QuickTime compatible (youtube-dl)', | |
203 | }, | |
fb7abb31 | 204 | }) |
44586389 PH |
205 | |
206 | return { | |
207 | '_type': 'playlist', | |
208 | 'id': movie, | |
209 | 'entries': playlist, | |
210 | } | |
60427f63 | 211 | |
212 | ||
213 | class AppleTrailersSectionIE(InfoExtractor): | |
214 | IE_NAME = 'appletrailers:section' | |
215 | _SECTIONS = { | |
216 | 'justadded': { | |
217 | 'feed_path': 'just_added', | |
218 | 'title': 'Just Added', | |
219 | }, | |
220 | 'exclusive': { | |
221 | 'feed_path': 'exclusive', | |
222 | 'title': 'Exclusive', | |
223 | }, | |
224 | 'justhd': { | |
225 | 'feed_path': 'just_hd', | |
226 | 'title': 'Just HD', | |
227 | }, | |
228 | 'mostpopular': { | |
229 | 'feed_path': 'most_pop', | |
230 | 'title': 'Most Popular', | |
231 | }, | |
232 | 'moviestudios': { | |
233 | 'feed_path': 'studios', | |
234 | 'title': 'Movie Studios', | |
235 | }, | |
236 | } | |
237 | _VALID_URL = r'https?://(?:www\.)?trailers\.apple\.com/#section=(?P<id>%s)' % '|'.join(_SECTIONS) | |
238 | _TESTS = [{ | |
239 | 'url': 'http://trailers.apple.com/#section=justadded', | |
240 | 'info_dict': { | |
241 | 'title': 'Just Added', | |
242 | 'id': 'justadded', | |
243 | }, | |
244 | 'playlist_mincount': 80, | |
245 | }, { | |
246 | 'url': 'http://trailers.apple.com/#section=exclusive', | |
247 | 'info_dict': { | |
248 | 'title': 'Exclusive', | |
249 | 'id': 'exclusive', | |
250 | }, | |
251 | 'playlist_mincount': 80, | |
252 | }, { | |
253 | 'url': 'http://trailers.apple.com/#section=justhd', | |
254 | 'info_dict': { | |
255 | 'title': 'Just HD', | |
256 | 'id': 'justhd', | |
257 | }, | |
258 | 'playlist_mincount': 80, | |
259 | }, { | |
260 | 'url': 'http://trailers.apple.com/#section=mostpopular', | |
261 | 'info_dict': { | |
262 | 'title': 'Most Popular', | |
263 | 'id': 'mostpopular', | |
264 | }, | |
76c19510 | 265 | 'playlist_mincount': 30, |
60427f63 | 266 | }, { |
267 | 'url': 'http://trailers.apple.com/#section=moviestudios', | |
268 | 'info_dict': { | |
269 | 'title': 'Movie Studios', | |
270 | 'id': 'moviestudios', | |
271 | }, | |
272 | 'playlist_mincount': 80, | |
273 | }] | |
274 | ||
275 | def _real_extract(self, url): | |
276 | section = self._match_id(url) | |
277 | section_data = self._download_json( | |
278 | 'http://trailers.apple.com/trailers/home/feeds/%s.json' % self._SECTIONS[section]['feed_path'], | |
279 | section) | |
280 | entries = [ | |
281 | self.url_result('http://trailers.apple.com' + e['location']) | |
282 | for e in section_data] | |
283 | return self.playlist_result(entries, section, self._SECTIONS[section]['title']) |