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