-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.py
331 lines (275 loc) · 9.78 KB
/
middleware.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import asyncio
from db_object import DBItem
from core import BotMiddleware, UsesMiddleware, FakeRAWMiddleware, FakeActionMiddleware, LoggerMiddleware, Log, UsesLogger
from actions import (
ReplyAction,
QueuedNotificationAction,
PlayOneSongAction,
ListQueueAction,
DumpQueue
)
from tinydb import TinyDB, where
from event import PlayEvent
from command import (
QueueCommand,
QueueFirstCommand,
SkipCommand,
DramaticSkipCommand, #for the my mys
ClearQueueCommand,
ListQueueCommand,
TestSkipCommand,
NeonLightShowCommand,
HelpCommand,
DumpQueueCommand
)
class UsesRaw(UsesMiddleware):
CONSUMES = FakeRAWMiddleware
class SendsActions(UsesMiddleware):
PRODUCES = FakeActionMiddleware
@asyncio.coroutine
def send_action(self, action):
yield from self.send(action, tag=FakeActionMiddleware.TAG)
class PacketMiddleware(BotMiddleware, UsesLogger, UsesRaw):
TAG = 'tag_bot_message'
CONTROL_TAG = 'tag_bot_message_control'
TYPE = BotMiddleware.OUTPUT
CONTROL_BACKLOG_START = 'bot_message_backlog_start'
CONTROL_BACKLOG_END = 'bot_message_backlog_end'
LOG_NAME = 'Packet'
ENABLED_MESSAGES = [
NeonLightShowCommand,
HelpCommand,
]
def __init__(self):
super(PacketMiddleware, self).__init__()
self.enabled_messages = []
self.enabled_messages.extend(self.ENABLED_MESSAGES)
UsesRaw.set_handler(self, self.handle_event)
def request_support(self, request):
incompatible_classes = []
for _class in request:
if hasattr(_class, 'is_this'):
self.enabled_messages.append(_class)
else:
incompatible_classes.append(_class)
if incompatible_classes:
return incompatible_classes
else:
return True
def message_id_exists(self, uid):
exists = self.db.search(where('uid') == uid)
return len(exists) > 0
def create_db_object(self, packet):
content = packet.data['content']
possibles = [(event_class.is_this(content), event_class) for event_class in self.enabled_messages]
score, event_class = sorted(possibles, key=lambda x: x[0])[-1]
if score > 0:
try:
return event_class(packet)
except:
self.exception('failed to create: {}', packet.data)
return None
def save_to_db(self, db_item):
#seld.debug('adding {} to DB' db_item)
if hasattr(db_item, 'to_db_dict'):
db_dict = db_item.to_db_dict()
self.db.insert(db_dict)
@asyncio.coroutine
def send_control_message(self, message):
#def _send(queue_list, tag, message):
yield from self._send(self._output[self.TAG], self.CONTROL_TAG, message)
@asyncio.coroutine
def handle_event(self, packet):
if packet.type == 'snapshot-event':
yield from self.send_control_message(self.CONTROL_BACKLOG_START)
for message in packet.messages():
if self.message_id_exists(message.uid):
if not message.past:
self.verbose('ignoring {}', message.data['content'])
continue
self.verbose('message: {}', message.data['content'])
db_object = self.create_db_object(message)
if db_object:
self.debug('DB Object: {}', db_object)
if not db_object.is_prepared():
try:
yield from db_object.prepare()
except Exception as e:
self.debug('Failed to process: {}; Exception: {}', db_object, e)
# only record objects that are sucessfully prepared to the db
if db_object.is_prepared():
self.save_to_db(db_object)
if db_object.DB_TAG == HelpCommand.DB_TAG:
db_object.set_commands(self.enabled_messages)
yield from self.send(db_object)
if self.closing:
break
if packet.type == 'snapshot-event':
yield from self.send_control_message(self.CONTROL_BACKLOG_END)
class UsesCommands(UsesMiddleware):
CONSUMES = PacketMiddleware
@classmethod
def setup_self(cls, self):
self.in_backlog = False
self.backlog_processed = False
self._recv_functions[PacketMiddleware.CONTROL_TAG] = self.handle_control_message
@asyncio.coroutine
def handle_control_message(self, message):
if message == PacketMiddleware.CONTROL_BACKLOG_START:
self.in_backlog = True
elif message == PacketMiddleware.CONTROL_BACKLOG_END:
self.in_backlog = False
self.backlog_processed = True
class SimpleActionMiddleware(BotMiddleware, UsesCommands, UsesLogger, SendsActions):
TAG = 'tag_simple_action_middleware'
TYPE = BotMiddleware.INPUT
LOG_NAME = 'Action'
@asyncio.coroutine
def setup(self, db):
yield from super(SimpleActionMiddleware, self).setup(db)
UsesCommands.set_handler(self, self.handle_event)
@asyncio.coroutine
def handle_event(self, command):
if hasattr(command, 'get_actions') and not self.in_backlog:
for action in command.get_actions():
yield from self.send_action(action)
class PlayQueuedSongsMiddleware(BotMiddleware, UsesCommands, UsesLogger, SendsActions):
TAG = 'tag_queue_events'
TYPE = BotMiddleware.OUTPUT
LOG_NAME = 'Queue'
MIDDLEWARE_SUPPORT_REQUESTS = {
PacketMiddleware.TAG: [
QueueCommand, SkipCommand, ClearQueueCommand, ListQueueCommand, TestSkipCommand, DumpQueueCommand, PlayEvent, DramaticSkipCommand, QueueFirstCommand
]
}
def __init__(self):
super(PlayQueuedSongsMiddleware, self).__init__()
self.message_queue = asyncio.JoinableQueue()
self.song_queue = []
self.current_song = None
self.play_callback = None
# queued a song, waiting to see if it turns up
self.expecting_song = False
self.in_backlog = False
UsesCommands.set_handler(self, self.handle_event)
@asyncio.coroutine
def start_close(self):
if self.play_callback:
self.play_callback.cancel()
yield from super(PlayQueuedSongsMiddleware, self).start_close()
def status_string(self):
return '\n'.join([
'QueueMiddleware: Current Song: {}({}s)'.format(self.current_song, self.current_song.remaining_duration() if self.current_song else 'NaN'),
'\tCurrent Queue: {}'.format(self.song_queue)
])
def load_state_from_db(self, db):
super(PlayQueuedSongsMiddleware, self).load_state_from_db(db)
self.debug('load state from db')
saved_state = db.search(where('type') == self.TAG)
if saved_state:
queue = [db.search(where('uid') == uid)[0] for uid in saved_state[0]['queue']]
self.song_queue = [DBItem.create_object_from_db_entry(song) for song in queue]
self.song_queue.sort()
self.debug('loaded queue: {}', self.song_queue)
events = db.search(where('type') == PlayEvent.DB_TAG)
if events:
events = sorted(events, key=lambda x: x['timestamp'])
if len(events):
event = DBItem.create_object_from_db_entry(events[-1])
self.current_song = event
self.debug('loded current song: {}', self.current_song)
if self.song_queue:
self.play_song()
def save_state_to_db(self, db):
db_dict = {
'type': self.TAG,
'queue': [str(item.uid) for item in self.song_queue]
}
if db.search(where('type') == self.TAG):
db.update(db_dict, where('type') == self.TAG)
else:
db.insert(db_dict)
def get_next_songs(self):
first = None
next = None
if len(self.song_queue) > 0:
first = self.song_queue[0]
if len(self.song_queue) > 1:
next = self.song_queue[1]
return first, next
@asyncio.coroutine
def play_later(self, delay):
song_one, song_two = self.get_next_songs()
self.debug("Playing {} in {} seconds.", song_one, delay)
yield from asyncio.sleep(delay)
song_one, song_two = self.get_next_songs()
while not self.backlog_processed:
self.verbose("Backlog not done, waiting")
yield from asyncio.sleep(0.5)
self.expecting_song = True
yield from self.send_action(PlayOneSongAction(song_one, song_two))
#yield from self.action_queue.put(PlayOneSongAction(song_one, song_two))
def play_song(self):
if self.closing:
return
if self.play_callback:
self.play_callback.cancel()
delay = 0
if self.current_song:
delay = self.current_song.remaining_duration()
if self.expecting_song:
delay += 3
self.play_callback = asyncio.get_event_loop().create_task(
self.play_later(delay)
)
def handle_play_event(self, play):
self.current_song = play
if self.song_queue and self.song_queue[0].youtube_info == play.youtube_info:
self.debug('Song matches first song in queue, popping item: {}', self.song_queue[0])
self.song_queue.pop(0)
return
for qcommand in self.song_queue:
if play.timestamp > qcommand.timestamp\
and play.youtube_info == qcommand.youtube_info:
self.debug('Play event can satisfy song in queue and so removing out of order queue event: {}', qcommand)
self.song_queue.remove(qcommand)
break
@asyncio.coroutine
def handle_event(self, message):
if not message.is_prepared():
return
self.verbose('Got Message: {}', message.DB_TAG)
reply_to = message.uid
action = None
if self.current_song and self.current_song.remaining_duration() == 0:
self.current_song = None
if QueueCommand.DB_TAG == message.DB_TAG:
self.song_queue.append(message)
action = QueuedNotificationAction(self.song_queue, self.current_song, message.youtube_info, reply_to)
elif QueueFirstCommand.DB_TAG == message.DB_TAG:
self.song_queue.insert(0, message)
# null queue as its first
action = QueuedNotificationAction([], self.current_song, message.youtube_info, reply_to)
elif PlayEvent.DB_TAG in message.DB_TAG:
self.expecting_song = False
self.handle_play_event(message)
elif SkipCommand.DB_TAG in message.DB_TAG:
self.current_song = None
self.expecting_song = False
elif DramaticSkipCommand.DB_TAG in message.DB_TAG:
action = ReplyAction("!play https://www.youtube.com/watch?v=a1Y73sPHKxw")
elif ClearQueueCommand.DB_TAG in message.DB_TAG:
self.song_queue = []
elif ListQueueCommand.DB_TAG in message.DB_TAG:
action = ListQueueAction(self.song_queue, self.current_song, reply_to)
elif DumpQueueCommand.DB_TAG in message.DB_TAG:
action = DumpQueue(self.song_queue)
self.song_queue = []
if action:
if not self.backlog_processed:
self.verbose('In backlog, would have sent: {}', action)
else:
yield from self.send_action(action)
self.play_song()
self.save_state_to_db(self.db)
self.debug(self.status_string())