forked from d-dd/Yukari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyClient.py
665 lines (586 loc) · 25.4 KB
/
cyClient.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import database, apiClient, tools, vdbapi
from tools import clog
from conf import config
import json, time, re, argparse
from collections import deque
from twisted.internet import reactor, defer, task
from twisted.internet.error import AlreadyCalled, AlreadyCancelled
from autobahn.twisted.websocket import WebSocketClientProtocol,\
WebSocketClientFactory
sys = 'CytubeClient'
vdb = config['UserAgent']['vocadb']
class CyProtocol(WebSocketClientProtocol):
def __init__(self):
self.votes = 0
self.unloggedChat = []
self.lastChatLogTime = 0
self.receivedChatBuffer = False
self.queueMediaList = deque()
self.canBurst = False
self.lastQueueTime = time.time() - 20 #TODO
### Need to imporve this regex, it matches non-videos
# ie https://www.youtube.com/feed/subscriptions
self.ytUrl = re.compile(
(r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.'
'(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'))
self.ytq = deque()
def onOpen(self):
clog.info('(onOpen) Connected to Cytube!', sys)
self.connectedTime = time.time()
self.lastUserlistTime = 0
self.factory.prot = self
self.factory.handle.cy = True
self.initialize()
def onMessage(self, msg, binary):
if msg == '2::':
self.sendMessage(msg) # return heartbeat
elif msg.startswith('5:::{'):
fstr = msg[4:]
fdict = json.loads(fstr)
#if fdict['name'] in ('chatMsg', 'userlist', 'addUser', 'userLeave'):
#print fstr
self.processFrame(fdict)
def onClose(self, wasClean, code, reason):
clog.info('(onClose) Closed Protocol connection: %s' % reason, sys)
def sendf(self, dict): # 'sendFrame' is a WebSocket method name
frame = json.dumps(dict)
frame = '5:::' + frame
clog.debug('(sendf) [->] %s' % frame, sys)
self.sendMessage(frame)
def doSendChat(self, msg, modflair=False):
if modflair:
modflair = 3 ### TODO remove hardcode rank
self.sendf({'name': 'chatMsg',
'args': {'msg': msg, 'meta': {'modflair': modflair}}})
def initialize(self):
self.sendf({'name': 'initChannelCallbacks'})
name = config['Cytube']['username']
pw = config['Cytube']['password']
self.sendf({'name': 'login',
'args': {'name': name, 'pw': pw}})
def processFrame(self, fdict):
name = fdict['name']
# send to the appropriate methods
thunk = getattr(self, '_cyCall_%s' % (name,), None)
if thunk is not None:
thunk(fdict)
else:
pass
#print 'No method defined for %s.' % name
def joinRoom(self):
channel = config['Cytube']['channel']
self.sendf({'name': 'joinChannel', 'args': {'name':channel}})
def searchYoutube(self, msg):
m = self.ytUrl.search(msg)
if m:
ytId = m.group(6)
clog.debug('(searchYoutube) matched: %s' % ytId, sys)
d = apiClient.requestYtApi(ytId)
d.addCallbacks(self.doSendChat, self.errYtInfo)
def _cyCall_login(self, fdict):
if fdict['args'][0]['success']:
self.joinRoom()
def _cyCall_setMotd(self, fdict):
# setMotd comes after the chat buffer when joining a channel
self.receivedChatBuffer = True
def _cyCall_pm(self, fdict):
return # TODO
clog.info(fdict, sys)
username = fdict['args'][0]['username']
msg = fdict['args'][0]['msg']
if msg == '$down':
self.votes -= 1
elif msg == '$up':
self.votes += 1
else:
return
self.sendCss()
def sendCss(self):
return # TODO
hor = -16 * self.votes
css = '#votebg{background-position: %spx 0px;}' % hor
self.sendf({'name':'setChannelCSS', 'args':{'css':css}})
def _cyCall_chatMsg(self, fdict):
if not self.receivedChatBuffer:
return
args = fdict['args'][0]
timeNow = round(time.time(), 2)
username = args['username']
msg = tools.unescapeMsg(args['msg'])
chatCyTime = round((args['time'])/1000.0, 2)
if 'modflair' in args['meta']:
modflair = args['meta']['modflair']
else:
modflair = None
if username == '[server]':
keyId = 2
elif username in self.userdict:
keyId = self.userdict[username]['keyId']
if keyId:
self.unloggedChat.append((None, keyId, timeNow, chatCyTime, msg,
modflair, 0))
if time.time() - self.lastChatLogTime < 3:
self.cancelChatLog()
self.dChat = reactor.callLater(3, self.bulkLogChat,
self.unloggedChat)
else:
self.cancelChatLog()
self.bulkLogChat(self.unloggedChat)
self.lastChatLogTime = time.time()
else:
assert keyId is None
chatArgs = (timeNow, chatCyTime, msg, modflair, 0)
self.userdict[username]['deferred'].addCallback(self.deferredChat,
chatArgs)
if username != config['Cytube']['username'] and username != '[server]':
# comment line below for test. #TODO make proper test
self.factory.handle.recCyMsg(username, msg)
self.searchYoutube(msg)
# check for commands
if msg.startswith('$'):
command = msg.split()[0][1:]
clog.debug('received command %s from %s' % (command, username), sys)
args = tuple(msg.split()[1:])
thunk = getattr(self, '_com_%s' % (command,), None)
if thunk is not None:
thunk(username, msg)
def _com_vocadb(self, username, msg):
if not vdb:
return
try:
songId = int(msg.split()[1])
except IndexError:
clog.error('(_com_vocadb) Index Error by %s' % username, sys)
return
except ValueError:
clog.error('(_com_vocadb) Value Error by %s' % username, sys)
return
userId = self.userdict[username]['keyId']
timeNow = round(time.time(), 2)
mType, mId, mTitle = self.nowPlaying
d = vdbapi.requestSongById(mType, mId, songId, userId, timeNow, 4)
# method 4 = manual set
def _com_add(self, username, msg):
rank = self._getRank(username)
clog.info('rank is %s' % rank, 'RANK')
if not rank:
return
elif rank < 2:
maxAdd = 5
else:
maxAdd = 20
clog.info(msg, sys)
arg = list(msg.split()[1:])
# shortcut in case people want to $add #
# of course this can't be combined with other args
try:
num = int(arg[0])
arg = ['-n', str(num)]
except(ValueError, IndexError):
pass
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sample', default='queue',
choices=('queue', 'q', 'add', 'a'))
parser.add_argument('-u', '--user', default='Anyone')
parser.add_argument('-g', '--guest', default=False, type=bool)
parser.add_argument('-n', '--number', default=3, type=int)
parser.add_argument('-t', '--title', default='') #TODO
parser.add_argument('-a', '--artist', default='') #TODO
parser.add_argument('-T', '--temporary', default=False, type=bool)
parser.add_argument('-N', '--next', default=False, type=bool)
try:
args = parser.parse_args(arg)
except(SystemExit):
self.doSendChat('Invalid arguments.')
return
args.number = min(args.number, maxAdd)
reply = ('Quantity:%s, sample:%s, user:%s, guest:%s, temp:%s, '
'pos:%s'
% (args.number, args.sample, args.user, args.guest,
args.temporary, args.next))
self.doSendChat(reply)
isRegistered = not args.guest
if args.next:
args.next = 'next'
else:
args.next = 'end'
if args.user == 'Anyone':
args.user = None
self.getRandMedia(args.sample, args.number, args.user,
args.temporary, args.next)
def _com_omit(self, username, msg):
rank = self._getRank(username)
if rank < 2:
return
def _getRank(self, username):
try:
return int(self.userdict[username]['rank'])
except(KeyError):
clog.error('(_getRank) %s not found in userdict' % username, sys)
def cancelChatLog(self):
try:
self.dChat.cancel()
clog.debug('(cancelChatLog) Cancelled log timer', sys)
except AttributeError as e:
clog.debug('(cancelChatLog): %s' % e, sys)
except AlreadyCancelled as e:
clog.debug('(cancelChatLog): %s' % e, sys)
except AlreadyCalled as e:
clog.debug('(cancelChatLog): %s' % e, sys)
except NameError as e:
clog.error('(cancelChatLog): %s' % e, sys)
def bulkLogChat(self, chatlist):
assert self.unloggedChat == chatlist
self.unloggedChat = []
#print 'Logging %s !!' % chatlist
return database.bulkLogChat('cyChat', chatlist)
def deferredChat(self, res, chatArgs):
""" Logs chat to database. Since this will be added to the userAdd
deferred chain, we ensure that the user is given a keyId before database
insert. This will rarely be triggered since a user must join and
immediatly chat before they are queried or written to the user database.
res is the key id from lastrowid or itself"""
keyId = res
dd = database.insertChat(None, keyId, *chatArgs)
# this inner deferred ensures that the outer deferred's response
# is always a keyId, in case we need to keep chaining more chat log
# callbacks.
dd.addCallback(self.deferredChatRes, keyId)
def deferredChatRes(self, res, key):
if not res:
clog.info('(deferredChatRes): wrote chat to database!', sys)
return defer.succeed(key)
else:
clog.err('(deferredChatRes): error writing to database!', sys)
return defer.fail(key)
def _cyCall_addUser(self, fdict):
user = fdict['args'][0]
timeNow = int(time.time())
if user['name'] not in self.userdict:
self.userJoin(user, timeNow)
self.userdict[user['name']]['inChannel'] = True
def userJoin(self, user, timeNow):
user['keyId'] = None
user['timeJoined'] = timeNow
self.userdict[user['name']] = user
reg = self.checkRegistered(user['name'])
d = database.dbQuery(('userId', 'flag', 'lastSeen'), 'cyUser',
nameLower=user['name'].lower(), registered=reg)
d.addCallback(database.queryResult)
values = (None, user['name'].lower(), reg, user['name'], 0, 0,
timeNow, timeNow, 0)
d.addErrback(database.dbInsertReturnLastRow, 'cyUser', *values)
d.addCallback(self.cacheKey, user)
# add a reference to the deferred to the userdict
self.userdict[user['name']]['deferred'] = d
def cacheKey(self, res, user):
assert res, 'no res at cacheKey'
if res:
clog.info("(cacheKey) cached %s's key %s" % (user['name'], res[0]),
sys)
self.userdict[user['name']]['keyId'] = res[0]
def _cyCall_userLeave(self, fdict):
username = fdict['args'][0]['name']
if not username:
return # when anon leaves, might be sync bug
self.userdict[username]['inChannel'] = False
d = self.userdict[username]['deferred']
clog.debug('_cyCall_userLeave) user %s has left. Adding callbacks'
% username, sys)
leftUser = self.userdict[username]
d.addCallback(self.clockUser, leftUser,
int(time.time()))
d.addErrback(self.dbErr)
d.addCallback(self.removeUser, username)
d.addErrback(self.dbErr)
def removeUser(self, res, username):
clog.debug('(removeUser) Removing user', sys)
try:
if not self.userdict[username]['inChannel']:
del self.userdict[username]
clog.debug('(removeUser) deleted %s' % username, sys)
else:
clog.error('(removeUser) skipping: user %s in channel' % username, sys)
except(KeyError):
clog.error('(removeUser) Failed: user %s not in userdict' % username, sys)
return KeyError
def _cyCall_userlist(self, fdict):
if time.time() - self.lastUserlistTime < 3: # most likely the same userlist
# with ip/aliases if mod+
clog.info('(_cy_userlist) Duplicate userlist detected', sys)
return
self.lastUserlistTime = time.time()
userlist = fdict['args'][0]
timeNow = int(time.time())
# make a dictonary of users
self.userdict = {}
for user in userlist:
user['timeJoined'] = timeNow
self.userdict[user['name']] = user
self.userJoin(user, timeNow)
def _cyCall_playlist(self, fdict):
""" Cache the playlist in memory, and write them to the media table """
# Don't add this to the queue table, since it'll end up adding
# multiple times each join/restart and also during shuffle and clear.
self.playlist = []
pl = fdict['args'][0]
clog.debug('(_cyCall_playlist) received playlist from Cytube', sys)
dbpl, qpl = [], []
for entry in pl:
self.playlist.append(entry)
if entry['media']['type'] != 'cu': # custom embed
dbpl.append((None, entry['media']['type'], entry['media']['id'],
entry['media']['seconds'], entry['media']['title'],
1, 0))
#'introduced by' Yukari
if entry['media']['type'] == 'yt':
qpl.append((entry['media']['type'], entry['media']['id']))
d = database.bulkLogMedia(dbpl)
d.addCallback(self.bulkCheckPlaylist, self.playlist)
if vdb:
d.addCallback(database.bulkQueryMediaSong, qpl)
d.addCallback(self.bulkCheckVocaDb)
def bulkCheckPlaylist(self, res, playlist):
for media in playlist:
mType = media['media']['type']
mId = media['media']['id']
d = self.checkMedia(0, mType, mId)
d.addCallback(self.flagOrDelete, media['media'], mType, mId)
def bulkCheckVocaDb(self, songlessMedia):
timeNow = round(time.time(), 4)
clog.info('(bulkCheckVocaDb)', sys)
for i, (mType, mId) in enumerate(songlessMedia):
# 0.5s delay between each call
reactor.callLater(i * 0.5, vdbapi.requestSongByPv, None, mType, mId,
1, timeNow, 4)
def addToPlaylist(self, item, afterUid):
if afterUid == 'prepend':
index = 0
else:
index = self.getIndexFromUid(afterUid)
self.playlist.insert(index + 1, item)
# I want to print media['title'] but depending on the terminal
# it fails to encode some characters (usually symbols)
clog.debug('(addToPlaylist) Inserting uid %s %s after index %s' %
(item['uid'], item['media']['title'].encode('utf-8'),
index), sys)
def movePlaylistItems(self, beforeUid, afterUid):
# 'before' is just the uid of the video that is going to move
clog.info('(movePlaylistItems) move uid:%s, after uid%s' %
(beforeUid, afterUid), sys)
if afterUid == 'prepend':
indexAfter = 0
else:
indexAfter = self.getIndexFromUid(afterUid)
indexBefore = self.getIndexFromUid(beforeUid)
if indexBefore > indexAfter and afterUid != 'prepend':
indexAfter += 1
self.playlist.insert(indexAfter, self.playlist.pop(indexBefore))
def getIndexFromUid(self, uid):
""" Return video index of self.playlist given an UID """
try:
media = (i for i in self.playlist if i['uid'] == uid).next()
index = self.playlist.index(media)
clog.debug('(getIndexFromUid) Looking up uid %s, index is %s' % (uid, index), sys)
return index
except StopIteration as e:
clog.error('(getIndexFromUid) media UID %s not found' % uid, sys)
def getUidFromTypeId(self, mType, mId):
for media in self.playlist:
if media['media']['id'] == mId:
if media['media']['type'] == mType:
return media['uid']
def displaypl(self):
for item in self.playlist:
print item['media']['title'].encode('utf-8')
def _cyCall_queue(self, fdict):
timeNow = round(time.time(), 2)
item = fdict['args'][0]['item']
isTemp = item['temp']
media = item['media']
queueby = item['queueby']
afterUid = fdict['args'][0]['after']
mType = media['type']
mId = media['id']
self.addToPlaylist(item, afterUid)
if queueby: # anonymous add is an empty string
userId = self.userdict[queueby]['keyId']
else:
userId = 3
if userId:
d = self.queryOrInsertMedia(media, userId)
else:
clog.error('(_cyCall_queue) user id not cached.', sys)
return
if isTemp:
flag = 1
else:
flag = None
d.addCallback(self.writeQueue, userId, timeNow, flag)
d.addCallback(self.checkMedia, mType, mId)
d.addCallback(self.flagOrDelete, media, mType, mId)
if mType == 'yt' and vdb:
timeNow = round(time.time(), 2)
# since this callback is added after checkMedia which has a delay,
# this also gets delayed
d.addCallback(vdbapi.requestSongByPv ,mType, mId, 1, timeNow, 0)
def checkMedia(self, res, mType, mId):
if mType == 'yt':
self.ytq.append(mId)
d = task.deferLater(reactor, 1 * (len(self.ytq)-1),
self.collectYtQueue, mId)
clog.debug('(checkMedia) Length of ytq %s' % len(self.ytq), sys)
return d
else:
return defer.succeed('EmbedOk') # TODO
def flagOrDelete(self, res, media, mType, mId):
if res == 'EmbedOk':
database.unflagMedia(0b1, mType, mId)
elif res == 'Status503':
clog.error('(flagOrDelete) Youtube service unavailable.', sys)
elif res == 'NetworkError':
clog.error('(flagOrDelete) There was a network error.', sys)
elif res in ('NoEmbed', 'Status403', 'Status404'):
self.doDeleteMedia(media['type'], media['id'])
mediaTitle = media['title'].encode('utf-8')
msg = 'Removing non-playable media %s' % mediaTitle
database.flagMedia(0b1, mType, mId)
self.doSendChat(msg)
clog.info(msg)
def collectYtQueue(self, mId):
# need another function because popleft() evaluates immediatly
return apiClient.requestYtApi(self.ytq.popleft(), 'check')
def _cyCall_delete(self, fdict):
uid = fdict['args'][0]['uid']
index = self.getIndexFromUid(uid)
deletedMedia = self.playlist.pop(index)
clog.info('(_cyCall_delete) Removed uid %s, index %s from my playlist' %
(uid, index), sys)
assert uid == deletedMedia['uid'], 'Deleted media not correct!'
def queryOrInsertMedia(self, media, userId):
d = database.dbQuery(('mediaId',) , 'Media', type=media['type'], id=media['id'])
d.addCallback(database.queryResult)
values = (None, media['type'], media['id'], media['seconds'],
media['title'], userId, 0)
d.addErrback(database.dbInsertReturnLastRow, 'Media', *values)
return d
def writeQueue(self, res, userId, timeNow, flag):
""" Insert queue into Queue. """
# res is the [mediaId]
return database.insertQueue(res[0], userId, timeNow, flag)
def printRes(self, res):
clog.info('(printRes) %s' % res, sys)
return defer.succeed(res)
def errYtInfo(self, err):
clog.error('(errYtInfo) %s' % err, sys)
def dbErr(self, err):
clog.error('(dbErr): %s' % err.value, sys)
def _cyCall_changeMedia(self, fdict):
# set self.nowPlaying
mType = fdict['args'][0]['type']
mId = fdict['args'][0]['id']
mTitle = fdict['args'][0]['title']
self.nowPlaying = (mType, mId, mTitle) # these are unicode
# everything has to be encoded to utf-8 or it errors
s = mTitle.encode('utf-8') + ' (%s, %s)' % (mType.encode('utf-8'),
mId.encode('utf-8'))
clog.info('(_cyCall_changeMedia) %s' % s, sys)
def _cyCall_moveVideo(self, fdict):
beforeUid = fdict['args'][0]['from']
afterUid = fdict['args'][0]['after']
self.movePlaylistItems(beforeUid, afterUid)
def _cyCall_queueFail(self, fdict):
msg = fdict['args'][0]['msg']
clog.error('(_cyCall_queueFail) %s' % msg, sys)
def cleanUp(self):
# set restart to False
self.factory.handle.cyRestart = False
# disconnect first so we don't get any more join/leaves
self.sendClose()
# log everyone's access time before shutting down
timeNow = int(time.time())
for name, user in self.userdict.iteritems():
user['deferred'].addCallback(self.clockUser, user, timeNow)
self.cyRestart = False
def clockUser(self, res, leftUser, timeNow):
""" Clock out a user, by updating their accessTime """
username = leftUser['name']
clog.info('(clockUser) Clocking out %s!' % username, sys)
timeJoined = leftUser['timeJoined']
timeStayed = timeNow - timeJoined
userId = leftUser['keyId']
return database.updateCyUser(timeNow, timeStayed, userId)
def checkRegistered(self, username):
""" Return wether a Cytube user is registered (1) or a guest (0) given
a username. Checks self.userdict for rank information."""
if username == '[server]':
return 1
else:
try:
user = self.userdict[username]
except KeyError as e:
clog.error('(checkRegistered): %s' % e, sys)
#raise
if user['rank'] == 0:
return 0
else:
return 1
def doAddMedia(self, media, temp, pos):
# Cytube has a throttle for queueing media
# burst: 10,
# sustained: 2
lenBefore = len(self.queueMediaList)
self.queueMediaList.extend(media)
if time.time() - self.lastQueueTime > 20:
self.canBurst = True
# burst!
bursted = 0
if self.canBurst and self.queueMediaList:
for i in range(min(len(self.queueMediaList), 10)):
mType, mId = self.queueMediaList.popleft()
self.sendf({'name': 'queue', 'args': {'type': mType,
'id': mId, 'pos': pos, 'temp': temp}})
bursted += 1
self.canBurst = False
self.lastQueueTime = time.time()
# sustain
# we can't use enumerate here; the list gets shorter each iteration
for i in range(len(media)-bursted):
# add a little slower than 2/sec to account for network latency
# and Twisted callLater time is not guaranteed
wait = (i + lenBefore + 3)/1.7
d = reactor.callLater(wait, self.doAddSustained, pos, temp)
def doAddSustained(self, pos, temp):
mType, mId = self.queueMediaList.popleft()
self.sendf({'name': 'queue', 'args': {'type': mType,
'id': mId, 'pos': pos, 'temp': temp}})
self.lastQueueTime = time.time()
def getRandMedia(self, sample, quantity, user, temp, pos):
""" Queues up to quantity number of media to the playlist """
if sample == 'queue' or sample == 'q':
d = database.addByUserQueue(user, quantity)
elif sample == 'add' or sample == 'a':
d = database.addByUserAdd(user, quantity)
else:
return
d.addCallback(self.doAddMedia, temp, pos)
def doDeleteMedia(self, mType, mId):
""" Delete media """
uid = self.getUidFromTypeId(mType, mId)
clog.info('(doDeleteMedia) Deleting media uid %s' % uid)
self.sendf({'name': 'delete', 'args': uid})
class WsFactory(WebSocketClientFactory):
protocol = CyProtocol
def __init__(self, arg):
WebSocketClientFactory.__init__(self, arg)
def clientConnectionLost(self, connector, reason):
clog.warning('(clientConnectionLost) Connection lost to Cyutbe. %s'
% reason, sys)
if not self.handle.cyRestart:
self.handle.doneCleanup('cy')
else:
#self.handle.cyPost() # reconnect
self.handle.doneCleanup('cy')
def clientConnectionFailed(self, connector, reason):
clog.error('(clientConnectionFailed) Connection failed to Cytube. %s'
% reason, sys)