-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·71 lines (52 loc) · 1.71 KB
/
app.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
import logging
from flask import Flask
from flask import jsonify
from flask import render_template
from mpc import mpc_get_position, mpc_command
from utils import get_stationlist, reload_playlist
logging.basicConfig(filename='pyradio.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
app = Flask('Radio PPJ')
@app.route("/")
def show_home():
return render_template('radio.html')
@app.route("/play", methods=['GET'])
def play_radio():
mpc_command(['mpc', 'play'])
return "ACK"
@app.route("/stop", methods=['GET'])
def stop_radio():
mpc_command(['mpc', 'stop'])
return "ACK"
@app.route("/status", methods=['GET'])
def get_status():
try:
position = mpc_get_position()
status = {
"volume": int(str(mpc_command(['mpc', 'volume'])).split(':')[1].replace('%\\n\'', '').strip()),
"playing": True if mpc_command(['mpc', 'current']) else False,
"radio": position,
"error": ""
}
except Exception as error:
logging.error(f'Cannot retrieve status: {error}')
status = {
"error": str(error),
}
return jsonify(status)
@app.route("/stations", methods=['GET'])
def get_stations():
return jsonify(get_stationlist())
@app.route("/stations/<string:position>", methods=['POST'])
def set_station(position):
mpc_command(['mpc', 'play', position])
return "ACK"
@app.route("/volume/<string:volume>", methods=['POST'])
def set_volume(volume):
print(volume)
mpc_command(['mpc', 'volume', volume])
print(mpc_command(['mpc', 'volume']))
return "ACK"
if __name__ == "__main__":
reload_playlist()
app.run(host='0.0.0.0', port=5000)