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