]>
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 | |
7 | from ..utils import ( | |
84353056 | 8 | compat_urlparse, |
44586389 | 9 | determine_ext, |
44586389 PH |
10 | ) |
11 | ||
12 | ||
13 | class AppleTrailersIE(InfoExtractor): | |
c0ade33e | 14 | _VALID_URL = r'https?://(?:www\.)?trailers\.apple\.com/trailers/(?P<company>[^/]+)/(?P<movie>[^/]+)' |
44586389 | 15 | _TEST = { |
3798eadc PH |
16 | "url": "http://trailers.apple.com/trailers/wb/manofsteel/", |
17 | "playlist": [ | |
44586389 | 18 | { |
3798eadc PH |
19 | "file": "manofsteel-trailer4.mov", |
20 | "md5": "d97a8e575432dbcb81b7c3acb741f8a8", | |
21 | "info_dict": { | |
22 | "duration": 111, | |
23 | "title": "Trailer 4", | |
24 | "upload_date": "20130523", | |
25 | "uploader_id": "wb", | |
44586389 PH |
26 | }, |
27 | }, | |
28 | { | |
3798eadc PH |
29 | "file": "manofsteel-trailer3.mov", |
30 | "md5": "b8017b7131b721fb4e8d6f49e1df908c", | |
31 | "info_dict": { | |
32 | "duration": 182, | |
33 | "title": "Trailer 3", | |
34 | "upload_date": "20130417", | |
35 | "uploader_id": "wb", | |
44586389 PH |
36 | }, |
37 | }, | |
38 | { | |
3798eadc PH |
39 | "file": "manofsteel-trailer.mov", |
40 | "md5": "d0f1e1150989b9924679b441f3404d48", | |
41 | "info_dict": { | |
42 | "duration": 148, | |
43 | "title": "Trailer", | |
44 | "upload_date": "20121212", | |
45 | "uploader_id": "wb", | |
44586389 PH |
46 | }, |
47 | }, | |
48 | { | |
3798eadc PH |
49 | "file": "manofsteel-teaser.mov", |
50 | "md5": "5fe08795b943eb2e757fa95cb6def1cb", | |
51 | "info_dict": { | |
52 | "duration": 93, | |
53 | "title": "Teaser", | |
54 | "upload_date": "20120721", | |
55 | "uploader_id": "wb", | |
44586389 PH |
56 | }, |
57 | } | |
58 | ] | |
59 | } | |
60 | ||
84353056 JMF |
61 | _JSON_RE = r'iTunes.playURL\((.*?)\);' |
62 | ||
44586389 PH |
63 | def _real_extract(self, url): |
64 | mobj = re.match(self._VALID_URL, url) | |
65 | movie = mobj.group('movie') | |
66 | uploader_id = mobj.group('company') | |
67 | ||
84353056 | 68 | playlist_url = compat_urlparse.urljoin(url, u'includes/playlists/itunes.inc') |
18258362 JMF |
69 | def fix_html(s): |
70 | s = re.sub(r'(?s)<script[^<]*?>.*?</script>', u'', s) | |
71 | s = re.sub(r'<img ([^<]*?)>', r'<img \1/>', s) | |
72 | # The ' in the onClick attributes are not escaped, it couldn't be parsed | |
73 | # like: http://trailers.apple.com/trailers/wb/gravity/ | |
74 | def _clean_json(m): | |
75 | return u'iTunes.playURL(%s);' % m.group(1).replace('\'', ''') | |
76 | s = re.sub(self._JSON_RE, _clean_json, s) | |
77 | s = u'<html>' + s + u'</html>' | |
78 | return s | |
79 | doc = self._download_xml(playlist_url, movie, transform_source=fix_html) | |
44586389 | 80 | |
44586389 PH |
81 | playlist = [] |
82 | for li in doc.findall('./div/ul/li'): | |
84353056 JMF |
83 | on_click = li.find('.//a').attrib['onClick'] |
84 | trailer_info_json = self._search_regex(self._JSON_RE, | |
85 | on_click, u'trailer info') | |
86 | trailer_info = json.loads(trailer_info_json) | |
87 | title = trailer_info['title'] | |
44586389 PH |
88 | video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower() |
89 | thumbnail = li.find('.//img').attrib['src'] | |
84353056 | 90 | upload_date = trailer_info['posted'].replace('-', '') |
44586389 | 91 | |
84353056 JMF |
92 | runtime = trailer_info['runtime'] |
93 | m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime) | |
44586389 PH |
94 | duration = None |
95 | if m: | |
96 | duration = 60 * int(m.group('minutes')) + int(m.group('seconds')) | |
97 | ||
84353056 | 98 | first_url = trailer_info['url'] |
bb4aa62c | 99 | trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower() |
84353056 JMF |
100 | settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id) |
101 | settings_json = self._download_webpage(settings_json_url, trailer_id, u'Downloading settings json') | |
102 | settings = json.loads(settings_json) | |
44586389 | 103 | |
84353056 JMF |
104 | formats = [] |
105 | for format in settings['metadata']['sizes']: | |
106 | # The src is a file pointing to the real video file | |
107 | format_url = re.sub(r'_(\d*p.mov)', r'_h\1', format['src']) | |
108 | formats.append({ | |
109 | 'url': format_url, | |
110 | 'ext': determine_ext(format_url), | |
111 | 'format': format['type'], | |
112 | 'width': format['width'], | |
113 | 'height': int(format['height']), | |
114 | }) | |
7b8af563 PH |
115 | |
116 | self._sort_formats(formats) | |
44586389 | 117 | |
fb7abb31 | 118 | playlist.append({ |
44586389 PH |
119 | '_type': 'video', |
120 | 'id': video_id, | |
121 | 'title': title, | |
122 | 'formats': formats, | |
123 | 'title': title, | |
124 | 'duration': duration, | |
125 | 'thumbnail': thumbnail, | |
126 | 'upload_date': upload_date, | |
127 | 'uploader_id': uploader_id, | |
128 | 'user_agent': 'QuickTime compatible (youtube-dl)', | |
fb7abb31 | 129 | }) |
44586389 PH |
130 | |
131 | return { | |
132 | '_type': 'playlist', | |
133 | 'id': movie, | |
134 | 'entries': playlist, | |
135 | } |