1
0
Fork 0
forked from tobast/sonnetteweb
sonnetteweb/sonnette.py

49 lines
1.1 KiB
Python
Raw Normal View History

""" Sonnetteweb — Enfin une sonnette à l'Arcoloc
Sonnez via le réseau local. C'est cool, hein ?
"""
from datetime import datetime, timedelta
2018-02-06 01:42:11 +01:00
import subprocess
from flask import Flask, render_template, request
app = Flask('sonnetteweb')
class Ringer:
_instance = None
def __init__(self):
self.last_rung = datetime.fromtimestamp(0)
@staticmethod
def get():
if Ringer._instance is None:
Ringer._instance = Ringer()
return Ringer._instance
def ring(self):
if datetime.now() - self.last_rung < timedelta(seconds=5):
return False
self.last_rung = datetime.now()
2018-02-06 01:42:11 +01:00
print('Dring!')
subprocess.run(['bash',
'-c',
'aplay ringtone.wav &'])
return True
@app.route('/', methods=['GET', 'POST'])
def view_home():
context = {}
if request.method == 'POST':
if request.form['ding'] == 'dong':
if Ringer.get().ring():
context['has_rung'] = True
else:
context['too_much_dring'] = True
return render_template('homepage.html', **context)