-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotifypi_service.py
137 lines (117 loc) · 3.3 KB
/
spotifypi_service.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
import os
import re
import asyncio
import subprocess
import websockets
import pyautogui
#
# tools
#
def system_call(cmd):
return subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf-8')
def analyze_amixer_result(text):
strings = [str.strip() for str in text.split('\n')]
for s in strings:
m = re.match(r'Front Left: Playback.+?\[(\d+?)%\]', s)
if (m is not None) and len(m.groups()) > 0:
print(m.group(1))
return m.group(1)
return 'missing'
#
# commands
#
def get_volume():
out_text = system_call(['amixer', 'sget', 'Master'])
result = analyze_amixer_result(out_text)
print(f'get_volume: {result}')
return result
def set_volume(volume):
out_text = system_call(['amixer', 'sset', 'Master', f'{volume}%'])
result = analyze_amixer_result(out_text)
print(f'set_volume: {result}')
return result
def toggle_play_pause():
print('toggle_play_pause')
pyautogui.hotkey('alt', 'shift', 'p')
def next_track():
print('next_track')
pyautogui.hotkey('alt', 'shift', '.')
def prev_track():
print('prev_track')
pyautogui.hotkey('alt', 'shift', ',')
def toggle_shuffle():
print('toggle_shuffle')
pyautogui.hotkey('alt', 'shift', 's')
def toggle_repeat_state():
print('toggle_repeat_state')
pyautogui.hotkey('alt', 'shift', 'r')
def shutdown():
print('shutdown')
os.system('sudo shutdown -h now')
def reboot():
print('reboot')
os.system('sudo reboot')
#
# run command
#
def run_cmd(cmd):
print(f'cmd: {cmd}')
if len(cmd) == 0:
return '[error](missing)'
name = cmd[0]
if name == 'get_volume':
result = get_volume()
return f'[volume]({result})'
elif name == 'set_volume':
if len(cmd) == 1:
return '[error](missing)'
result = set_volume(cmd[1])
return f'[volume]({result})'
elif name == 'toggle_play_pause':
toggle_play_pause()
return '[toggle_play_pause](ok)'
elif name == 'next_track':
next_track()
return '[next_track](ok)'
elif name == 'prev_track':
prev_track()
return '[prev_track](ok)'
elif name == 'toggle_shuffle':
toggle_shuffle()
return '[toggle_shuffle](ok)'
elif name == 'toggle_repeat_state':
toggle_repeat_state()
return '[toggle_repeat_state](ok)'
elif name == 'shutdown':
shutdown()
return '[shutdown]'
elif name == 'reboot':
reboot()
return '[reboot]'
return '[error](not_found)'
#
# websocket
#
async def handle_connection(websocket):
try:
connected_list.append(websocket)
async for message in websocket:
print(f'\nmessage: "{message}"')
cmd = message.split()
result = run_cmd(cmd)
if result != '[shutdown]' or result != '[reboot]':
for connected in connected_list:
await connected.send(result)
except websockets.exceptions.ConnectionClosedError:
print('> ConnectionClosedError')
finally:
connected_list.remove(websocket)
async def main():
async with websockets.serve(handle_connection, "0.0.0.0", 9487):
await asyncio.Future() # run forever
#
# setup and start
#
connected_list = []
pyautogui.FAILSAFE = False
asyncio.run(main())