forked from browserengineering/book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
211 lines (186 loc) · 5.9 KB
/
api.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python3
import bottle
import json
import os, sys
import time
import difflib
import html
import hashlib
bottle.TEMPLATE_PATH.append(".")
class Data:
def __init__(self, filename):
self.filename = filename
if os.path.exists(filename):
with open(filename, "r") as f:
self.data = json.load(f)
else:
self.data = []
def save(self):
with open(self.filename, "w") as f:
json.dump(self.data, f)
def safe_tag(self, tag):
if tag.lower() in [ "p", "li", "pre", "span" ]:
return tag
else:
return "p"
def typo(self, url, old, new, name, tag="p"):
if any(obj['type'] == 'typo' and
obj['url'] == url and
obj['old'] == old and
obj['new'] == new for obj in self.data):
return
self.data.append({
'id': len(self.data),
'time': time.time(),
'type': 'typo',
'tag': self.safe_tag(tag),
'url': url,
'old': old,
'new': new,
'name': name,
'status': 'new',
})
self.save()
def text_comment(self, url, text, comment, name, tag="p"):
if any(obj['type'] == 'comment' and
obj['url'] == url and
obj['text'] == text and
obj['comment'] == comment for obj in self.data):
return
self.data.append({
'id': len(self.data),
'time': time.time(),
'type': 'comment',
'tag': self.safe_tag(tag),
'url': url,
'text': text,
'comment': comment,
'name': name,
'status': 'new',
})
self.save()
def chapter_comment(self, url, comment, name, email):
if any(obj['type'] == 'chapter_comment' and
obj['url'] == url and
obj['comment'] == comment for obj in self.data):
return
self.data.append({
'id': len(self.data),
'time': time.time(),
'type': 'chapter_comment',
'url': url,
'comment': comment,
'name': name,
'email': email,
'status': 'new',
})
self.save()
def status(self, i):
return self.data[i]["status"]
def set_status(self, i, status):
self.data[i]['status'] = status
self.save()
def __iter__(self):
return iter(self.data)
DATA = Data("db.json")
@bottle.post("/api/typo")
def typo():
data = json.load(bottle.request.body)
DATA.typo(**data)
@bottle.post("/api/text_comment")
def text_comment():
data = json.load(bottle.request.body)
DATA.text_comment(**data)
@bottle.post("/api/chapter_comment")
def comment():
data = json.load(bottle.request.body)
DATA.chapter_comment(**data)
def splitword(text):
out = [[]]
ws = text[0].isspace()
for c in text:
if c.isspace() == ws:
out[-1].append(c)
else:
out.append([c])
ws = c.isspace()
return ["".join(s) for s in out]
def prettify(obj):
if obj['type'] != 'typo': return obj
old, new = obj['old'], obj['new']
old_words = [w + "\n" for w in splitword(old)]
new_words = [w + "\n" for w in splitword(new)]
d = difflib.Differ()
tag = obj.get("tag", "p")
results = []
state = " "
for out in d.compare(old_words, new_words):
new_state = out[0]
word = out[2:-1]
if new_state == "?": continue
if state != " ":
results.append("</span>")
if new_state == "+":
results.append("<span class='add'>")
if new_state == "-":
results.append("<span class='del'>")
results.append(html.escape(word))
state = new_state
if state != " ":
results.append("</span>")
obj = obj.copy()
otag = "<ul><li>" if tag == 'li' else ("<" + tag + ">")
ctag = "</li></ul>" if tag == 'li' else ("</" + tag + ">")
obj['diff'] = otag + "".join(results) + ctag
return obj
@bottle.route("/feedback")
@bottle.view("feedback.view")
def feedback():
new = [prettify(o) for o in DATA if o['status'] == "new"]
saved = {}
for o in DATA:
if o['status'] == "saved":
page = os.path.split(o['url'])[1].rsplit(".", 1)[0]
saved.setdefault(page, []).append(prettify(o))
return { 'new': new, 'saved': saved }
@bottle.route("/feedback.rss")
@bottle.view("feedback_rss.view")
def feedback():
bottle.response.content_type = 'application/rss+xml'
new = [prettify(o) for o in DATA if o['status'] == "new"]
return { 'new': new }
@bottle.route("/api/status", method=["POST", "OPTIONS"])
def status():
data = json.load(bottle.request.body)
pw = data.get('pw', "")
heng = hashlib.sha3_256()
heng.update(pw.encode("utf8"))
with open("pw.hash", "rb") as f:
good = f.read(256)
# Equivalent to `good == heng.digest()` but constant-time-ish
if sum([0 if a == b else 1 for a, b in zip(good, heng.digest())]) == 0:
DATA.set_status(data['id'], data['status'])
else:
raise ValueError("Invalid password")
@bottle.route('/<file>')
def static(file):
return bottle.static_file(file, root=".")
@bottle.route('/')
def index():
return bottle.static_file("index.html", root=".")
@bottle.route("/auth/tools")
def tools():
bottle.response.set_cookie("tools", "")
return "Editing tools enabled"
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "pw":
import getpass
pw = getpass.getpass("Password: ")
heng = hashlib.sha3_256()
heng.update(pw.encode("utf8"))
with open("pw.hash", "wb") as f:
f.write(heng.digest())
print("Please run: window.localStorage['pw'] = '" + pw + "'");
else:
debug = "--debug" in sys.argv
bottle.run(port=4000, debug=debug, reloader=True)