brynq-sdk-google-drive 0.2.1__tar.gz → 1.0.0__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.0
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,161 @@
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
+
27
+ def _get_google_drive_headers(self):
28
+ access_token = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)['access_token']
29
+ headers = {'Authorization': f'Bearer {access_token}'}
30
+ if self.debug:
31
+ print(headers)
32
+ return headers
33
+
34
+ def list_files(self, drive_id: str = None):
35
+ """
36
+ Get all files from Google Drive, including shared drives or a specific drive is specified
37
+ :param drive_id: ID of a specific drive (optional)
38
+ """
39
+ url = f'{self.base_url}files'
40
+ headers = self._get_google_drive_headers()
41
+ params = {
42
+ 'supportsAllDrives': True,
43
+ 'includeItemsFromAllDrives': True,
44
+ }
45
+ if drive_id:
46
+ params['driveId'] = drive_id
47
+ params['corpora'] = 'drive'
48
+ response = requests.get(url=url, headers=headers, params=params)
49
+ response.raise_for_status()
50
+ return response
51
+
52
+ def upload_file(self, local_file_path: str, remote_file_path: str):
53
+ """
54
+ This method performs the actual file upload to the formerly derived site + drive.
55
+ local_file_path: local path of the file you want to upload
56
+ remote_file_path: remote path of the folder and filename where you want to place the file
57
+ """
58
+ credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
59
+ service = build('drive', 'v3', credentials=credentials)
60
+
61
+ file_metadata = {'name': os.path.basename(remote_file_path)}
62
+ media = MediaFileUpload(local_file_path, resumable=True)
63
+
64
+ response = service.files().create(
65
+ body=file_metadata,
66
+ media_body=media,
67
+ fields='id'
68
+ ).execute()
69
+
70
+ if self.debug:
71
+ print(f'File ID: {response.get("id")}')
72
+
73
+ return response
74
+
75
+ def download_file(self, file_id: str, mime_type: str, local_file_path: str):
76
+ """
77
+ This method downloads a file from Google Drive to the local machine.
78
+ file_id: id of the file on Google Drive. Get it with the list_files function
79
+ mime_type: mime type of the file. Get it with the list_files function
80
+ local_file_path: local path where the file will be downloaded to
81
+ """
82
+ url = f'{self.base_url}files/{file_id}/export?mimeType={mime_type}'
83
+ headers = self._get_google_drive_headers()
84
+ response = requests.get(url=url, headers=headers)
85
+ response.raise_for_status()
86
+ with open(local_file_path, 'wb') as f:
87
+ f.write(response.content)
88
+ return response
89
+
90
+ def download_files(self, local_folder_path: str, remote_folder_path: str):
91
+ """
92
+ This method downloads all files from a Directory in Google Drive to the local machine.
93
+ local_folder_path: local folder where the files will be downloaded to
94
+ remote_folder_path: remote path of the folder you want to get on Google Drive
95
+ """
96
+ credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
97
+ service = build('drive', 'v3', credentials=credentials)
98
+
99
+ # List files in the specified remote folder
100
+ query = f"'{remote_folder_path}' in parents"
101
+ results = service.files().list(q=query, fields="files(id, name)").execute()
102
+ items = results.get('files', [])
103
+
104
+ if not items:
105
+ return 'No files found.'
106
+ else:
107
+ for item in items:
108
+ file_id = item['id']
109
+ file_name = item['name']
110
+ request = service.files().get_media(fileId=file_id)
111
+ fh = BytesIO()
112
+ downloader = MediaIoBaseDownload(fh, request)
113
+ done = False
114
+ while done is False:
115
+ status, done = downloader.next_chunk()
116
+ if self.debug:
117
+ print(f"Download {file_name} {int(status.progress() * 100)}%.")
118
+
119
+ # Save the file to the local folder
120
+ local_file_path = os.path.join(local_folder_path, file_name)
121
+ with open(local_file_path, 'wb') as f:
122
+ f.write(fh.getvalue())
123
+
124
+ return "Download complete"
125
+
126
+ def remove_file(self, remote_file_path: str):
127
+ """
128
+ Remove a file from Google Drive
129
+ remote_file_path: complete path including filename
130
+ :return: response from Google Drive
131
+ """
132
+ credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
133
+ service = build('drive', 'v3', credentials=credentials)
134
+
135
+ # Find the file ID
136
+ query = f"name = '{remote_file_path}'"
137
+ results = service.files().list(q=query, fields="files(id)").execute()
138
+ items = results.get('files', [])
139
+
140
+ if not items:
141
+ print('No files found.')
142
+ return "No files found."
143
+ else:
144
+ file_id = items[0]['id']
145
+ response = service.files().delete(fileId=file_id).execute()
146
+ if self.debug:
147
+ print(f'File {remote_file_path} deleted.')
148
+ return response
149
+
150
+ def remove_folder(self, folder_id: str):
151
+ """
152
+ Remove a folder from Google Drive
153
+ folder: folder id that you want to delete
154
+ """
155
+ credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
156
+ service = build('drive', 'v3', credentials=credentials)
157
+
158
+ response = service.files().delete(fileId=folder_id).execute()
159
+ if self.debug:
160
+ print(f'Folder {folder_id} deleted.')
161
+ 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.0
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.0',
6
6
  description='Google Drive wrapper from BrynQ',
7
7
  long_description='Groogle Drive wrapper from BrynQ',
8
8
  author='BrynQ',