-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDropBox.py
56 lines (44 loc) · 1.86 KB
/
DropBox.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
#https://stackoverflow.com/questions/23894221/upload-file-to-my-dropbox-from-python-script
#must install dropbox modules
#pip install dropbox
import sys
import pathlib
import dropbox
import re
import datetime
from Email import Send_Mail
import ConfigValues
def UploadToDropBox(filename):
try:
#the source file
folder = pathlib.Path(".") # located in this folder
#filename = "img.jpg" # file name
filepath = folder / filename # path object, defining the file
#target location in Dropbox
print("/Detection/" + str(datetime.datetime.today().strftime('%d-%m-%Y-%H-%M-%S')))
print(filename)
newfilename = "/Detection/" + str(datetime.datetime.today().strftime('%d-%m-%Y-%H-%M-%S')) + filename
targetfile = newfilename
print("Begin uploading " + targetfile + " to DropBox")
#Create a dropbox object using an API v2 key
d = dropbox.Dropbox(ConfigValues.ReturnDropBoxAPIKey())
# open the file and upload it
with filepath.open("rb") as f:
# upload gives you metadata about the file
# we want to overwite any previous version of the file
meta = d.files_upload(f.read(), targetfile, mode=dropbox.files.WriteMode("overwrite"))
# create a shared link
link = d.sharing_create_shared_link(targetfile)
# url which can be shared
url = link.url
# link which directly downloads by replacing ?dl=0 with ?dl=1
dl_url = re.sub(r"\?dl\=0", "?raw=1", url)
#print (dl_url)
#Check for error
if dl_url[0:5] != "https":
print("Error")
Send_Mail(filename, "RT_OD - DropBox Error:", "")
return dl_url
except:
print("Oops!, DropBox Error: ", sys.exc_info()[0], "occurred.")
Send_Mail(filename, "RT_OD - DropBox Error:", "")