From b99db40e20a19a7ba7e47440c7d3b4a6b7684b0e Mon Sep 17 00:00:00 2001 From: 0o120 <115984687+0o120@users.noreply.github.com> Date: Wed, 1 Feb 2023 17:39:20 -0700 Subject: [PATCH] Faster strm exports --- resources/lib/provider/googledrive.py | 42 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/resources/lib/provider/googledrive.py b/resources/lib/provider/googledrive.py index 60c5bd8..e49361f 100644 --- a/resources/lib/provider/googledrive.py +++ b/resources/lib/provider/googledrive.py @@ -42,7 +42,8 @@ def __init__(self, source_mode = False): super(GoogleDrive, self).__init__('googledrive', source_mode) self.download_requires_auth = True self._items_cache = Cache(KodiUtils.get_addon_info('id'), 'items', datetime.timedelta(minutes=KodiUtils.get_cache_expiration_time())) - + self._instance_cache = {} + def configure(self, account_manager, driveid): super(GoogleDrive, self).configure(account_manager, driveid) drive = account_manager.get_by_driveid('drive', driveid) @@ -153,7 +154,44 @@ def get_folder_items(self, item_driveid=None, item_id=None, path=None, on_items_ parameters = {'albumId': item_id} provider_method = self._photos_provider.post url = '/mediaItems:search' - + + if url == '/files' and item_id and include_download_info: + if not item_id in self._instance_cache: + self._instance_cache[item_id] = [] + ids_per_batch = 120 + id_batch = [item_id] + original_params = {k:v for k,v in parameters.items()} + while id_batch: + query = ['trashed=false', 'and'] + batch = id_batch[:ids_per_batch] + for idx, _id in enumerate(batch, start=1): + q = '\'%s\' in parents' % _id + if idx == 1: + q = '(' + q + if idx == len(batch): + q = q + ')' + query.append(q) + if idx != len(batch): + query.append('or') + parameters['q'] = ' '.join(query) + id_batch = id_batch[ids_per_batch:] + response = provider_method(url, parameters = parameters) + files = self.process_files(response, parameters, on_items_page_completed, include_download_info, on_before_add_item=on_before_add_item) + if self.cancel_operation(): + return + parameters = original_params.copy() + for file in files: + file_mimeType = file['mimetype'] + file_id = file['id'] + parent_id = file['parent'] + if file_mimeType == 'application/vnd.google-apps.folder': + id_batch.append(file_id) + self._instance_cache[file_id] = [] + if parent_id in self._instance_cache: + self._instance_cache[parent_id].append(file) + items.extend(self._instance_cache[item_id]) + return items + files = provider_method(url, parameters = parameters) if self.cancel_operation(): return