Refactored code for TRUE multithreaded performance startup time decreased from 2 secs to 0.2 secs updates song time decreased from 3 secs to 0.1 secs introduced proper and full error handler, can fully recover from errors (including performing a full restart) implemented web server to push updates and restart application (flask) getting lyrics is truely mulithreaded now, all in memory.
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from flask import Flask, Blueprint, render_template, redirect, url_for, request, flash
|
|
from werkzeug.utils import secure_filename
|
|
import os
|
|
import signal
|
|
import psutil
|
|
|
|
app = Flask(__name__)
|
|
app.config['SECRET_KEY'] = 'spotify-gui'
|
|
app.config['UPLOAD_FOLDER'] = 'update'
|
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
def update():
|
|
if request.method == 'POST':
|
|
if 'file' not in request.files:
|
|
flash('No file part')
|
|
return redirect(request.url)
|
|
file = request.files['file']
|
|
if file.filename == '':
|
|
flash('No selected file')
|
|
return redirect(request.url)
|
|
if file and file.filename.endswith('.zip'):
|
|
file.save('web/update/update.zip')
|
|
return redirect(request.url)
|
|
return '''
|
|
<!doctype html>
|
|
<title>Upload new File</title>
|
|
<h1>Upload new File</h1>
|
|
<form method=post enctype=multipart/form-data>
|
|
<input type=file name=file>
|
|
<input type=submit value=Upload>
|
|
</form><br>
|
|
<form method="POST" action="/restart">
|
|
<button>Restart</button>
|
|
</form>
|
|
'''
|
|
|
|
@app.route('/restart', methods=['POST'])
|
|
def restart():
|
|
for line in os.popen("ps ax | grep " + "spotifycontroller.py" + " | grep -v grep"):
|
|
fields = line.split()
|
|
pid = fields[0]
|
|
os.kill(int(pid), signal.SIGKILL)
|
|
exit()
|
|
return redirect(url_for('update'))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host='0.0.0.0', port=80) |