-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_server.py
317 lines (202 loc) · 9.78 KB
/
config_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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from flask import Flask, render_template, request,abort,jsonify, send_file, send_from_directory, flash, redirect
from werkzeug.utils import secure_filename
from os import path
from util.res import *
from json import dumps, loads
import keyring
from util.res import *
from datetime import datetime
UPLOAD_FOLDER = path.dirname(__file__)+'/skills_modules/'
ALLOWED_EXTENSIONS = {'py'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config["TEMPLATES_AUTO_RELOAD"] = True
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def config_page():
return render_template("config_page.html")
@app.route("/action/<page>",methods=['GET', 'POST'])
def action(page):
if page == "[MANAGE BLUE SKILLS]":
with open("config/skills.blue","r") as f:
modules = []
for line in f.read().splitlines():
modules.append(line.split(":")[0])
sentences = line.split(":")[1]
return render_template("manager_modules.html",modules=modules)
elif page == "[ADD CREDS]":
#display the page
if request.method == "GET":
return render_template("add_account.html",services= get_registered_services())
#creation of a new account
else:
try:
ac_service = request.form['service']
ac_username = request.form['username']
ac_password = request.form['password']
keyring.set_password(ac_service,ac_username,ac_password)
with open("config/accounts.blue","a") as f:
f.write(dumps({"service":ac_service,"username":ac_username})+"\n")
f.close()
return render_template("success_message.html")
except:
return jsonify({"error": "malformed post request"})
elif page == "[MANAGE CREDS]":
return render_template("manage_accounts.html")
elif page == "[ADD WEBSITE VOICE COMMAND]":
return render_template("add_custom_website.html")
elif page == "[ADD CUSTOM VOICE COMMAND TO SEND TO A SERVER]":
return render_template("add_custom_server.html")
elif page == "[ADD RSS FEED]":
return render_template("add_rss_feed.html")
elif page == "[ADD BLUE SKILL]":
return render_template("add_skill.html")
elif page == "[MANAGE RSS FEED]":
with open("config/custom_rss_feed.blue","r") as f:
feeds = []
for line in f.read().splitlines():
try:
feeds.append(loads(line))
except Exception as e:
print(e)
f.close()
return render_template("manage_rss_feed.html",feeds=feeds,lenght=len(feeds))
elif page == "[MANAGE WEBSITE VOICE COMMAND]":
array = get_custom_websites_voice_commands()
return render_template("manage_custom_website.html",lenght=len(array),array=array)
elif page == "[ADD REMINDER]":
if request.method == "GET":
return render_template("add_reminder.html")
else:
type = request.form['type']
if type == "reminder":
#this is a date based reminder
f_time = str(request.form['date']) + " " +str(request.form['time'])
f_time = datetime.strptime(datetime.strptime(f_time, "%Y-%m-%d %H:%M").strftime("%d/%m/%Y %H:%M"),"%d/%m/%Y %H:%M")
add_reminder(f_time, request.form['time'],request.form["content"])
return render_template("success_message.html")
elif type == "wakeup":
#this is a wake-up alarm
f_time = str(request.form['date']) + " " +str(request.form['time'])
f_time = datetime.strptime(datetime.strptime(f_time, "%Y-%m-%d %H:%M").strftime("%d/%m/%Y %H:%M"),"%d/%m/%Y %H:%M")
add_wake_up_alarm(f_time,request.form['time'],request.form["wakeup_music"])
return render_template("success_message.html")
elif page == "[MANAGE REMINDERS]":
return render_template("manage_reminders.html",reminders = get_all_reminders())
else:
return abort(404)
@app.route("/process/<process_id>",methods=['GET','POST'])
def process(process_id):
if process_id == "[DELETE WEBSITE]":
website_id = int(request.args.get("website_id"))
with open("config/custom_websites.blue","r") as f:
registered_websites = f.read().splitlines()
f.close()
#remove desired website from list
registered_websites.pop(website_id)
with open("config/custom_websites.blue","w") as f:
f.writelines(registered_websites)
f.close()
return render_template("success_message.html")
if process_id == "[ADD BLUE SKILL]":
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(app.config['UPLOAD_FOLDER']+ filename)
with open("/skills/skills.blue","a") as f:
f.write(path.extsep(filename)[0]+":"+request.form['voice_command'])
return render_template("success_message.html")
elif process_id == "[DELETE ACCOUNT]":
ac_service = request.args.get("service")
username = keyring.get_credential(ac_service)
password = keyring.get_password(ac_service,username)
keyring.delete_password(ac_service,username,password)
return render_template("success_message.html")
elif process_id == "[ADD RSS FEED]":
voice_command = str(request.form['command']).lower()
url = request.form['url']
with open("config/custom_rss_feed.blue","a") as f:
f.write(dumps({"url" : url,"command" : voice_command}))
f.close()
return render_template("success_message.html")
elif process_id == "[ADD WEBSITE VOICE COMMAND]":
voice_command = str(request.form['command']).lower()
url = request.form['url']
with open("config/custom_websites.blue","a") as f:
f.write(dumps({"url":url,"voice_command":voice_command}))
f.close()
return render_template("success_message.html")
elif process_id == "[ADD CUSTOM VOICE COMMAND TO SEND TO A SERVER]":
ip = request.form['ip_addr']
port = request.form['port']
voice_command = str(request.form['command']).lower()
message = str(request.form['msg']).replace("\n","[NL]")
with open("config/custom_servers.blue","a") as f:
f.write(f"{voice_command}\n{ip}\n{port}\n{message}\n")
f.close()
return render_template("success_message.html")
elif "[MANAGE WEBSITE VOICE COMMAND]" in process_id:
name = process_id.replace("[MANAGE WEBSITE VOICE COMMAND]","")
with open("config/custom_websites.blue","r") as f:
tt = f.read().split("\n")
print(tt)
for ele in tt:
if ele == name:
tt.pop(tt.index(name))
tt.pop(tt.index(name)+1)
with open("config/custom_websites.blue","w") as f:
for ele in tt:
f.write(str(ele) + "\n")
f.close()
return render_template("success_message.html")
elif "[DELETE RSS FEED]" in process_id:
id = request.args.get("feed_id")
with open("config/custom_rss_feed.blue","r") as f:
feeds = f.read().split("\n")
feeds.pop(int(id))
with open("config/custom_rss_feed.blue","w") as f:
for feed in feeds:
f.write(str(feed) + "\n")
f.close()
return render_template("success_message.html")
elif process_id == "[MANAGE CUSTOM VOICE COMMAND TO SEND TO A SERVER]'":
return render_template("success_message.html")
elif process_id == "[DELETE_REMINDER]":
id = int(request.args.get("id"))
reminders = get_all_reminders()
reminders.pop(id)
rewrite_reminders_file(reminders)
return redirect(request.base_url)
elif process_id == "[DELETE_IMAGE]":
img = request.args.get("img",default=False)
if img and (img in listdir("images/")):
remove(f"images/{img}")
return jsonify({"Success":"Image removed from disk."})
else:
return jsonify({"Error":"Invalid image name."})
else:
return render_template("config_page.html")
@app.route("/images/<image>")
def images(image):
images = listdir("images")
if (not image in images) and (image != "all"):
return abort(404)
elif image == "all":
return render_template("images.html",images=images)
else:
return send_file(f"images/{image}")
@app.errorhandler(404)
def page_not_found(error):
return "Tu t'es perdu je crois, fréro ya rien ici.", 404
def start_webserver():
app.run(host="0.0.0.0",port="8103",debug=False)