-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpd-plugin.py
executable file
·258 lines (169 loc) · 7.71 KB
/
mpd-plugin.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#! /usr/bin/python3
# by FvH, released under Apache License v2.0
# either install 'python3-paho-mqtt' or 'pip3 install paho-mqtt'
# also python-mpd2 is required (via pip)
import math
from mpd import MPDClient
import paho.mqtt.client as mqtt
import threading
import time
import socket
import sys
mqtt_server = 'mqtt.vm.nurd.space'
topic_prefix = 'GHBot/'
channels = ['nurdbottest', 'nurds', 'test', 'nurdsbofh']
mpd_server = 'spacesound.vm.nurd.space'
mpd_port = 6600
prefix = '!'
def announce_commands(client):
target_topic = f'{topic_prefix}to/bot/register'
client.publish(target_topic, 'cmd=play|descr=Start playing.')
client.publish(target_topic, 'cmd=stop|descr=Stop playing.')
client.publish(target_topic, 'cmd=next|descr=Skip to the next track.')
client.publish(target_topic, 'cmd=pause|descr=Stops the music, or unstops it.')
client.publish(target_topic, 'cmd=prev|descr=Skip to the previous track.')
# client.publish(target_topic, 'cmd=np|descr=What is playing right now?')
client.publish(target_topic, 'cmd=clearpl|descr=Clear the current playlist.')
client.publish(target_topic, 'cmd=mpdsearch|descr=Search for a song, by title keywords.')
client.publish(target_topic, 'cmd=mpdadd|descr=Add a song(s) to the current play queue. Searches by title keyword(s).')
def gen_song_name(song_meta):
playing = ''
if 'artist' in song_meta and 'title' in song_meta:
playing += song_meta['artist'] + ' - ' + song_meta['title']
else:
if 'title' in song_meta:
playing += f' title: {song_meta["title"]}'
if 'artist' in song_meta:
playing += f' (artist: {song_meta["artist"]})'
if 'album' in song_meta:
playing += f' (album: {song_meta["album"]})'
if playing.strip() == '' and 'file' in song_meta:
playing = song_meta['file']
if 'duration' in song_meta:
song_meta = float(song_meta['duration'])
duration_minutes = math.floor(song_meta / 60)
if math.fmod(song_meta, 60) >= 30:
playing += f' (takes almost {duration_minutes + 1:.0f} minutes to play)'
else:
playing += f' (takes about {duration_minutes:.0f} minutes to play)'
return playing
def group_results(in_):
out = ''
seen = set()
for result in in_:
if 'artist' in result and 'title' in result:
to_add = '\2' + result['artist'] + '\2: ' + result['title']
temp = to_add.lower()
if temp in seen:
continue
seen.add(temp)
if out != '':
out += ', '
out += to_add
if len(out) > 350:
break
return out
def on_message(client, userdata, message):
global prefix
text = message.payload.decode('utf-8')
topic = message.topic[len(topic_prefix):]
if topic == 'from/bot/command' and text == 'register':
announce_commands(client)
return
if topic == 'from/bot/parameter/prefix':
prefix = text
return
if len(text) == 0:
return
if text[0] != prefix:
return
parts = topic.split('/')
channel = parts[2] if len(parts) >= 3 else 'nurds'
nick = parts[3] if len(parts) >= 4 else 'jemoeder'
command = text[1:].split(' ')[0]
if (channel in channels or channel[0] == '\\') and command in ['next', 'np', 'prev', 'pause', 'clearpl', 'play', 'stop', 'mpdsearch', 'mpdadd']:
response_topic = f'{topic_prefix}to/irc/{channel}/notice'
try:
mpd_client = MPDClient()
mpd_client.connect(mpd_server, mpd_port)
current_song = mpd_client.currentsong()
status = mpd_client.status()
playing = gen_song_name(current_song)
if command == 'next':
mpd_client.next()
now_current_song = mpd_client.currentsong()
now_playing = gen_song_name(now_current_song)
client.publish(response_topic, f'Skipped \3{4}{playing}\3, now playing: \3{3}{now_playing}\3')
elif command == 'prev':
mpd_client.previous()
client.publish(response_topic, f'Went back to the previous song')
elif command == 'play':
mpd_client.pause(0)
client.publish(response_topic, f'The music should be playing now')
elif command == 'stop':
mpd_client.pause(1)
client.publish(response_topic, f'The music should be stopped now')
elif command == 'mpdsearch' or command == 'mpdadd':
space = text.find(' ')
if space != -1:
if command == 'mpdsearch':
results = mpd_client.search('title', text[space + 1:])
out = group_results(results)
if len(out) < 350:
results = mpd_client.search('artist', text[space + 1:])
temp = group_results(results)
if temp != '':
if out != '':
out += ' | '
out += temp
if len(out) > 350:
out = out[0:350]
if out == '':
client.publish(response_topic, 'No such song found')
else:
client.publish(response_topic, 'Results: ' + out)
elif command == 'mpdadd':
mpd_client.searchadd('title', text[space + 1:])
client.publish(response_topic, 'Some song(s) might have been added to the play queue')
else:
client.publish(response_topic, 'Parameter missing')
elif command == 'pause':
mpd_client.pause()
status = mpd_client.status()
if status["state"] == 'play':
client.publish(response_topic, f'The music is unpaused')
else:
client.publish(response_topic, f'The music is paused')
elif command == 'clearpl':
mpd_client.clear()
mpd_client.pause(1)
client.publish(response_topic, f'The playlist has been cleared (and paused)')
# elif command == 'np':
# duration = float(status['duration'])
# elapsed = float(status['elapsed'])
# finished_percentage = elapsed * 100 / duration
# time_left = duration - elapsed
# light_meters = 299792458 * time_left
# client.publish(response_topic, f'Now playing: {playing} ({100 - finished_percentage:.2f}% left or {time_left:.2f} seconds or the time that light moves {light_meters / 1000:.2f} meters)')
mpd_client.close()
mpd_client.disconnect()
except Exception as e:
print(f'Exception: {e}, line number: {e.__traceback__.tb_lineno}')
client.publish(response_topic, f'mpd: {e}')
def on_connect(client, userdata, flags, rc):
client.subscribe(f'{topic_prefix}from/irc/#')
client.subscribe(f'{topic_prefix}from/bot/command')
def announce_thread(client):
while True:
try:
announce_commands(client)
time.sleep(4.1)
except Exception as e:
print(f'Failed to announce: {e}')
client = mqtt.Client(f'{socket.gethostname()}_{sys.argv[0]}', clean_session=False)
client.on_message = on_message
client.on_connect = on_connect
client.connect(mqtt_server, port=1883, keepalive=4, bind_address="")
t = threading.Thread(target=announce_thread, args=(client,))
t.start()
client.loop_forever()