-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
151 lines (123 loc) · 3.45 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
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
import os
import signal
import datetime
import json
import time
import sqlite3
import logging
from flask import Flask, request, render_template, redirect
app = Flask(__name__)
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
DB = sqlite3.connect("alarms.db", check_same_thread=False)
@app.route("/")
def index():
weekday_names = {
"mo" : "Monday",
"di" : "Tuesday",
"mi" : "Wednesday",
"do" : "Thursday",
"fr" : "Friday",
"sa" : "Saturday",
"so" : "Sunday"
}
alarms = []
sounds = []
now = datetime.datetime.now()
cursor = DB.cursor()
for rowid, weekdays, hour, minute, sound in cursor.execute("SELECT rowid, weekdays, hour, minute, sound FROM Alarms ORDER BY rowid ASC;"):
weekdays = json.loads(weekdays)
weekdays = ", ".join(map(lambda s: weekday_names[s], filter(lambda x: weekdays[x], weekdays)))
alarms.append({
"value" : f"{weekdays} {hour:02d}:{minute:02d} {sound}",
"id" : rowid
})
for rowid, name in cursor.execute("SELECT rowid, name FROM Sounds ORDER BY rowid ASC;"):
sounds.append({
"value" : name,
"id" : rowid
})
cursor.close()
return render_template(
"index.html",
alarms=alarms,
hour=now.hour,
minute=now.minute,
weekday=now.date().weekday(),
sounds=sounds,
is_ringing=os.path.isfile("mpv.pid")
)
@app.route("/create", methods=["POST"])
def create():
weekdays = {
"mo" : False,
"di" : False,
"mi" : False,
"do" : False,
"fr" : False,
"sa" : False,
"so" : False
}
try:
hour = request.form["hour"]
minute = request.form["minute"]
sound = request.form["sound"]
except KeyError:
return "Bad request", 400
for key in weekdays:
if key in request.form:
weekdays[key] = True
if not any(weekdays.values()):
return "Bad request", 400
try:
hour = int(hour, 10)
minute = int(minute, 10)
except ValueError:
return "Bad request", 400
sound = str(sound)
weekdays = json.dumps(weekdays)
cursor = DB.cursor()
cursor.execute(
"""
SELECT 1 FROM Sounds WHERE name = ?;
""",
(sound,)
)
if cursor.fetchone() is None:
return "Bad request", 400
cursor.execute(
"""
INSERT INTO Alarms (weekdays, hour, minute, sound) VALUES (?, ?, ?, ?);
""",
(weekdays, hour, minute, sound)
)
cursor.close()
DB.commit()
return redirect("/")
@app.route("/delete", methods=["POST"])
def delete():
try:
id = int(request.form["id"], 10)
except (ValueError, KeyError):
return "Bad request", 400
cursor = DB.cursor()
cursor.execute(
"""
DELETE FROM Alarms WHERE rowid = ?;
""",
(id,)
)
cursor.close()
DB.commit()
return redirect("/")
@app.route("/stop", methods=["GET"])
def stop():
if os.path.isfile("mpv.pid"):
pid = None
with open("mpv.pid") as f:
pid = int(f.read(), 10)
os.kill(pid, signal.SIGKILL)
while os.path.isfile("mpv.pid"):
time.sleep(0.2)
return redirect("/")