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