]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/appletrailers.py
New upstream version 2019.01.16
[youtubedl] / youtube_dl / extractor / appletrailers.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..compat import compat_urlparse
8 from ..utils import (
9 int_or_none,
10 parse_duration,
11 unified_strdate,
12 )
13
14
15 class AppleTrailersIE(InfoExtractor):
16 IE_NAME = 'appletrailers'
17 _VALID_URL = r'https?://(?:www\.|movie)?trailers\.apple\.com/(?:trailers|ca)/(?P<company>[^/]+)/(?P<movie>[^/]+)'
18 _TESTS = [{
19 'url': 'http://trailers.apple.com/trailers/wb/manofsteel/',
20 'info_dict': {
21 'id': '5111',
22 'title': 'Man of Steel',
23 },
24 'playlist': [
25 {
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',
34 },
35 },
36 {
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',
45 },
46 },
47 {
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',
56 },
57 },
58 {
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',
67 },
68 },
69 ]
70 }, {
71 'url': 'http://trailers.apple.com/trailers/magnolia/blackthorn/',
72 'info_dict': {
73 'id': '4489',
74 'title': 'Blackthorn',
75 },
76 'playlist_mincount': 2,
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,
86 }, {
87 'url': 'http://trailers.apple.com/ca/metropole/autrui/',
88 'only_matching': True,
89 }, {
90 'url': 'http://movietrailers.apple.com/trailers/focus_features/kuboandthetwostrings/',
91 'only_matching': True,
92 }]
93
94 _JSON_RE = r'iTunes.playURL\((.*?)\);'
95
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
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,
131 'thumbnail': clip.get('screen') or clip.get('thumb'),
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
140 playlist_url = compat_urlparse.urljoin(url, 'includes/playlists/itunes.inc')
141
142 def fix_html(s):
143 s = re.sub(r'(?s)<script[^<]*?>.*?</script>', '', s)
144 s = re.sub(r'<img ([^<]*?)/?>', r'<img \1/>', s)
145 # The ' in the onClick attributes are not escaped, it couldn't be parsed
146 # like: http://trailers.apple.com/trailers/wb/gravity/
147
148 def _clean_json(m):
149 return 'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
150 s = re.sub(self._JSON_RE, _clean_json, s)
151 s = '<html>%s</html>' % s
152 return s
153 doc = self._download_xml(playlist_url, movie, transform_source=fix_html)
154
155 playlist = []
156 for li in doc.findall('./div/ul/li'):
157 on_click = li.find('.//a').attrib['onClick']
158 trailer_info_json = self._search_regex(self._JSON_RE,
159 on_click, 'trailer info')
160 trailer_info = json.loads(trailer_info_json)
161 first_url = trailer_info.get('url')
162 if not first_url:
163 continue
164 title = trailer_info['title']
165 video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
166 thumbnail = li.find('.//img').attrib['src']
167 upload_date = trailer_info['posted'].replace('-', '')
168
169 runtime = trailer_info['runtime']
170 m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
171 duration = None
172 if m:
173 duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
174
175 trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
176 settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id)
177 settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json')
178
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,
185 'format': format['type'],
186 'width': int_or_none(format['width']),
187 'height': int_or_none(format['height']),
188 })
189
190 self._sort_formats(formats)
191
192 playlist.append({
193 '_type': 'video',
194 'id': video_id,
195 'formats': formats,
196 'title': title,
197 'duration': duration,
198 'thumbnail': thumbnail,
199 'upload_date': upload_date,
200 'uploader_id': uploader_id,
201 'http_headers': {
202 'User-Agent': 'QuickTime compatible (youtube-dl)',
203 },
204 })
205
206 return {
207 '_type': 'playlist',
208 'id': movie,
209 'entries': playlist,
210 }
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 },
265 'playlist_mincount': 30,
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'])