-
Notifications
You must be signed in to change notification settings - Fork 53
/
upload.py
98 lines (82 loc) · 3.32 KB
/
upload.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
import argparse
import json
import os
import os.path as path
import re
from creds import Creds
from plugins import TEXT
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
FOLDER_MIME_TYPE = 'application/vnd.google-apps.folder'
drive: GoogleDrive
http = None
initial_folder = None
def upload(filename: str, update, context, parent_folder: str = None) -> None:
FOLDER_MIME_TYPE = 'application/vnd.google-apps.folder'
drive: GoogleDrive
http = None
initial_folder = None
gauth: drive.GoogleAuth = GoogleAuth()
ID = update.message.from_user.id
ID = str(ID)
gauth.LoadCredentialsFile(
path.join(path.dirname(path.abspath(__file__)), ID))
if gauth.credentials is None:
print("not Auth Users")
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
gauth.SaveCredentialsFile(
path.join(path.dirname(path.abspath(__file__)), ID))
else:
# Initialize the saved creds
gauth.Authorize()
drive = GoogleDrive(gauth)
http = drive.auth.Get_Http_Object()
if not path.exists(filename):
print(f"Specified filename {filename} does not exist!")
return
# print(filename)
if not Creds.TEAMDRIVE_FOLDER_ID :
if parent_folder:
# Check the files and folers in the root foled
file_list = drive.ListFile(
{'q': "'root' in parents and trashed=false"}).GetList()
for file_folder in file_list:
if file_folder['title'] == parent_folder:
# Get the matching folder id
folderid = file_folder['id']
# print(folderid)
print("Folder Already Exist !! Trying To Upload")
# We need to leave this if it's done
break
else:
# Create folder
folder_metadata = {'title': parent_folder,
'mimeType': 'application/vnd.google-apps.folder'}
folder = drive.CreateFile(folder_metadata)
folder.Upload()
folderid = folder['id']
# Get folder info and print to screen
foldertitle = folder['title']
# folderid = folder['id']
print('title: %s, id: %s' % (foldertitle, folderid))
file_params = {'title': filename.split('/')[-1]}
if Creds.TEAMDRIVE_FOLDER_ID :
file_params['parents'] = [{"kind": "drive#fileLink", "teamDriveId": Creds.TEAMDRIVE_ID, "id":Creds.TEAMDRIVE_FOLDER_ID}]
else:
if parent_folder:
file_params['parents'] = [{"kind": "drive#fileLink", "id": folderid}]
file_to_upload = drive.CreateFile(file_params)
file_to_upload.SetContentFile(filename)
try:
file_to_upload.Upload(param={"supportsTeamDrives" : True , "http": http})
except Exception as e:
print("upload",e)
if not Creds.TEAMDRIVE_FOLDER_ID:
file_to_upload.FetchMetadata()
file_to_upload.InsertPermission({
'type': 'anyone', 'value': 'anyone', 'role': 'reader', 'withLink': True
})
return file_to_upload['webContentLink']