-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
47 lines (40 loc) · 1.26 KB
/
sync.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
import requests
import json
import sys
from couchdb.client import Document
sync_folders = ["DroidA","DroidB"]
url = 'http://127.0.0.1:5000/file'
def send_sync(file_name,fold):
files = {'upload_file': open(file_name,'rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short','folder':fold}
r = requests.post(url, files=files, data=values)
return files
def receive_sync():
r = requests.post(url+"_get")
x=str(r)
try:
data=json.loads(r.text)
print(data)
except:
sys.exit()
doc_name=data['name'].split("/")[1]
from_folder=data['name'].split("/")[0]
# print(from_folder)
if from_folder == sync_folders[0]:
to_folder=sync_folders[1]
else:
to_folder=sync_folders[0]
print(data)
doc=(data['doc'])
# print(doc.save())
with open(to_folder+"/"+doc_name,'a') as f: # use a instead of w to append existing data
f.write(data['data']) # choose when to delete and when to append
values = {'id':data['id']}
t=requests.post(url+'_remove',data=values)
receive_sync()
def try_sync():
r = requests.post(url+"_get")
print(r.text)
if __name__=='__main__':
receive_sync()
# sync('/home/rohan/github/socialcops-challenge/DroidA/shit')