-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
108 lines (86 loc) · 3.02 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
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
from flask import Flask, render_template, redirect, url_for, Response, jsonify, request
from flask_restful import Resource, Api
from flask_socketio import SocketIO, emit, send
import subprocess
import spotifyinfo
import re
app = Flask(__name__)
spotifyinfo.tokenreauth()
api = Api(app)
socketio = SocketIO(app)
###### Flask ######
@app.route('/')
def index():
res = getspotify_np()
return render_template('index.html', res=res)
###### WebSockets ######
@socketio.on('connect')
def connect():
print('Client connect!')
@socketio.on('connect', namespace='/devices')
def connectDevices():
print('Client connected to /devices')
emit('message', {'data': "Hello from server to device!"}, namespace='/devices')
emit('connected')
@socketio.on('test', namespace='/devices')
def test(msg):
emit('message', {'data': msg}, namespace='/devices', broadcast=True)
@socketio.on('disconnect', namespace='/devices')
def device_disconnect():
print('Client disconnected from devices')
@socketio.on('disconnect')
def disconnect():
print('Client disconnected')
@socketio.on('get_nowplaying', namespace='/devices')
def get_nowplaying():
np_data = getspotify_np()
emit('nowplaying', {'data': np_data}, namespace='/devices')
###### API ######
# Change this when major API version rewrites occur!
# (Although support for older versions is recommended to be kept live too)
version = 1
class Refresh(Resource):
def get(self):
return {'message': 'refreshing devices!'}
def put(self):
data = request.form['data']
if (data == "test"):
try:
socketio.emit('refresh', namespace='/devices', broadcast=True)
socketio.disconnect()
return {'message': 'success!'}
except:
return {'message': 'failure!'}
else:
return {'message': 'invalid'}
api.add_resource(Refresh, '/api/' + 'v' + str(version) + '/refresh')
class Info(Resource):
def get(self):
return getspotify_np()
api.add_resource(Info, '/api/' + 'v' + str(version) + '/info')
###### Functions ######
def getspotify_np():
res = {"artist": "N/A", "track": "N/A", "img": "static/img/spotify_connect.png", "uri": "https://open.spotify.com"}
try:
infile = open('../auth_token', 'r')
except:
infile2 = open('../auth_token', 'w')
infile2.close()
spotifyinfo.tokenreauth()
infile = open('../auth_token', 'r')
try:
res = spotifyinfo.info(spotifyinfo.tokeneval(infile.readline().strip()), '/home/spotbot/librespot.log')
except:
infile.close()
spotifyinfo.tokenreauth()
infile = open('../auth_token', 'r')
res = spotifyinfo.info(spotifyinfo.tokeneval(infile.readline().strip()), '/home/spotbot/librespot.log')
finally:
infile.close()
return res
###### Error Handling ######
@app.errorhandler(500)
def servererror(error):
return redirect(url_for('index'))
if __name__ == '__main__':
socketio.run(app, debug=False, host='0.0.0.0', port=3000)