-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.py
66 lines (52 loc) · 1.62 KB
/
remote.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
import web
import subprocess
urls = (
'/(.*)', 'RemoteControl'
)
app = web.application(urls, globals())
allowed_commands = ['play', 'stop']
class RemoteControl:
def GET(self, command):
if not command:
return Remote.Instance().status()
elif command in allowed_commands:
return self.run(command)
else:
return "Unknown command"
def run(self, command):
if command == 'play':
Remote.Instance().play()
return "Starting radio"
elif command == 'stop':
Remote.Instance().stop()
return "Stopping radio"
return command
class Singleton:
def __init__(self, decorated):
self._decorated = decorated
def Instance(self):
try:
return self._instance
except AttributeError:
self._instance = self._decorated()
return self._instance
def __call__(self):
raise TypeError('Singletons must be accessed through the `Instance` method.')
@Singleton
class Remote(object):
player = None
def play(self):
"http://sverigesradio.se/topsy/direkt/164-hi-mp3.m3u"
if self.player is None:
self.player = subprocess.Popen(["vlc", "-I rc", "http://sverigesradio.se/topsy/direkt/164-hi-mp3.m3u"])
def stop(self):
if self.player is not None:
self.player.terminate()
self.player = None
def status(self):
if self.player is None:
return "Radio is not running"
else:
return "Radio is running"
if __name__ == "__main__":
app.run()