brynq-sdk-google-drive 0.1.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.
- brynq_sdk_google_drive-0.1.0/PKG-INFO +10 -0
- brynq_sdk_google_drive-0.1.0/brynq_sdk/google_drive/__init__.py +1 -0
- brynq_sdk_google_drive-0.1.0/brynq_sdk/google_drive/google_drive.py +154 -0
- brynq_sdk_google_drive-0.1.0/brynq_sdk_google_drive.egg-info/PKG-INFO +10 -0
- brynq_sdk_google_drive-0.1.0/brynq_sdk_google_drive.egg-info/SOURCES.txt +9 -0
- brynq_sdk_google_drive-0.1.0/brynq_sdk_google_drive.egg-info/dependency_links.txt +1 -0
- brynq_sdk_google_drive-0.1.0/brynq_sdk_google_drive.egg-info/not-zip-safe +1 -0
- brynq_sdk_google_drive-0.1.0/brynq_sdk_google_drive.egg-info/requires.txt +3 -0
- brynq_sdk_google_drive-0.1.0/brynq_sdk_google_drive.egg-info/top_level.txt +1 -0
- brynq_sdk_google_drive-0.1.0/setup.cfg +4 -0
- brynq_sdk_google_drive-0.1.0/setup.py +19 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: brynq_sdk_google_drive
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Google Drive wrapper from BrynQ
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: BrynQ
|
|
7
|
+
Author-email: support@brynq.com
|
|
8
|
+
License: BrynQ License
|
|
9
|
+
Description: Groogle Drive wrapper from BrynQ
|
|
10
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from brynq_sdk.google_drive.google_drive import GoogleDrive
|
|
@@ -0,0 +1,154 @@
|
|
|
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
|
+
|
|
12
|
+
class GoogleDrive(BrynQ):
|
|
13
|
+
def __init__(self, label: Union[str, List], debug: bool = False):
|
|
14
|
+
"""
|
|
15
|
+
:param label: label of the Google Drive system in BrynQ
|
|
16
|
+
:param debug: set to True to enable debug logging
|
|
17
|
+
"""
|
|
18
|
+
super().__init__()
|
|
19
|
+
api_version = 'v3'
|
|
20
|
+
self.base_url = f'https://www.googleapis.com/drive/{api_version}/'
|
|
21
|
+
credentials = self.get_system_credential(system='google-drive', label=label)
|
|
22
|
+
self.debug = debug
|
|
23
|
+
if self.debug:
|
|
24
|
+
print(f"credentials: {credentials}")
|
|
25
|
+
self.access_token = credentials['auth']['access_token']
|
|
26
|
+
self.brynq_system_id = credentials['id']
|
|
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):
|
|
36
|
+
"""
|
|
37
|
+
Get all files from Google Drive
|
|
38
|
+
"""
|
|
39
|
+
url = f'{self.base_url}files'
|
|
40
|
+
headers = self._get_google_drive_headers()
|
|
41
|
+
response = requests.get(url=url, headers=headers)
|
|
42
|
+
response.raise_for_status()
|
|
43
|
+
return response
|
|
44
|
+
|
|
45
|
+
def upload_file(self, local_file_path: str, remote_file_path: str):
|
|
46
|
+
"""
|
|
47
|
+
This method performs the actual file upload to the formerly derived site + drive.
|
|
48
|
+
local_file_path: local path of the file you want to upload
|
|
49
|
+
remote_file_path: remote path of the folder and filename where you want to place the file
|
|
50
|
+
"""
|
|
51
|
+
credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
|
|
52
|
+
service = build('drive', 'v3', credentials=credentials)
|
|
53
|
+
|
|
54
|
+
file_metadata = {'name': os.path.basename(remote_file_path)}
|
|
55
|
+
media = MediaFileUpload(local_file_path, resumable=True)
|
|
56
|
+
|
|
57
|
+
response = service.files().create(
|
|
58
|
+
body=file_metadata,
|
|
59
|
+
media_body=media,
|
|
60
|
+
fields='id'
|
|
61
|
+
).execute()
|
|
62
|
+
|
|
63
|
+
if self.debug:
|
|
64
|
+
print(f'File ID: {response.get("id")}')
|
|
65
|
+
|
|
66
|
+
return response
|
|
67
|
+
|
|
68
|
+
def download_file(self, file_id: str, mime_type: str, local_file_path: str):
|
|
69
|
+
"""
|
|
70
|
+
This method downloads a file from Google Drive to the local machine.
|
|
71
|
+
file_id: id of the file on Google Drive. Get it with the list_files function
|
|
72
|
+
mime_type: mime type of the file. Get it with the list_files function
|
|
73
|
+
local_file_path: local path where the file will be downloaded to
|
|
74
|
+
"""
|
|
75
|
+
url = f'{self.base_url}files/{file_id}/export?mimeType={mime_type}'
|
|
76
|
+
headers = self._get_google_drive_headers()
|
|
77
|
+
response = requests.get(url=url, headers=headers)
|
|
78
|
+
response.raise_for_status()
|
|
79
|
+
with open(local_file_path, 'wb') as f:
|
|
80
|
+
f.write(response.content)
|
|
81
|
+
return response
|
|
82
|
+
|
|
83
|
+
def download_files(self, local_folder_path: str, remote_folder_path: str):
|
|
84
|
+
"""
|
|
85
|
+
This method downloads all files from a Directory in Google Drive to the local machine.
|
|
86
|
+
local_folder_path: local folder where the files will be downloaded to
|
|
87
|
+
remote_folder_path: remote path of the folder you want to get on Google Drive
|
|
88
|
+
"""
|
|
89
|
+
credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
|
|
90
|
+
service = build('drive', 'v3', credentials=credentials)
|
|
91
|
+
|
|
92
|
+
# List files in the specified remote folder
|
|
93
|
+
query = f"'{remote_folder_path}' in parents"
|
|
94
|
+
results = service.files().list(q=query, fields="files(id, name)").execute()
|
|
95
|
+
items = results.get('files', [])
|
|
96
|
+
|
|
97
|
+
if not items:
|
|
98
|
+
return 'No files found.'
|
|
99
|
+
else:
|
|
100
|
+
for item in items:
|
|
101
|
+
file_id = item['id']
|
|
102
|
+
file_name = item['name']
|
|
103
|
+
request = service.files().get_media(fileId=file_id)
|
|
104
|
+
fh = BytesIO()
|
|
105
|
+
downloader = MediaIoBaseDownload(fh, request)
|
|
106
|
+
done = False
|
|
107
|
+
while done is False:
|
|
108
|
+
status, done = downloader.next_chunk()
|
|
109
|
+
if self.debug:
|
|
110
|
+
print(f"Download {file_name} {int(status.progress() * 100)}%.")
|
|
111
|
+
|
|
112
|
+
# Save the file to the local folder
|
|
113
|
+
local_file_path = os.path.join(local_folder_path, file_name)
|
|
114
|
+
with open(local_file_path, 'wb') as f:
|
|
115
|
+
f.write(fh.getvalue())
|
|
116
|
+
|
|
117
|
+
return "Download complete"
|
|
118
|
+
|
|
119
|
+
def remove_file(self, remote_file_path: str):
|
|
120
|
+
"""
|
|
121
|
+
Remove a file from Google Drive
|
|
122
|
+
remote_file_path: complete path including filename
|
|
123
|
+
:return: response from Google Drive
|
|
124
|
+
"""
|
|
125
|
+
credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
|
|
126
|
+
service = build('drive', 'v3', credentials=credentials)
|
|
127
|
+
|
|
128
|
+
# Find the file ID
|
|
129
|
+
query = f"name = '{remote_file_path}'"
|
|
130
|
+
results = service.files().list(q=query, fields="files(id)").execute()
|
|
131
|
+
items = results.get('files', [])
|
|
132
|
+
|
|
133
|
+
if not items:
|
|
134
|
+
print('No files found.')
|
|
135
|
+
return "No files found."
|
|
136
|
+
else:
|
|
137
|
+
file_id = items[0]['id']
|
|
138
|
+
response = service.files().delete(fileId=file_id).execute()
|
|
139
|
+
if self.debug:
|
|
140
|
+
print(f'File {remote_file_path} deleted.')
|
|
141
|
+
return response
|
|
142
|
+
|
|
143
|
+
def remove_folder(self, folder_id: str):
|
|
144
|
+
"""
|
|
145
|
+
Remove a folder from Google Drive
|
|
146
|
+
folder: folder id that you want to delete
|
|
147
|
+
"""
|
|
148
|
+
credentials = self.refresh_system_credential(system='google-drive', system_id=self.brynq_system_id)
|
|
149
|
+
service = build('drive', 'v3', credentials=credentials)
|
|
150
|
+
|
|
151
|
+
response = service.files().delete(fileId=folder_id).execute()
|
|
152
|
+
if self.debug:
|
|
153
|
+
print(f'Folder {folder_id} deleted.')
|
|
154
|
+
return response
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 1.0
|
|
2
|
+
Name: brynq-sdk-google-drive
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Google Drive wrapper from BrynQ
|
|
5
|
+
Home-page: UNKNOWN
|
|
6
|
+
Author: BrynQ
|
|
7
|
+
Author-email: support@brynq.com
|
|
8
|
+
License: BrynQ License
|
|
9
|
+
Description: Groogle Drive wrapper from BrynQ
|
|
10
|
+
Platform: UNKNOWN
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
setup.py
|
|
2
|
+
brynq_sdk/google_drive/__init__.py
|
|
3
|
+
brynq_sdk/google_drive/google_drive.py
|
|
4
|
+
brynq_sdk_google_drive.egg-info/PKG-INFO
|
|
5
|
+
brynq_sdk_google_drive.egg-info/SOURCES.txt
|
|
6
|
+
brynq_sdk_google_drive.egg-info/dependency_links.txt
|
|
7
|
+
brynq_sdk_google_drive.egg-info/not-zip-safe
|
|
8
|
+
brynq_sdk_google_drive.egg-info/requires.txt
|
|
9
|
+
brynq_sdk_google_drive.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
brynq_sdk
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from setuptools import setup
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
setup(
|
|
5
|
+
name='brynq_sdk_google_drive',
|
|
6
|
+
version='0.1.0',
|
|
7
|
+
description='Google Drive wrapper from BrynQ',
|
|
8
|
+
long_description='Groogle Drive wrapper from BrynQ',
|
|
9
|
+
author='BrynQ',
|
|
10
|
+
author_email='support@brynq.com',
|
|
11
|
+
packages=["brynq_sdk.google_drive"],
|
|
12
|
+
license='BrynQ License',
|
|
13
|
+
install_requires=[
|
|
14
|
+
'brynq-sdk-brynq>=1',
|
|
15
|
+
'google-api-python-client>=2,<3',
|
|
16
|
+
'requests>=2,<=3'
|
|
17
|
+
],
|
|
18
|
+
zip_safe=False,
|
|
19
|
+
)
|