]>
Commit | Line | Data |
---|---|---|
3e5f3df1 | 1 | import re |
2 | ||
984e4d48 | 3 | from .common import InfoExtractor |
a0566bbf | 4 | from ..compat import compat_parse_qs |
8e92d21e | 5 | from ..utils import ( |
fea82c17 | 6 | determine_ext, |
8e92d21e | 7 | ExtractorError, |
2181983a | 8 | get_element_by_class, |
5b251628 | 9 | int_or_none, |
e4e50f60 | 10 | lowercase_escape, |
a0566bbf | 11 | try_get, |
05915e37 | 12 | update_url_query, |
8e92d21e | 13 | ) |
984e4d48 | 14 | |
5b251628 | 15 | |
16 | class GoogleDriveIE(InfoExtractor): | |
1b41da48 S |
17 | _VALID_URL = r'''(?x) |
18 | https?:// | |
19 | (?: | |
20 | (?:docs|drive)\.google\.com/ | |
21 | (?: | |
22 | (?:uc|open)\?.*?id=| | |
23 | file/d/ | |
24 | )| | |
25 | video\.google\.com/get_player\?.*?docid= | |
26 | ) | |
27 | (?P<id>[a-zA-Z0-9_-]{28,}) | |
28 | ''' | |
58e6d097 | 29 | _TESTS = [{ |
5b251628 | 30 | 'url': 'https://drive.google.com/file/d/0ByeS4oOUV-49Zzh4R1J6R09zazQ/edit?pli=1', |
fea82c17 | 31 | 'md5': '5c602afbbf2c1db91831f5d82f678554', |
3e5f3df1 | 32 | 'info_dict': { |
5b251628 | 33 | 'id': '0ByeS4oOUV-49Zzh4R1J6R09zazQ', |
3e5f3df1 | 34 | 'ext': 'mp4', |
5b251628 | 35 | 'title': 'Big Buck Bunny.mp4', |
e4e50f60 | 36 | 'duration': 45, |
3e5f3df1 | 37 | } |
fea82c17 S |
38 | }, { |
39 | # video can't be watched anonymously due to view count limit reached, | |
067aa17e | 40 | # but can be downloaded (see https://github.com/ytdl-org/youtube-dl/issues/14046) |
fea82c17 | 41 | 'url': 'https://drive.google.com/file/d/0B-vUyvmDLdWDcEt4WjBqcmI2XzQ/view', |
a0566bbf | 42 | 'only_matching': True, |
58e6d097 S |
43 | }, { |
44 | # video id is longer than 28 characters | |
45 | 'url': 'https://drive.google.com/file/d/1ENcQ_jeCuj7y19s66_Ou9dRP4GKGsodiDQ/edit', | |
1b41da48 S |
46 | 'only_matching': True, |
47 | }, { | |
48 | 'url': 'https://drive.google.com/open?id=0B2fjwgkl1A_CX083Tkowdmt6d28', | |
49 | 'only_matching': True, | |
50 | }, { | |
51 | 'url': 'https://drive.google.com/uc?id=0B2fjwgkl1A_CX083Tkowdmt6d28', | |
52 | 'only_matching': True, | |
58e6d097 | 53 | }] |
5b251628 | 54 | _FORMATS_EXT = { |
55 | '5': 'flv', | |
56 | '6': 'flv', | |
57 | '13': '3gp', | |
58 | '17': '3gp', | |
59 | '18': 'mp4', | |
60 | '22': 'mp4', | |
61 | '34': 'flv', | |
62 | '35': 'flv', | |
63 | '36': '3gp', | |
64 | '37': 'mp4', | |
65 | '38': 'mp4', | |
66 | '43': 'webm', | |
67 | '44': 'webm', | |
68 | '45': 'webm', | |
69 | '46': 'webm', | |
70 | '59': 'mp4', | |
71 | } | |
05915e37 PV |
72 | _BASE_URL_CAPTIONS = 'https://drive.google.com/timedtext' |
73 | _CAPTIONS_ENTRY_TAG = { | |
74 | 'subtitles': 'track', | |
75 | 'automatic_captions': 'target', | |
76 | } | |
77 | _caption_formats_ext = [] | |
37d9af30 | 78 | _captions_xml = None |
3e5f3df1 | 79 | |
bfd973ec | 80 | @classmethod |
81 | def _extract_embed_urls(cls, url, webpage): | |
3e5f3df1 | 82 | mobj = re.search( |
58e6d097 | 83 | r'<iframe[^>]+src="https?://(?:video\.google\.com/get_player\?.*?docid=|(?:docs|drive)\.google\.com/file/d/)(?P<id>[a-zA-Z0-9_-]{28,})', |
3e5f3df1 | 84 | webpage) |
85 | if mobj: | |
bfd973ec | 86 | yield 'https://drive.google.com/file/d/%s' % mobj.group('id') |
3e5f3df1 | 87 | |
37d9af30 S |
88 | def _download_subtitles_xml(self, video_id, subtitles_id, hl): |
89 | if self._captions_xml: | |
90 | return | |
91 | self._captions_xml = self._download_xml( | |
92 | self._BASE_URL_CAPTIONS, video_id, query={ | |
05915e37 | 93 | 'id': video_id, |
37d9af30 | 94 | 'vid': subtitles_id, |
05915e37 PV |
95 | 'hl': hl, |
96 | 'v': video_id, | |
97 | 'type': 'list', | |
98 | 'tlangs': '1', | |
99 | 'fmts': '1', | |
100 | 'vssids': '1', | |
37d9af30 S |
101 | }, note='Downloading subtitles XML', |
102 | errnote='Unable to download subtitles XML', fatal=False) | |
103 | if self._captions_xml: | |
104 | for f in self._captions_xml.findall('format'): | |
105 | if f.attrib.get('fmt_code') and not f.attrib.get('default'): | |
106 | self._caption_formats_ext.append(f.attrib['fmt_code']) | |
107 | ||
108 | def _get_captions_by_type(self, video_id, subtitles_id, caption_type, | |
109 | origin_lang_code=None): | |
110 | if not subtitles_id or not caption_type: | |
111 | return | |
05915e37 | 112 | captions = {} |
37d9af30 S |
113 | for caption_entry in self._captions_xml.findall( |
114 | self._CAPTIONS_ENTRY_TAG[caption_type]): | |
05915e37 PV |
115 | caption_lang_code = caption_entry.attrib.get('lang_code') |
116 | if not caption_lang_code: | |
117 | continue | |
118 | caption_format_data = [] | |
119 | for caption_format in self._caption_formats_ext: | |
120 | query = { | |
37d9af30 | 121 | 'vid': subtitles_id, |
05915e37 PV |
122 | 'v': video_id, |
123 | 'fmt': caption_format, | |
37d9af30 S |
124 | 'lang': (caption_lang_code if origin_lang_code is None |
125 | else origin_lang_code), | |
05915e37 PV |
126 | 'type': 'track', |
127 | 'name': '', | |
128 | 'kind': '', | |
129 | } | |
37d9af30 | 130 | if origin_lang_code is not None: |
05915e37 PV |
131 | query.update({'tlang': caption_lang_code}) |
132 | caption_format_data.append({ | |
133 | 'url': update_url_query(self._BASE_URL_CAPTIONS, query), | |
134 | 'ext': caption_format, | |
135 | }) | |
136 | captions[caption_lang_code] = caption_format_data | |
05915e37 PV |
137 | return captions |
138 | ||
37d9af30 S |
139 | def _get_subtitles(self, video_id, subtitles_id, hl): |
140 | if not subtitles_id or not hl: | |
141 | return | |
142 | self._download_subtitles_xml(video_id, subtitles_id, hl) | |
143 | if not self._captions_xml: | |
144 | return | |
145 | return self._get_captions_by_type(video_id, subtitles_id, 'subtitles') | |
146 | ||
147 | def _get_automatic_captions(self, video_id, subtitles_id, hl): | |
148 | if not subtitles_id or not hl: | |
149 | return | |
150 | self._download_subtitles_xml(video_id, subtitles_id, hl) | |
151 | if not self._captions_xml: | |
152 | return | |
153 | track = self._captions_xml.find('track') | |
154 | if track is None: | |
155 | return | |
156 | origin_lang_code = track.attrib.get('lang_code') | |
157 | if not origin_lang_code: | |
158 | return | |
159 | return self._get_captions_by_type( | |
160 | video_id, subtitles_id, 'automatic_captions', origin_lang_code) | |
05915e37 | 161 | |
3e5f3df1 | 162 | def _real_extract(self, url): |
163 | video_id = self._match_id(url) | |
a0566bbf | 164 | video_info = compat_parse_qs(self._download_webpage( |
165 | 'https://drive.google.com/get_video_info', | |
166 | video_id, query={'docid': video_id})) | |
167 | ||
168 | def get_value(key): | |
169 | return try_get(video_info, lambda x: x[key][0]) | |
3e5f3df1 | 170 | |
a0566bbf | 171 | reason = get_value('reason') |
172 | title = get_value('title') | |
173 | if not title and reason: | |
174 | raise ExtractorError(reason, expected=True) | |
fea82c17 S |
175 | |
176 | formats = [] | |
a0566bbf | 177 | fmt_stream_map = (get_value('fmt_stream_map') or '').split(',') |
178 | fmt_list = (get_value('fmt_list') or '').split(',') | |
fea82c17 S |
179 | if fmt_stream_map and fmt_list: |
180 | resolutions = {} | |
181 | for fmt in fmt_list: | |
182 | mobj = re.search( | |
183 | r'^(?P<format_id>\d+)/(?P<width>\d+)[xX](?P<height>\d+)', fmt) | |
184 | if mobj: | |
185 | resolutions[mobj.group('format_id')] = ( | |
186 | int(mobj.group('width')), int(mobj.group('height'))) | |
984e4d48 | 187 | |
fea82c17 S |
188 | for fmt_stream in fmt_stream_map: |
189 | fmt_stream_split = fmt_stream.split('|') | |
190 | if len(fmt_stream_split) < 2: | |
191 | continue | |
192 | format_id, format_url = fmt_stream_split[:2] | |
193 | f = { | |
194 | 'url': lowercase_escape(format_url), | |
195 | 'format_id': format_id, | |
196 | 'ext': self._FORMATS_EXT[format_id], | |
197 | } | |
198 | resolution = resolutions.get(format_id) | |
199 | if resolution: | |
200 | f.update({ | |
201 | 'width': resolution[0], | |
202 | 'height': resolution[1], | |
203 | }) | |
204 | formats.append(f) | |
9be9ec59 | 205 | |
fea82c17 S |
206 | source_url = update_url_query( |
207 | 'https://drive.google.com/uc', { | |
208 | 'id': video_id, | |
209 | 'export': 'download', | |
210 | }) | |
da2069fb S |
211 | |
212 | def request_source_file(source_url, kind): | |
213 | return self._request_webpage( | |
214 | source_url, video_id, note='Requesting %s file' % kind, | |
215 | errnote='Unable to request %s file' % kind, fatal=False) | |
216 | urlh = request_source_file(source_url, 'source') | |
fea82c17 | 217 | if urlh: |
da2069fb | 218 | def add_source_format(urlh): |
fea82c17 | 219 | formats.append({ |
da2069fb S |
220 | # Use redirect URLs as download URLs in order to calculate |
221 | # correct cookies in _calc_cookies. | |
222 | # Using original URLs may result in redirect loop due to | |
223 | # google.com's cookies mistakenly used for googleusercontent.com | |
224 | # redirect URLs (see #23919). | |
225 | 'url': urlh.geturl(), | |
fea82c17 S |
226 | 'ext': determine_ext(title, 'mp4').lower(), |
227 | 'format_id': 'source', | |
228 | 'quality': 1, | |
9be9ec59 | 229 | }) |
fea82c17 | 230 | if urlh.headers.get('Content-Disposition'): |
da2069fb | 231 | add_source_format(urlh) |
fea82c17 S |
232 | else: |
233 | confirmation_webpage = self._webpage_read_content( | |
234 | urlh, url, video_id, note='Downloading confirmation page', | |
235 | errnote='Unable to confirm download', fatal=False) | |
236 | if confirmation_webpage: | |
237 | confirm = self._search_regex( | |
238 | r'confirm=([^&"\']+)', confirmation_webpage, | |
2181983a | 239 | 'confirmation code', default=None) |
fea82c17 | 240 | if confirm: |
da2069fb | 241 | confirmed_source_url = update_url_query(source_url, { |
fea82c17 | 242 | 'confirm': confirm, |
da2069fb S |
243 | }) |
244 | urlh = request_source_file(confirmed_source_url, 'confirmed source') | |
245 | if urlh and urlh.headers.get('Content-Disposition'): | |
246 | add_source_format(urlh) | |
2181983a | 247 | else: |
248 | self.report_warning( | |
249 | get_element_by_class('uc-error-subcaption', confirmation_webpage) | |
250 | or get_element_by_class('uc-error-caption', confirmation_webpage) | |
251 | or 'unable to extract confirmation code') | |
fea82c17 | 252 | |
a0566bbf | 253 | if not formats and reason: |
b7da73eb | 254 | self.raise_no_formats(reason, expected=True) |
fea82c17 | 255 | |
984e4d48 | 256 | self._sort_formats(formats) |
257 | ||
a0566bbf | 258 | hl = get_value('hl') |
37d9af30 | 259 | subtitles_id = None |
a0566bbf | 260 | ttsurl = get_value('ttsurl') |
05915e37 | 261 | if ttsurl: |
37d9af30 S |
262 | # the video Id for subtitles will be the last value in the ttsurl |
263 | # query string | |
264 | subtitles_id = ttsurl.encode('utf-8').decode( | |
265 | 'unicode_escape').split('=')[-1] | |
05915e37 | 266 | |
9809740b | 267 | self.cookiejar.clear(domain='.google.com', path='/', name='NID') |
67475072 | 268 | |
984e4d48 | 269 | return { |
270 | 'id': video_id, | |
271 | 'title': title, | |
a0566bbf | 272 | 'thumbnail': 'https://drive.google.com/thumbnail?id=' + video_id, |
273 | 'duration': int_or_none(get_value('length_seconds')), | |
5b251628 | 274 | 'formats': formats, |
37d9af30 S |
275 | 'subtitles': self.extract_subtitles(video_id, subtitles_id, hl), |
276 | 'automatic_captions': self.extract_automatic_captions( | |
277 | video_id, subtitles_id, hl), | |
984e4d48 | 278 | } |
145c5a83 ES |
279 | |
280 | ||
281 | class GoogleDriveFolderIE(InfoExtractor): | |
282 | IE_NAME = 'GoogleDrive:Folder' | |
283 | _VALID_URL = r'https?://(?:docs|drive)\.google\.com/drive/folders/(?P<id>[\w-]{28,})' | |
284 | _TESTS = [{ | |
285 | 'url': 'https://drive.google.com/drive/folders/1dQ4sx0-__Nvg65rxTSgQrl7VyW_FZ9QI', | |
286 | 'info_dict': { | |
287 | 'id': '1dQ4sx0-__Nvg65rxTSgQrl7VyW_FZ9QI', | |
288 | 'title': 'Forrest' | |
289 | }, | |
290 | 'playlist_count': 3, | |
291 | }] | |
292 | _BOUNDARY = '=====vc17a3rwnndj=====' | |
293 | _REQUEST = "/drive/v2beta/files?openDrive=true&reason=102&syncType=0&errorRecovery=false&q=trashed%20%3D%20false%20and%20'{folder_id}'%20in%20parents&fields=kind%2CnextPageToken%2Citems(kind%2CmodifiedDate%2CmodifiedByMeDate%2ClastViewedByMeDate%2CfileSize%2Cowners(kind%2CpermissionId%2Cid)%2ClastModifyingUser(kind%2CpermissionId%2Cid)%2ChasThumbnail%2CthumbnailVersion%2Ctitle%2Cid%2CresourceKey%2Cshared%2CsharedWithMeDate%2CuserPermission(role)%2CexplicitlyTrashed%2CmimeType%2CquotaBytesUsed%2Ccopyable%2CfileExtension%2CsharingUser(kind%2CpermissionId%2Cid)%2Cspaces%2Cversion%2CteamDriveId%2ChasAugmentedPermissions%2CcreatedDate%2CtrashingUser(kind%2CpermissionId%2Cid)%2CtrashedDate%2Cparents(id)%2CshortcutDetails(targetId%2CtargetMimeType%2CtargetLookupStatus)%2Ccapabilities(canCopy%2CcanDownload%2CcanEdit%2CcanAddChildren%2CcanDelete%2CcanRemoveChildren%2CcanShare%2CcanTrash%2CcanRename%2CcanReadTeamDrive%2CcanMoveTeamDriveItem)%2Clabels(starred%2Ctrashed%2Crestricted%2Cviewed))%2CincompleteSearch&appDataFilter=NO_APP_DATA&spaces=drive&pageToken={page_token}&maxResults=50&supportsTeamDrives=true&includeItemsFromAllDrives=true&corpora=default&orderBy=folder%2Ctitle_natural%20asc&retryCount=0&key={key} HTTP/1.1" | |
294 | _DATA = f'''--{_BOUNDARY} | |
295 | content-type: application/http | |
296 | content-transfer-encoding: binary | |
297 | ||
298 | GET %s | |
299 | ||
300 | --{_BOUNDARY} | |
301 | ''' | |
302 | ||
303 | def _call_api(self, folder_id, key, data, **kwargs): | |
304 | response = self._download_webpage( | |
305 | 'https://clients6.google.com/batch/drive/v2beta', | |
306 | folder_id, data=data.encode('utf-8'), | |
307 | headers={ | |
308 | 'Content-Type': 'text/plain;charset=UTF-8;', | |
309 | 'Origin': 'https://drive.google.com', | |
310 | }, query={ | |
311 | '$ct': f'multipart/mixed; boundary="{self._BOUNDARY}"', | |
312 | 'key': key | |
313 | }, **kwargs) | |
314 | return self._search_json('', response, 'api response', folder_id, **kwargs) or {} | |
315 | ||
316 | def _get_folder_items(self, folder_id, key): | |
317 | page_token = '' | |
318 | while page_token is not None: | |
319 | request = self._REQUEST.format(folder_id=folder_id, page_token=page_token, key=key) | |
320 | page = self._call_api(folder_id, key, self._DATA % request) | |
321 | yield from page['items'] | |
322 | page_token = page.get('nextPageToken') | |
323 | ||
324 | def _real_extract(self, url): | |
325 | folder_id = self._match_id(url) | |
326 | ||
327 | webpage = self._download_webpage(url, folder_id) | |
328 | key = self._search_regex(r'"(\w{39})"', webpage, 'key') | |
329 | ||
330 | folder_info = self._call_api(folder_id, key, self._DATA % f'/drive/v2beta/files/{folder_id} HTTP/1.1', fatal=False) | |
331 | ||
332 | return self.playlist_from_matches( | |
333 | self._get_folder_items(folder_id, key), folder_id, folder_info.get('title'), | |
334 | ie=GoogleDriveIE, getter=lambda item: f'https://drive.google.com/file/d/{item["id"]}') |