-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploader.py
More file actions
90 lines (79 loc) · 2.36 KB
/
uploader.py
File metadata and controls
90 lines (79 loc) · 2.36 KB
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
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import ast
import csv
import os.path
import pickle
import time
from secrets.other import FOLDER_ID
# all modified from
# https://github.com/googleapis/google-api-python-client
# https://developers.google.com/docs/api/quickstart/python
class Upload(object):
"""
An Upload instance; upload a video to the Drive folder
specified in secrets.other.FOLDER_ID
"""
def __init__(self,
localPath=None,
baseName=None,
mimeType='video/mp4',
folderAlt=None):
self.localPath = localPath
self.baseName = baseName
self.mimeType = mimeType
if folderAlt:
self.parents = [folderAlt]
else:
self.parents = [FOLDER_ID]
## DRIVE OAUTH SCOPES
# If modifying these scopes, delete the file token.pickle.
# These OAuth scopes are defined here:
# https://developers.google.com/identity/protocols/googlescopes#drivev3
self.SCOPES = [
'https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive',
"https://www.googleapis.com/auth/drive.file"
]
self.g_drive = None
def login(self):
# Do some login stuff
# Return live services for Docs and Drive APIs
creds = None
if os.path.exists('secrets/token.pickle'):
with open('secrets/token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'secrets/credentials.json', self.SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('secrets/token.pickle', 'wb') as token:
pickle.dump(creds, token)
try:
self.g_drive = build('drive','v3',credentials=creds)
except:
print("LOGIN ERROR")
def upload_it(self):
file_metadata = {
'name': self.baseName,
'parents': self.parents
}
media = MediaFileUpload(
self.localPath,
mimetype=self.mimeType,
resumable=True)
print(file_metadata)
uploaded_file = self.g_drive.files().create(
body=file_metadata,
media_body=media,
fields='id',
supportsAllDrives=True
).execute()
print(uploaded_file.get('id'))