Merge branch 'vgtv' of https://github.com/mrkolby/youtube-dl into mrkolby-vgtv
[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     compat_urllib_parse,
8     compat_urllib_request,
9 )
10
11
12 class GDCVaultIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?gdcvault\.com/play/(?P<id>\d+)/(?P<name>(\w|-)+)'
14     _TESTS = [
15         {
16             'url': 'http://www.gdcvault.com/play/1019721/Doki-Doki-Universe-Sweet-Simple',
17             'md5': '7ce8388f544c88b7ac11c7ab1b593704',
18             'info_dict': {
19                 'id': '1019721',
20                 'ext': 'mp4',
21                 'title': 'Doki-Doki Universe: Sweet, Simple and Genuine (GDC Next 10)'
22             }
23         },
24         {
25             'url': 'http://www.gdcvault.com/play/1015683/Embracing-the-Dark-Art-of',
26             'info_dict': {
27                 'id': '1015683',
28                 'ext': 'flv',
29                 'title': 'Embracing the Dark Art of Mathematical Modeling in AI'
30             },
31             'params': {
32                 'skip_download': True,  # Requires rtmpdump
33             }
34         },
35         {
36             'url': 'http://www.gdcvault.com/play/1015301/Thexder-Meets-Windows-95-or',
37             'md5': 'a5eb77996ef82118afbbe8e48731b98e',
38             'info_dict': {
39                 'id': '1015301',
40                 'ext': 'flv',
41                 'title': 'Thexder Meets Windows 95, or Writing Great Games in the Windows 95 Environment',
42             }
43         }
44     ]
45
46     def _parse_mp4(self, xml_description):
47         video_formats = []
48         mp4_video = xml_description.find('./metadata/mp4video')
49         if mp4_video is None:
50             return None
51
52         mobj = re.match(r'(?P<root>https?://.*?/).*', mp4_video.text)
53         video_root = mobj.group('root')
54         formats = xml_description.findall('./metadata/MBRVideos/MBRVideo')
55         for format in formats:
56             mobj = re.match(r'mp4\:(?P<path>.*)', format.find('streamName').text)
57             url = video_root + mobj.group('path')
58             vbr = format.find('bitrate').text
59             video_formats.append({
60                 'url': url,
61                 'vbr': int(vbr),
62             })
63         return video_formats
64
65     def _parse_flv(self, xml_description):
66         video_formats = []
67         akami_url = xml_description.find('./metadata/akamaiHost').text
68         slide_video_path = xml_description.find('./metadata/slideVideo').text
69         video_formats.append({
70             'url': 'rtmp://' + akami_url + '/' + slide_video_path,
71             'format_note': 'slide deck video',
72             'quality': -2,
73             'preference': -2,
74             'format_id': 'slides',
75         })
76         speaker_video_path = xml_description.find('./metadata/speakerVideo').text
77         video_formats.append({
78             'url': 'rtmp://' + akami_url + '/' + speaker_video_path,
79             'format_note': 'speaker video',
80             'quality': -1,
81             'preference': -1,
82             'format_id': 'speaker',
83         })
84         return video_formats
85
86     def _login(self, webpage_url, video_id):
87         (username, password) = self._get_login_info()
88         if username is None or password is None:
89             self.report_warning('It looks like ' + webpage_url + ' requires a login. Try specifying a username and password and try again.')
90             return None
91
92         mobj = re.match(r'(?P<root_url>https?://.*?/).*', webpage_url)
93         login_url = mobj.group('root_url') + 'api/login.php'
94         logout_url = mobj.group('root_url') + 'logout'
95
96         login_form = {
97             'email': username,
98             'password': password,
99         }
100
101         request = compat_urllib_request.Request(login_url, compat_urllib_parse.urlencode(login_form))
102         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
103         self._download_webpage(request, video_id, 'Logging in')
104         start_page = self._download_webpage(webpage_url, video_id, 'Getting authenticated video page')
105         self._download_webpage(logout_url, video_id, 'Logging out')
106
107         return start_page
108
109     def _real_extract(self, url):
110         mobj = re.match(self._VALID_URL, url)
111
112         video_id = mobj.group('id')
113         webpage_url = 'http://www.gdcvault.com/play/' + video_id
114         start_page = self._download_webpage(webpage_url, video_id)
115
116         direct_url = self._search_regex(
117             r's1\.addVariable\("file",\s*encodeURIComponent\("(/[^"]+)"\)\);',
118             start_page, 'url', default=None)
119         if direct_url:
120             video_url = 'http://www.gdcvault.com/' + direct_url
121             title = self._html_search_regex(
122                 r'<td><strong>Session Name</strong></td>\s*<td>(.*?)</td>',
123                 start_page, 'title')
124
125             return {
126                 'id': video_id,
127                 'url': video_url,
128                 'ext': 'flv',
129                 'title': title,
130             }
131
132         xml_root = self._html_search_regex(
133             r'<iframe src="(?P<xml_root>.*?)player.html.*?".*?</iframe>',
134             start_page, 'xml root', default=None)
135         if xml_root is None:
136             # Probably need to authenticate
137             login_res = self._login(webpage_url, video_id)
138             if login_res is None:
139                 self.report_warning('Could not login.')
140             else:
141                 start_page = login_res
142                 # Grab the url from the authenticated page
143                 xml_root = self._html_search_regex(
144                     r'<iframe src="(.*?)player.html.*?".*?</iframe>',
145                     start_page, 'xml root')
146
147         xml_name = self._html_search_regex(
148             r'<iframe src=".*?\?xml=(.+?\.xml).*?".*?</iframe>',
149             start_page, 'xml filename', default=None)
150         if xml_name is None:
151             # Fallback to the older format
152             xml_name = self._html_search_regex(r'<iframe src=".*?\?xmlURL=xml/(?P<xml_file>.+?\.xml).*?".*?</iframe>', start_page, 'xml filename')
153
154         xml_decription_url = xml_root + 'xml/' + xml_name
155         xml_description = self._download_xml(xml_decription_url, video_id)
156
157         video_title = xml_description.find('./metadata/title').text
158         video_formats = self._parse_mp4(xml_description)
159         if video_formats is None:
160             video_formats = self._parse_flv(xml_description)
161
162         return {
163             'id': video_id,
164             'title': video_title,
165             'formats': video_formats,
166         }