]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/appletrailers.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / appletrailers.py
CommitLineData
84353056 1import json
e897bd82 2import re
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 })
494172d2
RA
123
124 entries.append({
125 'id': movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', clip_title).lower(),
126 'formats': formats,
127 'title': clip_title,
fee70322 128 'thumbnail': clip.get('screen') or clip.get('thumb'),
494172d2
RA
129 'duration': parse_duration(clip.get('runtime') or clip.get('faded')),
130 'upload_date': unified_strdate(clip.get('posted')),
131 'uploader_id': uploader_id,
132 })
133
134 page_data = film_data.get('page', {})
135 return self.playlist_result(entries, film_id, page_data.get('movie_title'))
136
28acf550 137 playlist_url = compat_urlparse.urljoin(url, 'includes/playlists/itunes.inc')
5f6a1245 138
18258362 139 def fix_html(s):
28acf550 140 s = re.sub(r'(?s)<script[^<]*?>.*?</script>', '', s)
7fe37d8a 141 s = re.sub(r'<img ([^<]*?)/?>', r'<img \1/>', s)
18258362
JMF
142 # The ' in the onClick attributes are not escaped, it couldn't be parsed
143 # like: http://trailers.apple.com/trailers/wb/gravity/
5f6a1245 144
18258362 145 def _clean_json(m):
28acf550 146 return 'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
18258362 147 s = re.sub(self._JSON_RE, _clean_json, s)
e91cdcae 148 s = '<html>%s</html>' % s
18258362
JMF
149 return s
150 doc = self._download_xml(playlist_url, movie, transform_source=fix_html)
44586389 151
44586389
PH
152 playlist = []
153 for li in doc.findall('./div/ul/li'):
84353056
JMF
154 on_click = li.find('.//a').attrib['onClick']
155 trailer_info_json = self._search_regex(self._JSON_RE,
9e1a5b84 156 on_click, 'trailer info')
84353056 157 trailer_info = json.loads(trailer_info_json)
261b4c23 158 first_url = trailer_info.get('url')
159 if not first_url:
160 continue
84353056 161 title = trailer_info['title']
44586389
PH
162 video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
163 thumbnail = li.find('.//img').attrib['src']
84353056 164 upload_date = trailer_info['posted'].replace('-', '')
44586389 165
84353056
JMF
166 runtime = trailer_info['runtime']
167 m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
44586389
PH
168 duration = None
169 if m:
170 duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
171
bb4aa62c 172 trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
84353056 173 settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id)
28acf550 174 settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json')
44586389 175
84353056
JMF
176 formats = []
177 for format in settings['metadata']['sizes']:
178 # The src is a file pointing to the real video file
197224b7 179 format_url = re.sub(r'_(\d*p\.mov)', r'_h\1', format['src'])
84353056
JMF
180 formats.append({
181 'url': format_url,
84353056 182 'format': format['type'],
9572013d
PH
183 'width': int_or_none(format['width']),
184 'height': int_or_none(format['height']),
84353056 185 })
7b8af563 186
fb7abb31 187 playlist.append({
44586389
PH
188 '_type': 'video',
189 'id': video_id,
44586389
PH
190 'formats': formats,
191 'title': title,
192 'duration': duration,
193 'thumbnail': thumbnail,
194 'upload_date': upload_date,
195 'uploader_id': uploader_id,
e1554a40 196 'http_headers': {
7a5c1cfe 197 'User-Agent': 'QuickTime compatible (yt-dlp)',
e1554a40 198 },
fb7abb31 199 })
44586389
PH
200
201 return {
202 '_type': 'playlist',
203 'id': movie,
204 'entries': playlist,
205 }
60427f63 206
207
208class AppleTrailersSectionIE(InfoExtractor):
209 IE_NAME = 'appletrailers:section'
210 _SECTIONS = {
211 'justadded': {
212 'feed_path': 'just_added',
213 'title': 'Just Added',
214 },
215 'exclusive': {
216 'feed_path': 'exclusive',
217 'title': 'Exclusive',
218 },
219 'justhd': {
220 'feed_path': 'just_hd',
221 'title': 'Just HD',
222 },
223 'mostpopular': {
224 'feed_path': 'most_pop',
225 'title': 'Most Popular',
226 },
227 'moviestudios': {
228 'feed_path': 'studios',
229 'title': 'Movie Studios',
230 },
231 }
232 _VALID_URL = r'https?://(?:www\.)?trailers\.apple\.com/#section=(?P<id>%s)' % '|'.join(_SECTIONS)
233 _TESTS = [{
234 'url': 'http://trailers.apple.com/#section=justadded',
235 'info_dict': {
236 'title': 'Just Added',
237 'id': 'justadded',
238 },
239 'playlist_mincount': 80,
240 }, {
241 'url': 'http://trailers.apple.com/#section=exclusive',
242 'info_dict': {
243 'title': 'Exclusive',
244 'id': 'exclusive',
245 },
246 'playlist_mincount': 80,
247 }, {
248 'url': 'http://trailers.apple.com/#section=justhd',
249 'info_dict': {
250 'title': 'Just HD',
251 'id': 'justhd',
252 },
253 'playlist_mincount': 80,
254 }, {
255 'url': 'http://trailers.apple.com/#section=mostpopular',
256 'info_dict': {
257 'title': 'Most Popular',
258 'id': 'mostpopular',
259 },
76c19510 260 'playlist_mincount': 30,
60427f63 261 }, {
262 'url': 'http://trailers.apple.com/#section=moviestudios',
263 'info_dict': {
264 'title': 'Movie Studios',
265 'id': 'moviestudios',
266 },
267 'playlist_mincount': 80,
268 }]
269
270 def _real_extract(self, url):
271 section = self._match_id(url)
272 section_data = self._download_json(
273 'http://trailers.apple.com/trailers/home/feeds/%s.json' % self._SECTIONS[section]['feed_path'],
274 section)
275 entries = [
276 self.url_result('http://trailers.apple.com' + e['location'])
277 for e in section_data]
278 return self.playlist_result(entries, section, self._SECTIONS[section]['title'])