-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrooveBot.py
executable file
·269 lines (225 loc) · 9.67 KB
/
GrooveBot.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
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python2
# This file is part of GrooveBot.
#
# GrooveBot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GrooveBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GrooveBot. If not, see <http://www.gnu.org/licenses/>.
from collections import deque
from getpass import getpass
import shelve
import sys
from twisted.internet import reactor, threads
from JlewBot import JlewBotFactory
from VolBot import VolBot
import util
CONTROL = ['add', 'remove', 'oops']
PLAYBACK = ['pause', 'resume']
QUEUE = ['show', 'dump', 'status', 'skip']
VOTE = ['vote']
RADIO = ['radio']
TIRED = ['lame']
BLAME = ['blame']
class GrooveBot(VolBot):
bot_name = 'foss_groovebot'
# Valid values are 'stopped', 'playing', and 'paused'
status = 'stopped'
current_song = ''
api_inst = None
def setup(self, factory, api):
# super() for classic classes:
VolBot.setup(self, factory)
# Restore state
self.persistent_store = shelve.open('state.db')
self.song_request_db = self.persistent_store.get('requests', dict())
self.song_queue = self.persistent_store.get('queue', deque())
for command in self.capabilities:
factory.register_command(command, self.request_queue_song)
api.register_next_func(self._playback_cb)
self.api_inst = api
# Try to play a song if we've loaded one
self._playback_cb()
# State-based callbacks
def _add_lookup_cb(self, song, responder, user):
if not song:
responder("No songs found.")
elif song['SongID'] in self.song_queue:
responder('%s is already in queue' % self._display_name(song, rating=False))
else:
responder('Queueing %s' % (self._display_name(song, id_=True, album=True)))
self.song_request_db[song['SongID']] = user
self.song_queue.append(song['SongID'])
if self.status == 'stopped':
self._playback_cb()
else:
self.__flush_state()
def _playback_cb(self, *args, **kwargs):
"""Called whenever a new song might be wanted"""
if self.status == 'paused' or not self.song_queue:
return
self.status = 'playing'
self.current_song = self.song_queue.popleft()
try:
song = self.api_inst.lookup(self.current_song)
self.api_inst.play_song(self.current_song)
self.describe(self.channel, 'Playing %s' % self._display_name(song))
except Exception:
# Eeeek, lookup failed
self.describe(self.channel, 'Something went wrong... ):')
current_song = None
self.__flush_state()
def request_queue_song(self, responder, user, channel, command, msg):
if channel == self.bot_name and command not in self.quiet:
responder("Let's talk to the class")
return
if command == "add":
responder("Got Request, processing")
threads.deferToThread(self.api_inst.request_song_from_api, msg).addCallback(self._add_lookup_cb, responder, user).addErrback(util.err_chat, responder)
elif command == "remove":
try:
self.song_queue.remove(msg)
self.__flush_state()
song = self.api_inst.lookup(msg)
song_pretty = self._display_name(song)
responder('Removed %s' % song_pretty)
except:
responder('Could not remove %s' % msg)
elif command == "oops":
for song_id in reversed(self.song_queue):
if user == self.song_request_db[song_id]:
self.song_queue.remove(song_id)
self.__flush_state()
song = self.api_inst.lookup(song_id)
song_pretty = self._display_name(song)
responder('Removed %s' % song_pretty)
break
else:
responder('There was nothing to remove')
elif command == "show":
# song_queue is a deque, so slices are type Slice, which do not play
# well with comprehensions. This should only be an issue for
# extremely long queues
song_names = [
self._display_name(self.api_inst.lookup(song_id))
for song_id in self.song_queue
][:5]
responder(', '.join(song_names))
if len(self.song_queue) > 5:
responder('call "dump" to see more')
elif command == 'blame':
song = self.current_song
if msg:
song = msg
user = self.song_request_db.get(song)
if not user:
responder('No song like that was queued by anyone')
else:
responder('You can blame %s for that song.' % user)
elif command == "dump":
for song_id in self.song_queue:
song = self.api_inst.lookup(song_id)
self.msg(user, '%s' % self._display_name(song, id_=True, album=True))
elif command == "pause":
self.status = 'paused'
threads.deferToThread(self.api_inst.api_pause).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif command == "resume":
self.status = 'playing'
threads.deferToThread(self.api_inst.api_play).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif command == "skip":
self.status = 'stopped'
self.current_song = ''
self._playback_cb()
self.__flush_state()
elif command == "radio":
if msg == "on":
threads.deferToThread(self.api_inst.api_radio_on).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif msg == "off":
threads.deferToThread(self.api_inst.api_radio_off).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif command == "status":
if self.current_song:
display = self._display_name(self.api_inst.lookup(self.current_song))
responder('{}: {}'.format(self.status, display))
else:
responder("No song playing.")
elif command == 'vote':
if msg == 'up':
self.api_inst.api_radio_vote_up()
elif msg == 'down':
self.api_inst.api_radio_vote_down()
vote = self.api_inst.api_radio_get_vote()
if not vote:
responder('Current song has not been voted on')
else:
if vote == 'love':
vote = 'up'
elif vote == 'ban':
vote = 'down'
responder('Current song has been voted %s' % vote)
elif command == 'lame':
self.api_inst.api_radio_tired()
responder('Current song will not be played for a while.')
def _display_name(self, song, id_=False, album=False, rating=True):
"""Method to consistently output songs for each use."""
response = '{} by {}'.format(song['SongName'], song['ArtistName'])
if id_:
response = '{} {}'.format(song['SongID'], response)
if album:
response += ' on {}'.format(song['AlbumName'])
if rating:
response += ' {}'.format(song.get('Rating', ''))
return response
def __flush_state(self):
self.persistent_store['requests'] = self.song_request_db
# Push the current song to the front so it will be restored and
# restarted.
queue_copy = self.song_queue.__copy__()
queue_copy.appendleft(self.current_song)
self.persistent_store['queue'] = queue_copy
# Just in case
self.persistent_store.sync()
def pick_backend(backend, factory):
while not factory.active_bot:
print('Not ready yet.')
reactor.callLater(2, pick_backend, backend, factory)
return
bot = factory.active_bot
if backend == 'mpd':
bot.capabilities = QUEUE + PLAYBACK + CONTROL
bot.quiet = QUEUE
from api.mpd_wrapper import MPDApi
api = MPDApi()
elif backend == 'pandora':
bot.capabilities = QUEUE + PLAYBACK + VOTE + TIRED
bot.quiet = QUEUE
from api.pandora import PandApi
uname = raw_input('Enter your Pandora username: ').strip()
upass = getpass('Enter your Pandora password: ').strip()
station = raw_input('Which station would you like to connect to: ').strip()
if not (uname and upass and station):
reactor.stop()
return
api = PandApi(uname, upass, station)
elif backend == 'spotify':
bot.capabilities = QUEUE + PLAYBACK + CONTROL + BLAME
bot.quiet = QUEUE
from api.libspotify import SpotApi
api = SpotApi()
elif backend == 'soundcloud':
bot.capabilities = QUEUE + PLAYBACK + CONTROL + BLAME
bot.quiet = QUEUE
from api.sound_cloud import SoundCloudAPI
api = SoundCloudAPI()
bot.setup(factory, api)
if __name__ == '__main__':
f = JlewBotFactory(protocol=GrooveBot)
reactor.connectTCP("irc.freenode.net", 6667, f)
reactor.callLater(5, pick_backend, sys.argv[1], f)
reactor.run()