-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydrive_util.py
60 lines (45 loc) · 1.49 KB
/
pydrive_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import threading
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LoadCredentialsFile("cred.txt")
if gauth.credentials is None:
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
gauth.Refresh()
else:
gauth.Authorize()
gauth.SaveCredentialsFile("cred.txt")
os.chdir('static')
drive = GoogleDrive(gauth)
http = drive.auth.Get_Http_Object()
lock = threading.Semaphore(value=1)
def create_folder():
file = drive.CreateFile({'title': 'DriveWrapper',
"mimeType": "application/vnd.google-apps.folder"})
file.Upload(param={'http':http})
file.InsertPermission({
'type': 'anyone',
'value': 'anyone',
'role': 'reader' })
folder_id = file['id']
return file['id']
def upload_file( filetitle, format ):
lock.acquire()
file = drive.CreateFile({ 'parents': [{'id': os.getenv('FOLDER_ID')}] })
file.SetContentFile(f'{filetitle}.{format}')
file.Upload(param={'http':http})
lock.release()
return file['id']
def download_file(id):
file = drive.CreateFile({'id':id})
file.GetContentFile(file['title'])
return file['title']
def delete_file( id ):
file = drive.CreateFile({'id': id})
file.Delete()
def list_file( parents ):
file_list = drive.ListFile({'q': f"'{parents}' in parents"}).GetList()
for file1 in file_list:
print( 'title: {}, id: {}'.format(file1['title'], file1['id']) )