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