-
Notifications
You must be signed in to change notification settings - Fork 6
/
webserver.py
156 lines (141 loc) · 3.83 KB
/
webserver.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
144
145
146
147
148
149
150
151
152
153
154
155
156
import json
import uuid
import logging
from datetime import datetime
import storage
import markdown
from flask import Flask, url_for, request
app = Flask(__name__)
logging.basicConfig(filename='d-fine.log', level=logging.DEBUG)
def parse_request():
word = request.args.get("word", None)
pos = request.args.get("def", None)
return word, pos
@app.route("/")
def main():
with open("static/index.html") as f:
return f.read()
# apis
@app.route("/api/def/find/")
def find():
contains = request.args.get("contains", None)
startswith = request.args.get("startswith", None)
endswith = request.args.get("endswith", None)
data = storage.get_all_def()
if contains:
data = [d for d in data if contains in d]
if startswith:
data = [d for d in data if d.startswith(startswith)]
if endswith:
data = [d for d in data if d.endswith(endswith)]
return json.dumps({
"status": "worked",
"data": data
})
@app.route("/api/def/add/", methods=["POST", "PUT"])
def add_def():
word, pos = parse_request()
data = storage.get_def(word)
if not data:
data = []
d = json.loads(request.data)
if not d or "def" not in d:
return json.dumps({
"status": "error",
"message":"Incorrect or no Data sent"
})
def_data = {
"id": str(uuid.uuid4()),
"def": d["def"].strip(),
"html": markdown.markdown(d["def"], safe_mode='escape'),
"last_touch": datetime.now().isoformat()
}
if pos is None or pos == "":
data.append(def_data)
else:
def_data["id"] = pos
found = False
for d, i in zip(data, range(len(data))):
if d["id"] == pos:
data[i] = def_data
found = True
break
if not found:
return json.dumps({
"status": "error",
"message": "the def '%s' was not found" % pos
})
storage.set_def(word, data)
return json.dumps({"status": "worked"})
@app.route("/api/def/del/", methods=["POST", "PUT"])
def del_def():
word, pos = parse_request()
data = storage.get_def(word)
if not data:
return json.dumps({
"status": "error",
"message":"word is already deleted"
})
for i in range(len(data)):
d = data[i]
if d["id"] == pos:
del data[i]
break
if len(data) == 0:
storage.del_def(word)
else:
storage.set_def(word, data)
return json.dumps({"status": "worked"})
@app.route("/api/def/get/")
def single_word_def():
word, pos = parse_request()
data = storage.get_def(word)
if not data:
logging.debug("LOOK UP FAILED: %s" % word)
storage.failed_lookup(word)
return json.dumps({
"status": "error",
"message": "Coudn't find '%s'" % (word)
})
if not pos:
logging.debug("LOOK UP WORKED: %s" % word)
return json.dumps({
"status": "worked",
"word": word,
"defs": data
});
result = None
for d in data:
if d["id"] == pos:
result = d
break
if not result:
logging.debug("LOOK UP WITH POS FAILED: %s (%s)" % (word, pos))
return json.dumps({
"status": "error",
"message": "Coudn't find '%s' with def '%s'" % (word, pos)
})
logging.debug("LOOK UP WORKED: %s (%s)" % (word, pos))
return json.dumps({
"status": "worked",
"word": word,
"def": result
});
@app.route("/api/failed/get/")
def failed_lookups():
# I could get redis to more of this work
r = storage.get_failed_lookup()
words = [(i[0], int(i[1])) for i in r.items() if i[1] != "1"]
words.sort(key=lambda i: -i[1])
return json.dumps({
"status": "worked",
"words": words
})
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
url_for("static", filename='index.html')
url_for("static", filename='js/jquery.min.js')
url_for("static", filename='js/shodow.js')
url_for("static", filename='js/bootstrap.min.js')
url_for("static", filename='js/ICanHaz.min.js')
url_for("static", filename='css/bootstrap.css')