[gdcvault] Fix for videos with hard-coded hostnames
[youtube-dl] / youtube_dl / extractor / gdcvault.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     remove_end,
8     HEADRequest,
9     sanitized_Request,
10     urlencode_postdata,
11 )
12
13
14 class GDCVaultIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?gdcvault\.com/play/(?P<id>\d+)/(?P<name>(\w|-)+)?'
16     _NETRC_MACHINE = 'gdcvault'
17     _TESTS = [
18         {
19             'url': 'http://www.gdcvault.com/play/1019721/Doki-Doki-Universe-Sweet-Simple',
20             'md5': '7ce8388f544c88b7ac11c7ab1b593704',
21             'info_dict': {
22                 'id': '1019721',
23                 'display_id': 'Doki-Doki-Universe-Sweet-Simple',
24                 'ext': 'mp4',
25                 'title': 'Doki-Doki Universe: Sweet, Simple and Genuine (GDC Next 10)'
26             }
27         },
28         {
29             'url': 'http://www.gdcvault.com/play/1015683/Embracing-the-Dark-Art-of',
30             'info_dict': {
31                 'id': '1015683',
32                 'display_id': 'Embracing-the-Dark-Art-of',
33                 'ext': 'flv',
34                 'title': 'Embracing the Dark Art of Mathematical Modeling in AI'
35             },
36             'params': {
37                 'skip_download': True,  # Requires rtmpdump
38             }
39         },
40         {
41             'url': 'http://www.gdcvault.com/play/1015301/Thexder-Meets-Windows-95-or',
42             'md5': 'a5eb77996ef82118afbbe8e48731b98e',
43             'info_dict': {
44                 'id': '1015301',
45                 'display_id': 'Thexder-Meets-Windows-95-or',
46                 'ext': 'flv',
47                 'title': 'Thexder Meets Windows 95, or Writing Great Games in the Windows 95 Environment',
48             },
49             'skip': 'Requires login',
50         },
51         {
52             'url': 'http://gdcvault.com/play/1020791/',
53             'only_matching': True,
54         },
55         {
56             'url': 'http://gdcvault.com/play/1023460/Tenacious-Design-and-The-Interface',
57             'md5': 'a8efb6c31ed06ca8739294960b2dbabd',
58             'info_dict': {
59                 'id': '1023460',
60                 'ext': 'mp4',
61                 'display_id': 'Tenacious-Design-and-The-Interface',
62                 'title': 'Tenacious Design and The Interface of \'Destiny\'',
63             },
64         },
65     ]
66
67     def _parse_mp4(self, xml_description):
68         video_formats = []
69         video_root = None
70
71         mp4_video = xml_description.find('./metadata/mp4video')
72         if mp4_video is not None:
73             mobj = re.match(r'(?P<root>https?://.*?/).*', mp4_video.text)
74             video_root = mobj.group('root')
75         if video_root is None:
76             # Hard-coded in http://evt.dispeak.com/ubm/gdc/sf16/custom/player2.js
77             video_root = 'http://s3-2u.digitallyspeaking.com/'
78
79         formats = xml_description.findall('./metadata/MBRVideos/MBRVideo')
80         if not formats:
81             return None
82         for format in formats:
83             mobj = re.match(r'mp4\:(?P<path>.*)', format.find('streamName').text)
84             url = video_root + mobj.group('path')
85             vbr = format.find('bitrate').text
86             video_formats.append({
87                 'url': url,
88                 'vbr': int(vbr),
89             })
90         return video_formats
91
92     def _parse_flv(self, xml_description):
93         formats = []
94         akamai_url = xml_description.find('./metadata/akamaiHost').text
95         audios = xml_description.find('./metadata/audios')
96         if audios is not None:
97             for audio in audios:
98                 formats.append({
99                     'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
100                     'play_path': remove_end(audio.get('url'), '.flv'),
101                     'ext': 'flv',
102                     'vcodec': 'none',
103                     'format_id': audio.get('code'),
104                 })
105         slide_video_path = xml_description.find('./metadata/slideVideo').text
106         formats.append({
107             'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
108             'play_path': remove_end(slide_video_path, '.flv'),
109             'ext': 'flv',
110             'format_note': 'slide deck video',
111             'quality': -2,
112             'preference': -2,
113             'format_id': 'slides',
114         })
115         speaker_video_path = xml_description.find('./metadata/speakerVideo').text
116         formats.append({
117             'url': 'rtmp://%s/ondemand?ovpfv=1.1' % akamai_url,
118             'play_path': remove_end(speaker_video_path, '.flv'),
119             'ext': 'flv',
120             'format_note': 'speaker video',
121             'quality': -1,
122             'preference': -1,
123             'format_id': 'speaker',
124         })
125         return formats
126
127     def _login(self, webpage_url, display_id):
128         (username, password) = self._get_login_info()
129         if username is None or password is None:
130             self.report_warning('It looks like ' + webpage_url + ' requires a login. Try specifying a username and password and try again.')
131             return None
132
133         mobj = re.match(r'(?P<root_url>https?://.*?/).*', webpage_url)
134         login_url = mobj.group('root_url') + 'api/login.php'
135         logout_url = mobj.group('root_url') + 'logout'
136
137         login_form = {
138             'email': username,
139             'password': password,
140         }
141
142         request = sanitized_Request(login_url, urlencode_postdata(login_form))
143         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
144         self._download_webpage(request, display_id, 'Logging in')
145         start_page = self._download_webpage(webpage_url, display_id, 'Getting authenticated video page')
146         self._download_webpage(logout_url, display_id, 'Logging out')
147
148         return start_page
149
150     def _real_extract(self, url):
151         mobj = re.match(self._VALID_URL, url)
152
153         video_id = mobj.group('id')
154         display_id = mobj.group('name') or video_id
155
156         webpage_url = 'http://www.gdcvault.com/play/' + video_id
157         start_page = self._download_webpage(webpage_url, display_id)
158
159         direct_url = self._search_regex(
160             r's1\.addVariable\("file",\s*encodeURIComponent\("(/[^"]+)"\)\);',
161             start_page, 'url', default=None)
162         if direct_url:
163             title = self._html_search_regex(
164                 r'<td><strong>Session Name</strong></td>\s*<td>(.*?)</td>',
165                 start_page, 'title')
166             video_url = 'http://www.gdcvault.com' + direct_url
167             # resolve the url so that we can detect the correct extension
168             head = self._request_webpage(HEADRequest(video_url), video_id)
169             video_url = head.geturl()
170
171             return {
172                 'id': video_id,
173                 'display_id': display_id,
174                 'url': video_url,
175                 'title': title,
176             }
177
178         PLAYER_REGEX = r'<iframe src="(?P<xml_root>.+?)/player.*?\.html.*?".*?</iframe>'
179
180         xml_root = self._html_search_regex(
181             PLAYER_REGEX, start_page, 'xml root', default=None)
182         if xml_root is None:
183             # Probably need to authenticate
184             login_res = self._login(webpage_url, display_id)
185             if login_res is None:
186                 self.report_warning('Could not login.')
187             else:
188                 start_page = login_res
189                 # Grab the url from the authenticated page
190                 xml_root = self._html_search_regex(
191                     PLAYER_REGEX, start_page, 'xml root')
192
193         xml_name = self._html_search_regex(
194             r'<iframe src=".*?\?xml=(.+?\.xml).*?".*?</iframe>',
195             start_page, 'xml filename', default=None)
196         if xml_name is None:
197             # Fallback to the older format
198             xml_name = self._html_search_regex(
199                 r'<iframe src=".*?\?xmlURL=xml/(?P<xml_file>.+?\.xml).*?".*?</iframe>',
200                 start_page, 'xml filename')
201
202         xml_description = self._download_xml(
203             '%s/xml/%s' % (xml_root, xml_name), display_id)
204
205         video_title = xml_description.find('./metadata/title').text
206         video_formats = self._parse_mp4(xml_description)
207         if video_formats is None:
208             video_formats = self._parse_flv(xml_description)
209
210         return {
211             'id': video_id,
212             'display_id': display_id,
213             'title': video_title,
214             'formats': video_formats,
215         }