-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjukebox.py
354 lines (280 loc) · 10.9 KB
/
jukebox.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python3
import logging
import random
import re
import sys
import termios
from socket import gaierror
from mpd import MPDClient
# RADIO STATIONS
# Found via https://radiostationusa.fm/states/washington
# 9710 710 ESPN KIRO http://playerservices.streamtheworld.com/api/livestream-redirect/KIROAMAAC.aac
# 9790 790 KGMI http://18683.live.streamtheworld.com/KGMIAMAAC.aac
# 91000 1000 KOMO http://live.wostreaming.net/direct/sinclair-komoamaac-ibc2
# 91170 1170 KPUG http://18683.live.streamtheworld.com/KPUGAMAAC.aac
# 9929 92.9 KISM http://playerservices.streamtheworld.com/api/livestream-redirect/KISMFMAAC.aac
# 9965 96.5 JACK http://c14icyelb.prod.playlists.ihrhls.com/7788_icy
# 91043 104.3 KAFE http://playerservices.streamtheworld.com/api/livestream-redirect/KAFEFMAAC.aac
# 91053 105.3 SPIRIT http://crista-kcms.streamguys1.com/kcmsmp3
# 91065 106.5 PRAISE https://crista-kwpz.streamguys1.com/kwpzmp3
# Found via https://www.radio.net/genre/70s
# 91990 90s Hits HitsRadio http://playerservices.streamtheworld.com/api/livestream-redirect/977_90.mp3
# 91980 80s Planet http://80s.streamthenet.com:8888/stream/1/
# 91970 America's Greatest 70s Hits http://hydra.cdnstream.com/1823_128
# 91960 60s http://60s.streamthenet.com:8888/stream/1/
SONG_PATTERN = re.compile(r'(?:SDCARD|USB).*/(\d{3})-.*\..+')
RADIO_PATTERN = re.compile(r'RADIO/(\d{3,4})-.*.pls')
RANDOM_PLAY = "777"
STARTUP_SONG = "623"
RADIO_PREFIX = "9"
VOLUME_LIMIT = 35
CONNECT_TIMEOUT_SECONDS = 5
class Jukebox():
def __init__(self, server="localhost", port=6600):
self.server = server
self.port = port
self.songs = []
self.stations = []
self.is_random_play = False
self.is_radio_play = False
self.key_queue = []
self.mpd = None
def get_status(self):
status = None
if self.mpd is not None:
try:
status = self.mpd.status()
except:
self.mpd.disconnect()
self.mpd = None
if self.mpd is not None:
return status
self.mpd = MPDClient()
self.mpd.timeout = CONNECT_TIMEOUT_SECONDS
try:
self.mpd.connect(host=self.server, port=self.port)
status = self.mpd.status()
except gaierror:
logging.error("Could not connect to server, does the server name exist?")
return None
except ConnectionRefusedError:
logging.error("Connection refused, is the port open?")
return None
except Exception as ex:
logging.exception(ex)
return None
logging.info("Connected to MPD version %s", self.mpd.mpd_version)
return status
def initialize_connection(self):
logging.debug("Checking to see if connection to %s:%i can be established", self.server, self.port)
status = self.get_status()
if not status:
return False
volume = int(status['volume'])
logging.debug("Volume is set to %i", volume)
logging.debug("%s songs in queue", status['playlistlength'])
# Set jukebox required settings
self.mpd.consume(1)
self.mpd.single(0)
self.mpd.random(0)
self.mpd.repeat(0)
if volume > VOLUME_LIMIT:
self.mpd.setvol(VOLUME_LIMIT)
if status['state'] != 'play':
self.mpd.play()
self.songs = {SONG_PATTERN.match(song)[1]: song for song in self.mpd.list('file') if SONG_PATTERN.match(song)}
logging.info("Found %i jukebox songs", len(self.songs))
rootls = self.mpd.lsinfo()
if "RADIO" in [entry.get("directory") for entry in rootls]:
self.stations = {RADIO_PATTERN.match(radio["playlist"])[1]: radio["playlist"] for radio in self.mpd.lsinfo("RADIO") if RADIO_PATTERN.match(radio["playlist"])}
else:
self.stations = []
logging.info("Found %i radio stations", len(self.stations))
# TODO: In the future we might be able to detect these states
self.is_random_play = False
self.is_radio_play = False
# Enqueue startup song, if it exists
if status["playlistlength"] == "0":
self.enqueue_song(STARTUP_SONG)
return True
def close_connection(self):
if self.mpd:
self.mpd.close()
self.mpd.disconnect()
self.mpd = None
def handle_keyboard(self, key):
if key in "0123456789":
self.add_key_to_queue(key)
elif ord(key) == 127: # Backspace
self.remove_key_from_queue()
elif key == "+":
self.increase_volume()
elif key == "-":
self.decrease_volume()
elif key == "s" or key == "/":
self.skip_song()
elif key == "r" or key == "\n":
self.reset_key_queue()
elif key == "i" or key == "*":
self.close_connection()
self.initialize_connection()
else:
logging.debug("Ignoring keyboard %i", ord(key))
def reset_key_queue(self):
logging.debug("Reset key queue")
self.key_queue = []
def remove_key_from_queue(self):
logging.debug("Removing key from queue")
self.key_queue = self.key_queue[:-1]
def add_key_to_queue(self, key):
logging.debug("Add key to queue %s", key)
self.key_queue.append(key)
song = ''.join(self.key_queue)
# Radio stations start with a prefix
if song[0] == RADIO_PREFIX:
if len(song) > 3:
logging.debug("Trying to find station %s", song[1:])
if self.enqueue_station(song[1:]) or len(song) >= 5:
self.reset_key_queue()
elif len(song) == 3:
self.reset_key_queue()
if self.is_radio_play:
self.end_radio_play()
if song == RANDOM_PLAY:
if self.is_random_play:
self.end_random_play()
else:
self.start_random_play()
return
if self.is_random_play:
self.end_random_play()
logging.debug("Trying to find song %s", song)
self.enqueue_song(song)
def enqueue_song(self, number):
logging.debug("Finding song %s to enqueue", number)
if number in self.songs:
logging.debug("Found song %s", self.songs[number])
# Make sure this song isn't already on queue
status = self.get_status()
if int(status["playlistlength"]) > 0:
queue = [record["file"] for record in self.mpd.playlistinfo()]
if self.songs[number] in queue:
logging.info("Did not queue song because it's already in queue")
return False
logging.debug("Enqueuing %s", self.songs[number])
self.mpd.findadd("file", self.songs[number])
if status["state"] != "play":
self.mpd.play()
return True
else:
logging.debug("Could not locate the song")
return False
def enqueue_station(self, number):
logging.debug("Finding station %s to enqueue", number)
if number in self.stations:
logging.debug("Found station %s", self.stations[number])
status = self.get_status()
self.mpd.clear()
logging.debug("Enqueuing %s", self.stations[number])
self.mpd.load(self.stations[number])
if status["state"] != "play":
self.mpd.play()
self.is_radio_play = True
return True
logging.debug("Could not locate the station")
return False
def end_radio_play(self):
self.is_radio_play = False
self.get_status()
self.mpd.clear()
def start_random_play(self):
if not self.is_random_play:
logging.info("Starting random play")
self.is_random_play = True
self.get_status()
# Add all songs except those in queue
queue = [record["file"] for record in self.mpd.playlistinfo()]
songs_to_add = [song for song in self.songs.values() if song not in queue]
# Shuffle the songs
random.shuffle(songs_to_add)
for song in songs_to_add:
logging.debug("Enqueuing %s", song)
self.mpd.findadd("file", song)
self.mpd.play()
def end_random_play(self):
if self.is_random_play:
logging.info("Ending random play")
self.is_random_play = False
status = self.get_status()
if status["state"] == "play":
# Delete all songs except the first
self.mpd.delete((1,))
else:
self.mpd.clear()
def play(self):
status = self.get_status()
if status['state'] != 'play':
self.mpd.play()
def skip_song(self):
status = self.get_status()
if status['state'] == 'play':
self.mpd.next()
def increase_volume(self):
# Pull current volume from system
status = self.get_status()
volume = int(status['volume'])
if volume < VOLUME_LIMIT:
volume += 1
logging.info("Increasing volume to %i", volume)
self.mpd.setvol(volume)
else:
self.mpd.setvol(VOLUME_LIMIT)
def decrease_volume(self):
# Pull current volume from system
status = self.get_status()
volume = int(status['volume'])
if volume > 0:
volume -= 1
logging.info("Decreasing volume to %i", volume)
self.mpd.setvol(volume)
else:
self.mpd.setvol(0)
def get_char_keyboard():
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
c = None
try:
c = sys.stdin.read(1)
except IOError:
pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
return c
def main(argv):
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', "%H:%M:%S")
handler.setFormatter(formatter)
root.addHandler(handler)
logging.debug("Starting jukebox")
# Read server from command line as first argument
server = "localhost"
if len(argv) > 0:
server = argv[0]
j = Jukebox(server=server)
if not j.initialize_connection():
logging.critical("Could not establish connection to MPD")
return
key = get_char_keyboard()
while key != "q":
j.handle_keyboard(key)
key = get_char_keyboard()
logging.debug("Stopping jukebox")
j.close_connection()
sys.exit(0)
if __name__ == "__main__":
main(sys.argv[1:])