-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.py
More file actions
68 lines (58 loc) · 2.57 KB
/
Copy pathclient.py
File metadata and controls
68 lines (58 loc) · 2.57 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
import ipfshttpclient
from src.config import SUPERSET_THRESHOLD
from src.utils import *
DOWNLOAD_FOLDER = './objects'
class Client:
def __init__(self, addr, server):
self.ipfs = ipfshttpclient.connect(addr)
self.id = self.ipfs.id()['ID']
self.server = server
log(self.id, 'CONNECTION', '{}'.format(addr))
def add_obj(self, path, keyword):
obj_hash = self.ipfs.add(path)['Hash']
if request(create_binary_id(self.server), INSERT, {'keyword': str(keyword), 'obj': obj_hash, 'hop': str(0)}).text == 'success':
res = 'REFERENCE ({},{}) ADDED'.format(keyword, obj_hash)
log(self.id, INSERT[1:], res)
else:
res = 'REFERENCE ({},{}) ALREADY EXIST'.format(keyword, obj_hash)
log(self.id, INSERT[1:], res)
return res
def remove_obj(self, obj_hash, keyword):
if request(create_binary_id(self.server), REMOVE, {'keyword': str(keyword), 'obj': obj_hash}).text == 'success':
res = 'REFERENCE ({},{}) REMOVED'.format(keyword, obj_hash)
log(self.id, REMOVE[1:], res)
else:
res = 'REFERENCE ({},{}) NOT EXIST'.format(keyword, obj_hash)
log(self.id, REMOVE[1:], res)
return res
def get_obj(self, obj):
try:
self.ipfs.get(obj, target=DOWNLOAD_FOLDER)
res = "OBJECT '{}' DOWNLOADED".format(obj)
log(self.id, 'GET', res)
except Exception:
res = "OBJECT '{}' NOT FOUND".format(obj)
log(self.id, 'GET', res)
return res
def pin_search(self, keyword, threshold=-1):
if threshold == -1:
res = get_response(request(create_binary_id(self.server), PIN_SEARCH, {'keyword': str(keyword)}).text)
else:
res = get_response(request(create_binary_id(self.server), PIN_SEARCH, {'keyword': str(keyword), 'threshold': threshold}).text)
if len(res) > 0:
log(self.id, PIN_SEARCH[1:], '{}'.format(res))
else:
res = []
log(self.id, PIN_SEARCH[1:], 'NO RESULTS FOUND')
return res
def superset_search(self, keyword, threshold=SUPERSET_THRESHOLD):
res = get_response(request(create_binary_id(self.server), SUPERSET_SEARCH, {'keyword': str(keyword), 'threshold': threshold, 'sender': 'user'}).text)
if len(res) > 0:
log(self.id, SUPERSET_SEARCH[1:], '{}'.format(res))
else:
res = []
log(self.id, SUPERSET_SEARCH[1:], 'NO RESULTS FOUND')
return res
def close(self):
self.ipfs.close()
return