-
Notifications
You must be signed in to change notification settings - Fork 0
/
nkplay.py
executable file
·85 lines (72 loc) · 2.09 KB
/
nkplay.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
#!/usr/bin/env python3
from time import time
from getch import getche
from subprocess import call
import signal
import musicpd
MAXPLAYTIME = 3600
def sleep_handler(signum, frame):
music = musicpd.MPDClient()
music.connect('localhost', 6600)
music.clear()
print('going to sleep')
music.disconnect()
signal.signal(signal.SIGALRM, sleep_handler)
def get_command():
command = ''
while True:
c = getche()
if c == '\n':
return 'STOP' if command == '' else command
if c in ['+', '-']:
return c
if c in [str(x) for x in range(10)]:
command += c
else:
print('garbage received: "' + str(c) + '", clearing command')
command = ''
def main():
music = musicpd.MPDClient()
music.connect('localhost', 6600)
music.replay_gain_mode('track')
music.volume(100)
music.update()
music.disconnect()
while True:
try:
code = get_command()
except OverflowError:
# because alarm fired
continue
music.connect('localhost', 6600)
try:
if code == 'STOP':
print('STOP')
music.clear()
elif code == '8686':
call('/sbin/poweroff')
elif code == '-':
print('previous')
music.previous()
elif code == '+':
print('next')
music.next()
else:
music.clear()
music.load(code + '.m3u')
music.consume(0)
music.repeat(0)
if code == '999':
music.random(1)
else:
music.random(0)
music.play()
signal.alarm(MAXPLAYTIME)
print('Now playing: ' + code)
except Exception as e:
print('got an error: ' + str(e))
music.disconnect()
if __name__ == '__main__':
# this should be called from a script which reruns it on failure.
print("listening for song ids...")
main()