-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropboxManager.py
More file actions
93 lines (78 loc) · 2.63 KB
/
Copy pathDropboxManager.py
File metadata and controls
93 lines (78 loc) · 2.63 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
91
92
93
# Documentation on Dropbox - Python API
# https://dropbox-sdk-python.readthedocs.io/en/latest/index.html
import sys
import dropbox
from dropbox.files import WriteMode
from dropbox.exceptions import ApiError, AuthError
class DropboxManager:
'''
Class that manages all the interactions with Dropbox
'''
def __init__(self, access_token):
# dropbox object
self.dbx = dropbox.Dropbox(access_token)
# check that the access token is valid
try:
self.dbx.users_get_current_account()
except AuthError:
sys.exit("ERROR: Invalid access token; try re-generating an "
"access token from the app console on the web.")
def upload_file(self, file_item, dbx_item_path):
'''
Upload file on Dropbox
'''
with open(file_item.path, 'rb') as file:
print("[+]", file_item.name)
try:
self.dbx.files_upload(file.read(), dbx_item_path, mode=WriteMode.overwrite)
except ApiError as err:
if err.user_message_text:
print(err.user_message_text)
sys.exit()
else:
print(err)
sys.exit()
def create_directory(self, dbx_item_path):
'''
Create a new directory on Dropbox
'''
print("[+] Creating new directory", dbx_item_path)
try:
self.dbx.files_create_folder(dbx_item_path)
except ApiError as err:
if err.user_message_text:
print(err.user_message_text)
sys.exit()
else:
print(err)
sys.exit()
def clean(self, list_to_delete):
'''
Clean files or directories on Dropbox
'''
for entry in list_to_delete:
# delete the item
print("[-] Deleting", entry)
try:
self.dbx.files_delete(entry)
except ApiError as err:
if err.user_message_text:
print(err.user_message_text)
sys.exit()
else:
print(err)
sys.exit()
def check_directory_exists(self, dbx_item_path):
'''
Check if a directory exists on Dropbox
'''
# check if the directory exists
try:
self.dbx.files_list_folder(dbx_item_path)
return True
except ApiError:
# if the directory does not exist, say it
print("Directory does not exist yet")
return False
if __name__ == "__main__":
pass