[airmozilla] Add new extractor
[youtube-dl] / 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/(?P<company>[^/]+)/(?P<movie>[^/]+)'
15     _TEST = {
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
68     _JSON_RE = r'iTunes.playURL\((.*?)\);'
69
70     def _real_extract(self, url):
71         mobj = re.match(self._VALID_URL, url)
72         movie = mobj.group('movie')
73         uploader_id = mobj.group('company')
74
75         playlist_url = compat_urlparse.urljoin(url, 'includes/playlists/itunes.inc')
76
77         def fix_html(s):
78             s = re.sub(r'(?s)<script[^<]*?>.*?</script>', '', s)
79             s = re.sub(r'<img ([^<]*?)>', r'<img \1/>', s)
80             # The ' in the onClick attributes are not escaped, it couldn't be parsed
81             # like: http://trailers.apple.com/trailers/wb/gravity/
82
83             def _clean_json(m):
84                 return 'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
85             s = re.sub(self._JSON_RE, _clean_json, s)
86             s = '<html>%s</html>' % s
87             return s
88         doc = self._download_xml(playlist_url, movie, transform_source=fix_html)
89
90         playlist = []
91         for li in doc.findall('./div/ul/li'):
92             on_click = li.find('.//a').attrib['onClick']
93             trailer_info_json = self._search_regex(self._JSON_RE,
94                                                    on_click, 'trailer info')
95             trailer_info = json.loads(trailer_info_json)
96             title = trailer_info['title']
97             video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
98             thumbnail = li.find('.//img').attrib['src']
99             upload_date = trailer_info['posted'].replace('-', '')
100
101             runtime = trailer_info['runtime']
102             m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
103             duration = None
104             if m:
105                 duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
106
107             first_url = trailer_info['url']
108             trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
109             settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id)
110             settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json')
111
112             formats = []
113             for format in settings['metadata']['sizes']:
114                 # The src is a file pointing to the real video file
115                 format_url = re.sub(r'_(\d*p.mov)', r'_h\1', format['src'])
116                 formats.append({
117                     'url': format_url,
118                     'format': format['type'],
119                     'width': int_or_none(format['width']),
120                     'height': int_or_none(format['height']),
121                 })
122
123             self._sort_formats(formats)
124
125             playlist.append({
126                 '_type': 'video',
127                 'id': video_id,
128                 'formats': formats,
129                 'title': title,
130                 'duration': duration,
131                 'thumbnail': thumbnail,
132                 'upload_date': upload_date,
133                 'uploader_id': uploader_id,
134                 'http_headers': {
135                     'User-Agent': 'QuickTime compatible (youtube-dl)',
136                 },
137             })
138
139         return {
140             '_type': 'playlist',
141             'id': movie,
142             'entries': playlist,
143         }