-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
143 lines (116 loc) · 4.25 KB
/
server.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
from flask import Flask
from flask import request
from flask import abort, url_for
#from flask_sqlalchemy import SQLAlchemy
#from sqlalchemy import and_
from settings import DATABASE_URI, DATA_DIR, SERVER_PORT, DEBUG
import os
import json
import random
import time
import string
import requests
#from models import *
from weboftrust import (load_data, calc_paths_and_ranks, get_capacity,
derive_capacities, calculate_score, calculate_score_for_all,
update_scores_from_one_trust)
app = Flask(__name__)
#app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI
#app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#db = SQLAlchemy(app)
# start time
start_time = time.time()
stored = 0
trusts, G = load_data("testdata/test01.csv")
ownidentity = "0"
paths, ranks = calc_paths_and_ranks(G, trusts, ownidentity)
capacities = derive_capacities(ranks)
scores = calculate_score_for_all(G, paths, capacities, ownidentity)
@app.route('/')
def get_home():
'''Home endpoint'''
home_obj = [{"name": "wot-server",
"description": "Server to support moderation via web-of-trust. Visit github.com/weex/wot-server for more info."
}
]
body = json.dumps(home_obj, indent=2)
return (body, 200, {'Content-length': len(body),
'Content-type': 'application/json',
}
)
@app.route('/status')
def get_status():
'''Return general info about server instance. '''
uptime = str(int(time.time() - start_time))
st = os.statvfs(DATA_DIR)
free = st.f_bavail * st.f_frsize
body = json.dumps({'uptime': uptime,
'free': str(free/1024/1024) + " Mb",
}, indent=2
)
return (body, 200, {'Content-length': len(body),
'Content-type': 'application/json',
}
)
#rate
@app.route('/trust', methods=['POST'])
def post_trust():
'''Store a rating pair.'''
try:
body = request.data.decode('utf-8')
in_obj = json.loads(body)
source = in_obj['source'] # source user's id
target = in_obj['target'] # target user's id
value = in_obj['value'] # float value for rating
except:
body = '{"message": "JSON Decode failed"}'
return (body, 400, {'Content-length': len(body),
'Content-Type':'text/plain'})
global scores
scores = update_scores_from_one_trust(G, trusts, paths, ranks, capacities, scores, ownidentity, source, target, value)
#trust = db.session.query(Trust).filter(and_(Trust.user_id == s, Trust.user_id2 == t)).first()
#if trust is None:
# trust = Trust(s, t, v)
# db.session.add(trust)
# db.session.commit()
#else:
# trust.value = v
# db.session.commit()
return ('', 200, {'Content-length': 0,
'Content-type': 'application/json',
}
)
@app.route('/score')
def get_score():
'''Get trusted status of target user from source's perspective.'''
try:
source = str(request.args.get('source')) # ignored for now, assumed to be 0
target = str(request.args.get('target'))
except:
body = '{"message": "Missing one or more required parameters."}'
return (body, 400, {'Content-length': len(body),
'Content-Type':'text/plain'})
#trusts = Trust.query.filter_by(user_id=source).all()
global scores
body = str(target in scores and scores[target] >= 0 )
return (body, 200, {'Content-length': len(body),
'Content-type': 'application/json',
}
)
@app.route('/debug')
def get_debug():
'''Get full state for debugging purposes.'''
out = [{"paths": paths,
"scores": scores
}]
body = json.dumps(out)
return (body, 200, {'Content-length': len(body),
'Content-type': 'application/json',
}
)
if __name__ == '__main__':
if DEBUG:
app.debug = True
app.run(host='0.0.0.0', port=(os.environ.get('SERVER_PORT', SERVER_PORT)))
#app.run(host='127.0.0.1', port=SERVER_PORT)