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