-
Notifications
You must be signed in to change notification settings - Fork 3
/
MpdActor.py
47 lines (34 loc) · 1.35 KB
/
MpdActor.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
import pykka, mpd, logging
DEFAULT_VOLUME = 100
class MpdActor(pykka.ThreadingActor):
def __init__(self):
super(MpdActor, self).__init__()
self.client = mpd.MPDClient()
self.client.timeout = 10
self.client.idletimeout = None
self.client.connect("localhost", 6600)
def getCurrentSong(self):
try:
name = self.client.currentsong()["file"]
elapsed = int(float(self.client.status()["elapsed"]))
return {"name": name, "elapsed": elapsed}
except KeyError:
return None
def playByNameFrom(self, name, playFrom):
try:
self.client.setvol(0)
self.client.clear()
self.client.add(name)
self.client.play()
currentSong = self.client.currentsong()
if playFrom + 20 > int(currentSong['time']):
# play from start, if trying to play too close from the end
playFrom = 0
logging.getLogger('zbap').info('Playing %s from %s' % (name, playFrom))
self.client.seekid(currentSong['id'], playFrom)
self.client.setvol(DEFAULT_VOLUME)
except mpd.CommandError as e:
logging.getLogger('zbap').error('MPD Command error')
logging.getLogger('zbap').exception(e)
def pause(self):
self.client.pause()