-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileUpload.py
49 lines (43 loc) · 1.61 KB
/
fileUpload.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
# The following script uses watchdog which helps in firing an event whenever
# a directory undergoes any modification. Whenever a new file/directory is created
# we push the change onto dropbox which will help us in synchronizing contents across
# various systems.
# This script has to be run in the background as a daemon.
try:
import os
import sys
import dropbox
from dropbox.client import DropboxClient
from config import ACCESS_TOKEN,LOCAL_DIRECTORY_WATCH,DROPBOX_SYNC_LOCATION
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
except Exception as e:
print e
client = dropbox.Dropbox(ACCESS_TOKEN)
class MyHandler(FileSystemEventHandler):
#event which gets fired whenever our specifies directory undergoes any change
#specify the directory name to be watched in the config.py file
def on_modified(self,event):
for root, dirs, files in os.walk(LOCAL_DIRECTORY_WATCH):
for filename in files:
local_path = os.path.join(root, filename)
DROP_BOX_PATH=local_path.replace("/home","")
DROP_BOX_PATH=DROPBOX_SYNC_LOCATION+DROP_BOX_PATH
try:
with open(local_path, 'rb') as f:
data=f.read()
client.files_upload(data,DROP_BOX_PATH,mode=dropbox.files.WriteMode.overwrite)
except Exception as e:
print e
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path=LOCAL_DIRECTORY_WATCH, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()