-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_to_gdrive.py
131 lines (102 loc) · 4.31 KB
/
upload_to_gdrive.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import argparse
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import inotify.adapters
class RecordingsPublisher:
def __init__(self):
credentials_path = 'local_credentials.txt'
self.__drive = GoogleDrive(self.__authenticate(credentials_path))
def __authenticate(self, creds):
# Snippet from https://stackoverflow.com/questions/24419188/automating-pydrive-verification-process
gauth = GoogleAuth()
gauth.LoadCredentialsFile(creds)
if gauth.credentials is None:
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
gauth.Refresh()
else:
gauth.Authorize()
gauth.SaveCredentialsFile(creds)
return gauth
def upload_file(self, filepath):
def _get_folder_id(file_list, folder_name):
for f in file_list:
if f['title'] == folder_name:
return f['id']
return None
def _get_folder_contents(file_list):
contents = []
for f in file_list:
contents.append(f['title'])
return contents
# Find the id of the recordings folder
root_contents = self.__drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
recordings_folder_id = _get_folder_id(root_contents, 'recordings')
# Get a list of the recordings folder contents
recordings_contents = \
self.__drive.ListFile({'q': "'{}' in parents and trashed=false".format(recordings_folder_id)}).GetList()
recordings_contents = _get_folder_contents(recordings_contents)
# Extract filename from path
filename = os.path.basename(filepath)
if filename in recordings_contents:
print('File already exists in recordings. Aborting upload.')
return
# Check if file exists just before upload
if not os.path.exists(filepath):
print('File does not exist. Aborting upload.')
# Create file metadata and upload
file_meta = {'title': filename, 'parents': [{'kind': 'drive#fileLink', 'id': recordings_folder_id}]}
f = self.__drive.CreateFile(file_meta)
f.SetContentFile(filepath)
f.Upload()
print('Succesfully uploaded file:{}'.format(filename))
def inotify_loop(recordings_path):
watch_folder = recordings_path.encode()
audio_logger_prefix = 'Live-['
file_ext = '.ogg'
rec_pub = RecordingsPublisher()
notifier = inotify.adapters.Inotify()
notifier.add_watch(watch_folder)
try:
for event in notifier.event_gen():
if event is not None:
(header, type_names, watch_path, filename) = event
# Catch creation or rename
# TODO: Probably there is a better way to omit multiple notifications
if 'IN_CREATE' in type_names or 'IN_CLOSE_NOWRITE' in type_names \
and 'IN_ISDIR' not in type_names:
watch_path = watch_path.decode('ascii')
filename = filename.decode('ascii')
if filename.endswith(file_ext) and not filename.startswith(audio_logger_prefix):
path = os.path.join(watch_path, filename)
rec_pub.upload_file(path)
else:
print('Omiting upload for file:{}'.format(filename))
print(event)
finally:
notifier.remove_watch(watch_folder)
def run_once(filepath):
rec_pub = RecordingsPublisher()
rec_pub.upload_file(filepath)
def main():
# initiate the parser
parser = argparse.ArgumentParser()
parser.add_argument("--mode", "-m", help="Set mode. Options: watch, once")
parser.add_argument("--path", "-p", help="Path for file to be uploaded.")
parser.add_argument("--recordings-path", "-r", help="Recordings path.")
# read arguments from the command line
args = parser.parse_args()
# check for --width
if args.mode:
print("Set mode to %s" % args.mode)
if args.mode == "watch":
inotify_loop(args.recordings_path)
elif args.mode == "once":
run_once(args.path)
else:
raise ValueError("Invalid mode.")
if __name__ == "__main__":
main()