-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
305 lines (257 loc) · 9.69 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
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
from flask import Flask, render_template, send_from_directory, jsonify, request, send_file, abort
from flask_socketio import SocketIO, emit
import logging
import time
from music_downloader import maindownload
import os
import json
import random
import asyncio
import urllib.parse
import base64
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins="*",
logger=True, engineio_logger=True)
global current_timestamp, is_playing, queue, current_song_index, last_update_time
current_timestamp = 0
is_playing = False
queue = []
current_song_index = 0
last_update_time = time.time()
logging.basicConfig(level=logging.DEBUG)
LIBRARY_FOLDER = 'library'
LIBDATA_FILE = 'libdata.json'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/music/<encoded_path>')
def music(encoded_path):
try:
decoded_path = base64.urlsafe_b64decode(encoded_path.encode('utf-8')).decode('utf-8')
directory = os.path.dirname(decoded_path)
file_name = os.path.basename(decoded_path)
logging.debug(f"Sending music file: {file_name} from {directory}")
return send_from_directory(directory, file_name, as_attachment=True)
except Exception as e:
logging.error(f"Error decoding path: {e}")
abort(404)
@app.route('/clear_library')
def clear_library():
global queue
os.remove(LIBDATA_FILE)
file_list = os.listdir(LIBRARY_FOLDER)
for file_name in file_list:
file_path = os.path.join(LIBRARY_FOLDER, file_name)
os.remove(file_path)
queue = []
return jsonify({'status': 'success'})
@app.route('/library')
def get_library():
global queue
return jsonify(queue)
@app.route('/add_song', methods=['POST'])
def add_song():
url = request.json['songurl']
logging.debug(f"Adding song from URL: {url}")
asyncio.run(maindownload(url))
time.sleep(5)
return jsonify({'status': 'success'})
@app.route('/update_queue')
def update_queue():
global queue
with open(LIBDATA_FILE, 'r', encoding='utf-8') as file:
libdata = json.load(file)
existing_urls = {song['url'] for song in queue}
new_songs = [song for song in libdata if song['url'] not in existing_urls]
queue.extend(new_songs)
return jsonify({'status': 'success', 'new_queue': queue})
@app.route('/jbtheme')
def jbtheme():
return send_file('jukeboxtheme.css')
def get_current_song():
if queue and 0 <= current_song_index < len(queue):
return queue[current_song_index]
return None
@socketio.on('connect')
def handle_connect():
global current_timestamp, is_playing, last_update_time, queue, current_song_index
logging.debug("Client connected")
current_song = get_current_song()
if current_song:
emit('sync', {
'timestamp': current_timestamp,
'is_playing': is_playing,
'song': {
'filename': current_song['path'],
'title': current_song['name'],
'artist': current_song['artist'],
'cover_art': current_song['cover_url']
}
})
else:
emit('sync', {'timestamp': current_timestamp,
'is_playing': is_playing})
@socketio.on('play')
def handle_play(data):
global is_playing, current_timestamp, last_update_time
logging.debug(f"Play event received with data: {data}")
if 'timestamp' in data:
current_timestamp = data['timestamp']
logging.debug(f"Updated current_timestamp to: {current_timestamp}")
else:
logging.warning("Timestamp not found in play data")
is_playing = True
last_update_time = time.time()
emit('play', {'timestamp': current_timestamp}, broadcast=True)
@socketio.on('pause')
def handle_pause(data):
global is_playing, current_timestamp, last_update_time
logging.debug(f"Pause event received with data: {data}")
if 'timestamp' in data:
current_timestamp = data['timestamp']
logging.debug(f"Updated current_timestamp to: {current_timestamp}")
else:
logging.warning("Timestamp not found in pause data")
is_playing = False
last_update_time = time.time()
emit('pause', {'timestamp': current_timestamp}, broadcast=True)
@socketio.on('request_sync')
def handle_request_sync():
global current_timestamp, is_playing, last_update_time
logging.debug("Sync request received")
emit('sync', {'timestamp': current_timestamp, 'is_playing': is_playing})
@socketio.on('sync')
def handle_sync(data):
global current_timestamp, is_playing, last_update_time
logging.debug(f"Sync event received with data: {data}")
if 'timestamp' in data:
current_timestamp = data['timestamp']
logging.debug(f"Updated current_timestamp to: {current_timestamp}")
else:
logging.warning("Timestamp not found in sync data")
if 'is_playing' in data:
is_playing = data['is_playing']
else:
logging.warning("is_playing not found in sync data")
last_update_time = time.time()
emit('sync', {'timestamp': current_timestamp,
'is_playing': is_playing}, broadcast=True)
@socketio.on('timestamp')
def handle_timestamp(data):
global current_timestamp, last_update_time
logging.debug(f"Timestamp received from client: {data}")
if 'timestamp' in data:
current_timestamp = data['timestamp']
last_update_time = time.time()
logging.debug(f"Updated server timestamp to: {current_timestamp}")
# Ensure broadcast of the latest state
emit('sync', {'timestamp': current_timestamp,
'is_playing': is_playing}, broadcast=True)
@socketio.on('seek')
def handle_seek(data):
global current_timestamp, last_update_time, is_playing
logging.debug(f"Seek event received with timestamp: {data['timestamp']}")
if 'timestamp' in data:
current_timestamp = data['timestamp']
last_update_time = time.time()
logging.debug(f"Updated current_timestamp to: {current_timestamp}")
emit('sync', {'timestamp': current_timestamp, 'is_playing': is_playing, 'action': 'seek'}, broadcast=True)
else:
logging.warning("Timestamp not found in seek data")
@socketio.on('next_song')
def handle_next_song():
global current_song_index, queue, current_timestamp, is_playing, last_update_time
logging.debug("Next song requested")
current_song_index += 1
if current_song_index >= len(queue):
current_song_index = 0
if queue:
next_song = queue[current_song_index]
is_playing = True
last_update_time = time.time()
emit('next_song', {
'filename': next_song['path'],
'title': next_song['name'],
'artist': next_song['artist'],
'cover_art': next_song['cover_url']
}, broadcast=True)
else:
is_playing = False
emit('pause', {'timestamp': 0}, broadcast=True)
@socketio.on('prev_song')
def handle_prev_song():
global current_song_index, queue, current_timestamp, is_playing, last_update_time
logging.debug("Previous song requested")
current_song_index -= 1
if current_song_index < 0:
current_song_index = len(queue) - 1
if queue:
prev_song = queue[current_song_index]
is_playing = True
last_update_time = time.time()
emit('prev_song', {
'filename': prev_song['path'],
'title': prev_song['name'],
'artist': prev_song['artist'],
'cover_art': prev_song['cover_url']
}, broadcast=True)
else:
is_playing = False
emit('pause', {'timestamp': 0}, broadcast=True)
@socketio.on('select_song')
def handle_select_song(data):
global current_song_index, queue, current_timestamp, last_update_time, is_playing
logging.debug(f"Song selected: {data}")
current_song_index = data['index']
if current_song_index < len(queue):
selected_song = queue[current_song_index]
is_playing = True
last_update_time = time.time()
try:
emit('song_selected', {
'filename': selected_song['path'],
'title': selected_song['name'],
'artist': selected_song['artist'],
'cover_art': selected_song['cover_url']
}, broadcast=True)
except Exception as e:
logging.error(f"Error selecting song: {e}")
get_dir = os.listdir(LIBRARY_FOLDER)
for file in get_dir:
if selected_song['name'] in file:
emit('song_selected', {
'filename': os.path.join(LIBRARY_FOLDER, file),
'title': selected_song['name'],
'artist': selected_song['artist'],
'cover_art': selected_song['cover_url']
}, broadcast=True)
else:
logging.error("Selected song index out of range.")
def load_library():
global queue
if not os.path.exists(LIBRARY_FOLDER):
os.makedirs(LIBRARY_FOLDER)
if not os.path.exists(LIBDATA_FILE):
with open(LIBDATA_FILE, 'x', encoding='utf-8') as file:
file.write('[]')
queue = []
else:
with open(LIBDATA_FILE, 'r', encoding='utf-8') as file:
queue = json.load(file)
@app.route('/shuffle')
def shuffle():
global queue
random.shuffle(queue)
return jsonify({'status': 'success', 'new_queue': queue})
@app.route('/listeners')
def get_listeners():
room = '/' # Default namespace
participants = socketio.server.manager.rooms.get(room, {})
count = len(participants)
if count >= 2:
count = count - 1
return jsonify({'listeners': count})
if __name__ == '__main__':
load_library()
socketio.run(app, host='0.0.0.0', port=5135)