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