brynq-sdk-google-drive 0.2.1__tar.gz → 1.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq_sdk_google_drive
3
- Version: 0.2.1
3
+ Version: 1.0.1
4
4
  Summary: Google Drive wrapper from BrynQ
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -0,0 +1 @@
1
+ from .google_drive import GoogleDrive
@@ -0,0 +1,162 @@
1
+ import os
2
+ from typing import List, Union
3
+ import requests
4
+ import json
5
+ from io import BytesIO
6
+ import typing
7
+ from googleapiclient.discovery import build
8
+ from googleapiclient.http import MediaFileUpload
9
+ from brynq_sdk_brynq import BrynQ
10
+
11
+ class GoogleDrive(BrynQ):
12
+ def __init__(self, label: Union[str, List], debug: bool = False):
13
+ """
14
+ :param label: label of the Google Drive system in BrynQ
15
+ :param debug: set to True to enable debug logging
16
+ """
17
+ super().__init__()
18
+ api_version = 'v3'
19
+ self.base_url = f'https://www.googleapis.com/drive/{api_version}/'
20
+ credentials = self.get_system_credential(system='google-drive', label=label)
21
+ self.debug = debug
22
+ if self.debug:
23
+ print(f"credentials: {credentials}")
24
+ self.access_token = credentials['auth']['access_token']
25
+ self.brynq_system_id = credentials['id']
26
+ self.timeout = 3600
27
+
28
+ def _get_google_drive_headers(self):
29
+ access_token = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)['access_token']
30
+ headers = {'Authorization': f'Bearer {access_token}'}
31
+ if self.debug:
32
+ print(headers)
33
+ return headers
34
+
35
+ def list_files(self, drive_id: str = None):
36
+ """
37
+ Get all files from Google Drive, including shared drives or a specific drive is specified
38
+ :param drive_id: ID of a specific drive (optional)
39
+ """
40
+ url = f'{self.base_url}files'
41
+ headers = self._get_google_drive_headers()
42
+ params = {
43
+ 'supportsAllDrives': True,
44
+ 'includeItemsFromAllDrives': True,
45
+ }
46
+ if drive_id:
47
+ params['driveId'] = drive_id
48
+ params['corpora'] = 'drive'
49
+ response = requests.get(url=url, headers=headers, params=params, timeout=self.timeout)
50
+ response.raise_for_status()
51
+ return response
52
+
53
+ def upload_file(self, local_file_path: str, remote_file_path: str):
54
+ """
55
+ This method performs the actual file upload to the formerly derived site + drive.
56
+ local_file_path: local path of the file you want to upload
57
+ remote_file_path: remote path of the folder and filename where you want to place the file
58
+ """
59
+ credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
60
+ service = build('drive', 'v3', credentials=credentials)
61
+
62
+ file_metadata = {'name': os.path.basename(remote_file_path)}
63
+ media = MediaFileUpload(local_file_path, resumable=True)
64
+
65
+ response = service.files().create(
66
+ body=file_metadata,
67
+ media_body=media,
68
+ fields='id'
69
+ ).execute()
70
+
71
+ if self.debug:
72
+ print(f'File ID: {response.get("id")}')
73
+
74
+ return response
75
+
76
+ def download_file(self, file_id: str, mime_type: str, local_file_path: str):
77
+ """
78
+ This method downloads a file from Google Drive to the local machine.
79
+ file_id: id of the file on Google Drive. Get it with the list_files function
80
+ mime_type: mime type of the file. Get it with the list_files function
81
+ local_file_path: local path where the file will be downloaded to
82
+ """
83
+ url = f'{self.base_url}files/{file_id}/export?mimeType={mime_type}'
84
+ headers = self._get_google_drive_headers()
85
+ response = requests.get(url=url, headers=headers, timeout=self.timeout)
86
+ response.raise_for_status()
87
+ with open(local_file_path, 'wb') as f:
88
+ f.write(response.content)
89
+ return response
90
+
91
+ def download_files(self, local_folder_path: str, remote_folder_path: str):
92
+ """
93
+ This method downloads all files from a Directory in Google Drive to the local machine.
94
+ local_folder_path: local folder where the files will be downloaded to
95
+ remote_folder_path: remote path of the folder you want to get on Google Drive
96
+ """
97
+ credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
98
+ service = build('drive', 'v3', credentials=credentials)
99
+
100
+ # List files in the specified remote folder
101
+ query = f"'{remote_folder_path}' in parents"
102
+ results = service.files().list(q=query, fields="files(id, name)").execute()
103
+ items = results.get('files', [])
104
+
105
+ if not items:
106
+ return 'No files found.'
107
+ else:
108
+ for item in items:
109
+ file_id = item['id']
110
+ file_name = item['name']
111
+ request = service.files().get_media(fileId=file_id)
112
+ fh = BytesIO()
113
+ downloader = MediaIoBaseDownload(fh, request)
114
+ done = False
115
+ while done is False:
116
+ status, done = downloader.next_chunk()
117
+ if self.debug:
118
+ print(f"Download {file_name} {int(status.progress() * 100)}%.")
119
+
120
+ # Save the file to the local folder
121
+ local_file_path = os.path.join(local_folder_path, file_name)
122
+ with open(local_file_path, 'wb') as f:
123
+ f.write(fh.getvalue())
124
+
125
+ return "Download complete"
126
+
127
+ def remove_file(self, remote_file_path: str):
128
+ """
129
+ Remove a file from Google Drive
130
+ remote_file_path: complete path including filename
131
+ :return: response from Google Drive
132
+ """
133
+ credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
134
+ service = build('drive', 'v3', credentials=credentials)
135
+
136
+ # Find the file ID
137
+ query = f"name = '{remote_file_path}'"
138
+ results = service.files().list(q=query, fields="files(id)").execute()
139
+ items = results.get('files', [])
140
+
141
+ if not items:
142
+ print('No files found.')
143
+ return "No files found."
144
+ else:
145
+ file_id = items[0]['id']
146
+ response = service.files().delete(fileId=file_id).execute()
147
+ if self.debug:
148
+ print(f'File {remote_file_path} deleted.')
149
+ return response
150
+
151
+ def remove_folder(self, folder_id: str):
152
+ """
153
+ Remove a folder from Google Drive
154
+ folder: folder id that you want to delete
155
+ """
156
+ credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
157
+ service = build('drive', 'v3', credentials=credentials)
158
+
159
+ response = service.files().delete(fileId=folder_id).execute()
160
+ if self.debug:
161
+ print(f'Folder {folder_id} deleted.')
162
+ return response
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq-sdk-google-drive
3
- Version: 0.2.1
3
+ Version: 1.0.1
4
4
  Summary: Google Drive wrapper from BrynQ
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -1,4 +1,6 @@
1
1
  setup.py
2
+ brynq_sdk_google_drive/__init__.py
3
+ brynq_sdk_google_drive/google_drive.py
2
4
  brynq_sdk_google_drive.egg-info/PKG-INFO
3
5
  brynq_sdk_google_drive.egg-info/SOURCES.txt
4
6
  brynq_sdk_google_drive.egg-info/dependency_links.txt
@@ -0,0 +1 @@
1
+ brynq_sdk_google_drive
@@ -2,7 +2,7 @@ from setuptools import setup, find_namespace_packages
2
2
 
3
3
  setup(
4
4
  name='brynq_sdk_google_drive',
5
- version='0.2.1',
5
+ version='1.0.1',
6
6
  description='Google Drive wrapper from BrynQ',
7
7
  long_description='Groogle Drive wrapper from BrynQ',
8
8
  author='BrynQ',