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