/
lmsio.py
executable file
·107 lines (89 loc) · 3.16 KB
/
lmsio.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
#!/usr/bin/python
# -*- coding: utf-8
"""Interface functions for the communication with a Squeezebox
Server"""
__author__ = 'Tim B. Herbstrith'
from pylms.server import Server
import time
import re
import subprocess
import os
_was_playing = ''
def connect_to_player_at_server(playername, hostname):
"""Connects to a Squeezbox player connected to a server by
their name.
Args:
playername (string): name of player
hostname (string): hostname of Squeezbox server
Returns:
pylms.player.Player
"""
server = Server(hostname=hostname)
server.connect()
players = [ply for ply in server.get_players() if ply.name ==
playername]
if len(players) < 1:
raise RuntimeError(('No player named %s connected to '
'server named %s') % (playername, hostname))
return players[0]
def phono(player, p=None):
"""Runs or stops *darkice*
If `p` is `None` a new subprocess running *darkice* is
created. If `p` is a subprocess it is killed.
Args:
p (subprocess.Popen): Process running *darkice* or `None`,
defaults to `None`
Returns:
subprocess.Popen / `None`: subprocess running *darkice* or
`None` if the subprocess has been killed.
"""
path = os.path.split(os.path.realpath(__file__))[0]
log_fn = os.path.join(path, 'cartridge.log')
if p == None:
with open(log_fn, 'a') as f:
on = 'On: %d\n' % int(time.time())
f.write(on)
return _start_phono(player)
else:
with open(log_fn, 'a') as f:
off = 'Off: %d\n' % int(time.time())
f.write(off)
player.set_volume(player.get_volume() // 2)
player.playlist_play(_was_playing)
p.terminate()
def _start_phono(player):
global _was_playing
arecord = subprocess.Popen(['arecord', '-l'], stdout=subprocess.PIPE)
hw = arecord.communicate()[0]
print hw
try:
pat_dev = r'card (\d+):'
device = re.search(pat_dev, hw).groups(1)[0]
pat_sub = r'Subdevice #(\d+):'
subdev = re.search(pat_sub, hw).groups(1)[0]
except AttributeError:
return
path = os.path.split(os.path.realpath(__file__))[0]
cfg_in_fn = os.path.join(path, 'darkice.cfg')
pwd_fn = os.path.join(path, 'pwd.txt')
cfg_out_fn = os.path.join(path, '.cfg')
with open(cfg_in_fn, 'r') as cfg_in:
cfg = cfg_in.read()
with open(pwd_fn, 'r') as pwd_file:
pwd = pwd_file.read()
cfg = cfg.format(dev=device, sub=subdev, pwd=pwd)
with open(cfg_out_fn, 'w') as cfg_out:
cfg_out.write(cfg)
p = subprocess.Popen(['sudo', 'darkice', '-c', cfg_out_fn])
# Save current track and start playing phono
_was_playing = player.get_track_path()
flds = ['server', 'port', 'mountPoint']
pats = [fld + r'\s*=\s(\w+)' for fld in flds]
parts = [re.search(pat, cfg).groups(1)[0] for pat in pats]
url = 'http://{0}:{1}/{2}'.format(*parts)
player.playlist_play(url)
while not player.get_mode() == 'play':
player.play()
time.sleep(.1)
player.set_volume(2 * player.get_volume())
return p