-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
74 lines (57 loc) · 1.54 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
from flask import (
Flask,
json,
redirect,
render_template,
url_for,
)
from tinydb import TinyDB, Query
# Configurables
DB_FILENAME = '.db'
app = Flask(__name__)
@app.route('/')
def main():
return render_template('list.html')
@app.route('/api')
def api():
db = TinyDB(DB_FILENAME)
results = db.search((Query().hidden == False) | (Query().starred == True))
db.close()
for result in results:
result['eid'] = result.eid
result['url'] = url_for('goto', eid=result.eid)
results.sort(key=lambda r: r['created_at'])
return json.dumps(results)
@app.route('/goto/<int:eid>')
def goto(eid):
db = TinyDB(DB_FILENAME)
result = db.get(eid=eid)
db.update({'seen': True}, eids=[eid])
return redirect(result['url'], code=302)
@app.route('/star/<int:eid>', methods=['POST'])
def star(eid):
db = TinyDB(DB_FILENAME)
result = db.get(eid=eid)
db.update({'starred': not result['starred']}, eids=[eid])
db.close()
return 'OK'
@app.route('/hide/all', methods=['POST'])
def hide_all():
db = TinyDB(DB_FILENAME)
eids = [
r.eid for r in db.search(
(Query().hidden == False) & (Query().starred == False)
)
]
db.update({'hidden': True}, eids=eids)
db.close()
return 'OK'
@app.route('/hide/<int:eid>', methods=['POST'])
def hide(eid):
db = TinyDB(DB_FILENAME)
result = db.get(eid=eid)
db.update({'hidden': not result['hidden']}, eids=[eid])
db.close()
return 'OK'
if __name__ == '__main__':
app.run(debug=True)