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