]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/gdcvault.py
[nzz] Add new extractor(#4407)
[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     HEADRequest,
8     sanitized_Request,
9     urlencode_postdata,
10 )
11
12
13 class GDCVaultIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?gdcvault\.com/play/(?P<id>\d+)/(?P<name>(\w|-)+)?'
15     _NETRC_MACHINE = 'gdcvault'
16     _TESTS = [
17         {
18             'url': 'http://www.gdcvault.com/play/1019721/Doki-Doki-Universe-Sweet-Simple',
19             'md5': '7ce8388f544c88b7ac11c7ab1b593704',
20             'info_dict': {
21                 'id': '1019721',
22                 'display_id': 'Doki-Doki-Universe-Sweet-Simple',
23                 'ext': 'mp4',
24                 'title': 'Doki-Doki Universe: Sweet, Simple and Genuine (GDC Next 10)'
25             }
26         },
27         {
28             'url': 'http://www.gdcvault.com/play/1015683/Embracing-the-Dark-Art-of',
29             'info_dict': {
30                 'id': '1015683',
31                 'display_id': 'Embracing-the-Dark-Art-of',
32                 'ext': 'flv',
33                 'title': 'Embracing the Dark Art of Mathematical Modeling in AI'
34             },
35             'params': {
36                 'skip_download': True,  # Requires rtmpdump
37             }
38         },
39         {
40             'url': 'http://www.gdcvault.com/play/1015301/Thexder-Meets-Windows-95-or',
41             'md5': 'a5eb77996ef82118afbbe8e48731b98e',
42             'info_dict': {
43                 'id': '1015301',
44                 'display_id': 'Thexder-Meets-Windows-95-or',
45                 'ext': 'flv',
46                 'title': 'Thexder Meets Windows 95, or Writing Great Games in the Windows 95 Environment',
47             },
48             'skip': 'Requires login',
49         },
50         {
51             'url': 'http://gdcvault.com/play/1020791/',
52             'only_matching': True,
53         },
54         {
55             # Hard-coded hostname
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             # Multiple audios
67             'url': 'http://www.gdcvault.com/play/1014631/Classic-Game-Postmortem-PAC',
68             'info_dict': {
69                 'id': '1014631',
70                 'ext': 'flv',
71                 'title': 'How to Create a Good Game - From My Experience of Designing Pac-Man',
72             },
73             'params': {
74                 'skip_download': True,  # Requires rtmpdump
75                 'format': 'jp',  # The japanese audio
76             }
77         },
78     ]
79
80     def _login(self, webpage_url, display_id):
81         (username, password) = self._get_login_info()
82         if username is None or password is None:
83             self.report_warning('It looks like ' + webpage_url + ' requires a login. Try specifying a username and password and try again.')
84             return None
85
86         mobj = re.match(r'(?P<root_url>https?://.*?/).*', webpage_url)
87         login_url = mobj.group('root_url') + 'api/login.php'
88         logout_url = mobj.group('root_url') + 'logout'
89
90         login_form = {
91             'email': username,
92             'password': password,
93         }
94
95         request = sanitized_Request(login_url, urlencode_postdata(login_form))
96         request.add_header('Content-Type', 'application/x-www-form-urlencoded')
97         self._download_webpage(request, display_id, 'Logging in')
98         start_page = self._download_webpage(webpage_url, display_id, 'Getting authenticated video page')
99         self._download_webpage(logout_url, display_id, 'Logging out')
100
101         return start_page
102
103     def _real_extract(self, url):
104         mobj = re.match(self._VALID_URL, url)
105
106         video_id = mobj.group('id')
107         display_id = mobj.group('name') or video_id
108
109         webpage_url = 'http://www.gdcvault.com/play/' + video_id
110         start_page = self._download_webpage(webpage_url, display_id)
111
112         direct_url = self._search_regex(
113             r's1\.addVariable\("file",\s*encodeURIComponent\("(/[^"]+)"\)\);',
114             start_page, 'url', default=None)
115         if direct_url:
116             title = self._html_search_regex(
117                 r'<td><strong>Session Name</strong></td>\s*<td>(.*?)</td>',
118                 start_page, 'title')
119             video_url = 'http://www.gdcvault.com' + direct_url
120             # resolve the url so that we can detect the correct extension
121             head = self._request_webpage(HEADRequest(video_url), video_id)
122             video_url = head.geturl()
123
124             return {
125                 'id': video_id,
126                 'display_id': display_id,
127                 'url': video_url,
128                 'title': title,
129             }
130
131         PLAYER_REGEX = r'<iframe src="(?P<xml_root>.+?)/player.*?\.html.*?".*?</iframe>'
132
133         xml_root = self._html_search_regex(
134             PLAYER_REGEX, start_page, 'xml root', default=None)
135         if xml_root is None:
136             # Probably need to authenticate
137             login_res = self._login(webpage_url, display_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                     PLAYER_REGEX, start_page, 'xml root')
145
146         xml_name = self._html_search_regex(
147             r'<iframe src=".*?\?xml=(.+?\.xml).*?".*?</iframe>',
148             start_page, 'xml filename', default=None)
149         if xml_name is None:
150             # Fallback to the older format
151             xml_name = self._html_search_regex(
152                 r'<iframe src=".*?\?xmlURL=xml/(?P<xml_file>.+?\.xml).*?".*?</iframe>',
153                 start_page, 'xml filename')
154
155         return {
156             '_type': 'url_transparent',
157             'id': video_id,
158             'display_id': display_id,
159             'url': '%s/xml/%s' % (xml_root, xml_name),
160             'ie_key': 'DigitallySpeaking',
161         }