forked from nortxort/tinybot-rtc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinybot.py
More file actions
1785 lines (1520 loc) · 72.6 KB
/
tinybot.py
File metadata and controls
1785 lines (1520 loc) · 72.6 KB
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
""" Tinybot by Nortxort (https://github.com/nortxort/tinybot-rtc) """
import logging
import threading
import pinylib
from util import tracklist
from page import privacy
from apis import youtube, lastfm, other, locals_
import check_user
__version__ = '2.0.3'
log = logging.getLogger(__name__)
class TinychatBot(pinylib.TinychatRTCClient):
privacy_ = None
timer_thread = None
playlist = tracklist.PlayList()
search_list = []
is_search_list_yt_playlist = False
bl_search_list = []
@property
def config_path(self):
""" Returns the path to the rooms configuration directory. """
return pinylib.CONFIG.CONFIG_PATH + self.room_name + '/'
def user_check(self, user, account=False, guest=False, nick=False, lurker=False):
"""
A wrapper for the CheckUser class.
:return: True, if the user was banned.
:rtype: bool
"""
if not self.is_client_mod:
return False
judge = check_user.CheckUser(self, user, pinylib.CONFIG)
if account and user.account != '':
log.debug('checking account: %s' % user.account)
if not user.is_mod and judge.check_account():
return True
api = pinylib.apis.tinychat.user_info(user.account)
if api is not None:
user.tinychat_id = api['tinychat_id']
user.last_login = api['last_active']
if guest:
log.debug('checking guest entrance: %s' % user.nick)
if judge.guest_entry():
return True
if nick and user.nick:
log.debug('checking nick: %s' % user.nick)
if not user.is_mod and judge.check_nick():
return True
if lurker and user.is_lurker:
log.debug('check lurker: %s' % user.nick)
if judge.check_lurker():
return True
return False
def on_joined(self, client_info):
"""
Received when the client have joined the room successfully.
:param client_info: This contains info about the client, such as user role and so on.
:type client_info: dict
"""
log.info('client info: %s' % client_info)
self.client_id = client_info['handle']
self.is_client_mod = client_info['mod']
self.is_client_owner = client_info['owner']
client = self.users.add(client_info)
client.user_level = 0
self.console_write(pinylib.COLOR['bright_green'], 'Client joined the room: %s:%s' % (client.nick, client.id))
# do special operations.
threading.Thread(target=self.options).start()
def on_join(self, join_info):
"""
Received when a user joins the room.
:param join_info: This contains user information such as role, account and so on.
:type join_info: dict
"""
log.info('user join info: %s' % join_info)
_user = self.users.add(join_info)
if _user.account:
if _user.is_owner:
_user.user_level = 1
self.console_write(pinylib.COLOR['red'], 'Room Owner %s:%d:%s' %
(_user.nick, _user.id, _user.account))
elif _user.is_mod:
_user.user_level = 3
self.console_write(pinylib.COLOR['bright_red'], 'Moderator %s:%d:%s' %
(_user.nick, _user.id, _user.account))
else:
self.console_write(pinylib.COLOR['bright_yellow'], '%s:%d has account: %s' %
(_user.nick, _user.id, _user.account))
if not self.user_check(_user, account=True, guest=True, nick=True, lurker=True):
if pinylib.CONFIG.B_GREET and self.is_client_mod:
if not _user.nick.startswith('guest-'):
if _user.account:
self.send_chat_msg('Welcome to the room %s:%s:%s' %
(_user.nick, _user.id, _user.account))
else:
self.send_chat_msg('Welcome to the room %s:%s' % (_user.nick, _user.id))
self.console_write(pinylib.COLOR['cyan'], '%s:%d joined the room.' % (_user.nick, _user.id))
def on_nick(self, uid, nick):
"""
Received when a user changes nick name.
:param uid: The ID (handle) of the user.
:type uid: int
:param nick: The new nick name.
:type nick: str
"""
_user = self.users.search(uid)
old_nick = _user.nick
_user.nick = nick
if uid != self.client_id:
if not self.user_check(_user, nick=True):
if pinylib.CONFIG.B_GREET and self.is_client_mod:
if old_nick.startswith('guest-'):
if _user.account:
self.send_chat_msg('Welcome to the room %s:%s:%s' %
(_user.nick, _user.id, _user.account))
else:
self.send_chat_msg('Welcome to the room %s:%s' % (_user.nick, _user.id))
self.console_write(pinylib.COLOR['bright_cyan'], '%s:%s Changed nick to: %s' % (old_nick, uid, nick))
def on_yut_play(self, yt_data):
"""
Received when a youtube gets started or time searched.
This also gets received when the client starts a youtube, the information is
however ignored in that case.
:param yt_data: The event information contains info such as the ID (handle) of the user
starting/searching the youtube, the youtube ID, youtube time and so on.
:type yt_data: dict
"""
if self.playlist.has_active_track:
self.cancel_timer()
track = youtube.video_details(yt_data['item']['id'], False)
if 'handle' in yt_data:
if yt_data['handle'] != self.client_id:
_user = self.users.search(yt_data['handle'])
if yt_data['item']['offset'] == 0:
self.playlist.start(_user.nick, track)
self.timer(track.time)
self.console_write(pinylib.COLOR['bright_magenta'], '%s started youtube video (%s)' %
(_user.nick, track.title))
elif yt_data['item']['offset'] > 0:
offset = self.playlist.play(yt_data['item']['offset'])
self.timer(offset)
self.console_write(pinylib.COLOR['bright_magenta'], '%s searched the youtube video to: %s' %
(_user.nick, int(round(yt_data['item']['offset']))))
else:
if yt_data['item']['offset'] > 0:
self.playlist.start('started before joining.', track)
offset = self.playlist.play(yt_data['item']['offset'])
self.timer(offset)
def on_yut_pause(self, yt_data):
"""
Received when a youtube gets paused or searched while paused.
This also gets received when the client pauses or searches while paused, the information is
however ignored in that case.
:param yt_data: The event information contains info such as the ID (handle) of the user
pausing/searching the youtube, the youtube ID, youtube time and so on.
:type yt_data: dict
"""
if self.playlist.has_active_track:
self.cancel_timer()
self.playlist.pause()
if 'handle' in yt_data:
if yt_data['handle'] != self.client_id:
_user = self.users.search(yt_data['handle'])
self.console_write(pinylib.COLOR['bright_magenta'], '%s paused the video at %s' %
(_user.nick, int(round(yt_data['item']['offset']))))
else:
log.info('no handle for youtube pause: %s' % yt_data)
def message_handler(self, msg):
"""
A basic handler for chat messages.
Overrides message_handler in pinylib
to allow commands.
:param msg: The chat message.
:type msg: str
"""
prefix = pinylib.CONFIG.B_PREFIX
if msg.startswith(prefix):
parts = msg.split(' ')
cmd = parts[0].lower().strip()
cmd_arg = ' '.join(parts[1:]).strip()
if self.has_level(1):
if self.is_client_owner:
if cmd == prefix + 'mod':
threading.Thread(target=self.do_make_mod, args=(cmd_arg,)).start()
elif cmd == prefix + 'rmod':
threading.Thread(target=self.do_remove_mod, args=(cmd_arg,)).start()
elif cmd == prefix + 'dir':
threading.Thread(target=self.do_directory).start()
elif cmd == prefix + 'p2t':
threading.Thread(target=self.do_push2talk).start()
elif cmd == prefix + 'crb':
threading.Thread(target=self.do_clear_room_bans).start()
if cmd == prefix + 'kill':
self.do_kill()
elif cmd == prefix + 'reboot':
self.do_reboot()
if self.has_level(2):
if cmd == prefix + 'mi':
self.do_media_info()
if self.has_level(3):
if cmd == prefix + 'op':
self.do_op_user(cmd_arg)
elif cmd == prefix + 'deop':
self.do_deop_user(cmd_arg)
elif cmd == prefix + 'noguest':
self.do_guests()
elif cmd == prefix + 'lurkers':
self.do_lurkers()
elif cmd == prefix + 'guestnick':
self.do_guest_nicks()
elif cmd == prefix + 'greet':
self.do_greet()
elif cmd == prefix + 'pub':
self.do_public_cmds()
elif cmd == prefix + 'kab':
self.do_kick_as_ban()
elif cmd == prefix + 'rs':
self.do_room_settings()
elif cmd == prefix + 'top':
threading.Thread(target=self.do_lastfm_chart, args=(cmd_arg,)).start()
elif cmd == prefix + 'ran':
threading.Thread(target=self.do_lastfm_random_tunes, args=(cmd_arg,)).start()
elif cmd == prefix + 'tag':
threading.Thread(target=self.do_search_lastfm_by_tag, args=(cmd_arg,)).start()
elif cmd == prefix + 'pls':
threading.Thread(target=self.do_youtube_playlist_search, args=(cmd_arg,)).start()
elif cmd == prefix + 'plp':
threading.Thread(target=self.do_play_youtube_playlist, args=(cmd_arg,)).start()
elif cmd == prefix + 'ssl':
self.do_show_search_list()
if self.has_level(4):
if cmd == prefix + 'skip':
self.do_skip()
elif cmd == prefix + 'del':
self.do_delete_playlist_item(cmd_arg)
elif cmd == prefix + 'rpl':
self.do_media_replay()
elif cmd == prefix + 'mbpl':
self.do_play_media()
elif cmd == prefix + 'mbpa':
self.do_media_pause()
elif cmd == prefix + 'seek':
self.do_seek_media(cmd_arg)
elif cmd == prefix + 'cm':
self.do_close_media()
elif cmd == prefix + 'cpl':
self.do_clear_playlist()
elif cmd == prefix + 'spl':
self.do_playlist_info()
elif cmd == prefix + 'yts':
threading.Thread(target=self.do_youtube_search, args=(cmd_arg,)).start()
elif cmd == prefix + 'pyts':
self.do_play_youtube_search(cmd_arg)
elif cmd == prefix + 'clr':
self.do_clear()
elif cmd == prefix + 'nick':
self.do_nick(cmd_arg)
elif cmd == prefix + 'kick':
threading.Thread(target=self.do_kick, args=(cmd_arg,)).start()
elif cmd == prefix + 'ban':
threading.Thread(target=self.do_ban, args=(cmd_arg,)).start()
elif cmd == prefix + 'bn':
self.do_bad_nick(cmd_arg)
elif cmd == prefix + 'rmbn':
self.do_remove_bad_nick(cmd_arg)
elif cmd == prefix + 'bs':
self.do_bad_string(cmd_arg)
elif cmd == prefix + 'rmbs':
self.do_remove_bad_string(cmd_arg)
elif cmd == prefix + 'ba':
self.do_bad_account(cmd_arg)
elif cmd == prefix + 'rmba':
self.do_remove_bad_account(cmd_arg)
elif cmd == prefix + 'list':
self.do_list_info(cmd_arg)
elif cmd == prefix + 'uinfo':
self.do_user_info(cmd_arg)
elif cmd == prefix + 'cam':
self.do_cam_approve(cmd_arg)
elif cmd == prefix + 'close':
self.do_close_broadcast(cmd_arg)
elif cmd == prefix + 'sbl':
self.do_banlist_search(cmd_arg)
elif cmd == prefix + 'fg':
self.do_forgive(cmd_arg)
elif cmd == prefix + 'unb':
self.do_unban(cmd_arg)
if (pinylib.CONFIG.B_PUBLIC_CMD and self.has_level(5)) or self.active_user.user_level < 5:
if cmd == prefix + 'v':
self.do_version()
elif cmd == prefix + 'help':
self.do_help()
elif cmd == prefix + 't':
self.do_uptime()
elif cmd == prefix + 'yt':
threading.Thread(target=self.do_play_youtube, args=(cmd_arg,)).start()
elif cmd == prefix + 'q':
self.do_playlist_status()
elif cmd == prefix + 'n':
self.do_next_tune_in_playlist()
elif cmd == prefix + 'np':
self.do_now_playing()
elif cmd == prefix + 'wp':
self.do_who_plays()
# Tinychat API commands.
elif cmd == prefix + 'acspy':
threading.Thread(target=self.do_account_spy, args=(cmd_arg,)).start()
# Other API commands.
elif cmd == prefix + 'urb':
threading.Thread(target=self.do_search_urban_dictionary, args=(cmd_arg,)).start()
elif cmd == prefix + 'wea':
threading.Thread(target=self.do_weather_search, args=(cmd_arg,)).start()
elif cmd == prefix + 'ip':
threading.Thread(target=self.do_whois_ip, args=(cmd_arg,)).start()
# Just for fun.
elif cmd == prefix + 'cn':
threading.Thread(target=self.do_chuck_noris).start()
elif cmd == prefix + '8ball':
self.do_8ball(cmd_arg)
elif cmd == prefix + 'roll':
self.do_dice()
elif cmd == prefix + 'flip':
self.do_flip_coin()
if cmd == prefix + 'pmme':
self.do_pmme()
# Print command to console.
self.console_write(pinylib.COLOR['yellow'], self.active_user.nick + ': ' + cmd + ' ' + cmd_arg)
else:
# Print chat message to console.
self.console_write(pinylib.COLOR['green'], self.active_user.nick + ': ' + msg)
# Only check chat msg for ban string if we are mod.
if self.is_client_mod and self.active_user.user_level > 4:
threading.Thread(target=self.check_msg, args=(msg,)).start()
self.active_user.last_msg = msg
# Level 1 Command methods.
def do_make_mod(self, account):
"""
Make a tinychat account a room moderator.
:param account: The account to make a moderator.
:type account: str
"""
if self.is_client_owner:
if len(account) is 0:
self.send_chat_msg('Missing account name.')
else:
tc_user = self.privacy_.make_moderator(account)
if tc_user is None:
self.send_chat_msg('The account is invalid.')
elif not tc_user:
self.send_chat_msg('%s is already a moderator.' % account)
elif tc_user:
self.send_chat_msg('%s was made a room moderator.' % account)
def do_remove_mod(self, account):
"""
Removes a tinychat account from the moderator list.
:param account: The account to remove from the moderator list.
:type account: str
"""
if self.is_client_owner:
if len(account) is 0:
self.send_chat_msg('Missing account name.')
else:
tc_user = self.privacy_.remove_moderator(account)
if tc_user:
self.send_chat_msg('%s is no longer a room moderator.' % account)
elif not tc_user:
self.send_chat_msg('%s is not a room moderator.' % account)
def do_directory(self):
""" Toggles if the room should be shown on the directory. """
if self.is_client_owner:
if self.privacy_.show_on_directory():
self.send_chat_msg('Room IS shown on the directory.')
else:
self.send_chat_msg('Room is NOT shown on the directory.')
def do_push2talk(self):
""" Toggles if the room should be in push2talk mode. """
if self.is_client_owner:
if self.privacy_.set_push2talk():
self.send_chat_msg('Push2Talk is enabled.')
else:
self.send_chat_msg('Push2Talk is disabled.')
def do_green_room(self):
""" Toggles if the room should be in greenroom mode. """
if self.is_client_owner:
if self.privacy_.set_greenroom():
self.send_chat_msg('Green room is enabled.')
else:
self.send_chat_msg('Green room is disabled.')
def do_clear_room_bans(self):
""" Clear all room bans. """
if self.is_client_owner:
if self.privacy_.clear_bans():
self.send_chat_msg('All room bans was cleared.')
def do_kill(self):
""" Kills the bot. """
self.disconnect()
def do_reboot(self):
""" Reboots the bot. """
self.reconnect()
# Level 2 Command Methods.
def do_media_info(self):
""" Show information about the currently playing youtube. """
if self.is_client_mod and self.playlist.has_active_track:
self.send_chat_msg(
'Playlist Tracks: ' + str(len(self.playlist.track_list)) + '\n' +
'Track Title: ' + self.playlist.track.title + '\n' +
'Track Index: ' + str(self.playlist.track_index) + '\n' +
'Elapsed Track Time: ' + self.format_time(self.playlist.elapsed) + '\n' +
'Remaining Track Time: ' + self.format_time(self.playlist.remaining)
)
# Level 3 Command Methods.
def do_op_user(self, user_name):
"""
Lets the room owner, a mod or a bot controller make another user a bot controller.
:param user_name: The user to op.
:type user_name: str
"""
if self.is_client_mod:
if len(user_name) is 0:
self.send_chat_msg('Missing username.')
else:
_user = self.users.search_by_nick(user_name)
if _user is not None:
_user.user_level = 4
self.send_chat_msg('%s is now a bot controller (L4)' % user_name)
else:
self.send_chat_msg('No user named: %s' % user_name)
def do_deop_user(self, user_name):
"""
Lets the room owner, a mod or a bot controller remove a user from being a bot controller.
:param user_name: The user to deop.
:type user_name: str
"""
if self.is_client_mod:
if len(user_name) is 0:
self.send_chat_msg('Missing username.')
else:
_user = self.users.search_by_nick(user_name)
if _user is not None:
_user.user_level = 5
self.send_chat_msg('%s is not a bot controller anymore (L5)' % user_name)
else:
self.send_chat_msg('No user named: %s' % user_name)
def do_guests(self):
""" Toggles if guests are allowed to join the room or not. """
pinylib.CONFIG.B_ALLOW_GUESTS = not pinylib.CONFIG.B_ALLOW_GUESTS
self.send_chat_msg('Allow Guests: %s' % pinylib.CONFIG.B_ALLOW_GUESTS)
def do_lurkers(self):
""" Toggles if lurkers are allowed or not. """
pinylib.CONFIG.B_ALLOW_LURKERS = not pinylib.CONFIG.B_ALLOW_LURKERS
self.send_chat_msg('Allowe Lurkers: %s' % pinylib.CONFIG.B_ALLOW_LURKERS)
def do_guest_nicks(self):
""" Toggles if guest nicks are allowed or not. """
pinylib.CONFIG.B_ALLOW_GUESTS_NICKS = not pinylib.CONFIG.B_ALLOW_GUESTS_NICKS
self.send_chat_msg('Allow Guest Nicks: %s' % pinylib.CONFIG.B_ALLOW_GUESTS_NICKS)
def do_greet(self):
""" Toggles if users should be greeted on entry. """
pinylib.CONFIG.B_GREET = not pinylib.CONFIG.B_GREET
self.send_chat_msg('Greet Users: %s' % pinylib.CONFIG.B_GREET)
def do_public_cmds(self):
""" Toggles if public commands are public or not. """
pinylib.CONFIG.B_PUBLIC_CMD = not pinylib.CONFIG.B_PUBLIC_CMD
self.send_chat_msg('Public Commands Enabled: %s' % pinylib.CONFIG.B_PUBLIC_CMD)
def do_kick_as_ban(self):
""" Toggles if kick should be used instead of ban for auto bans . """
pinylib.CONFIG.B_USE_KICK_AS_AUTOBAN = not pinylib.CONFIG.B_USE_KICK_AS_AUTOBAN
self.send_chat_msg('Use Kick As Auto Ban: %s' % pinylib.CONFIG.B_USE_KICK_AS_AUTOBAN)
def do_room_settings(self):
""" Shows current room settings. """
if self.is_client_owner:
settings = self.privacy_.current_settings()
self.send_chat_msg(
'Broadcast Password: ' + settings['broadcast_pass'] + '\n' +
'Room Password: ' + settings['room_pass'] + '\n' +
'Login Type: ' + settings['allow_guest'] + '\n' +
'Directory: ' + settings['show_on_directory'] + '\n' +
'Push2Talk: ' + settings['push2talk'] + '\n' +
'Greenroom: ' + settings['greenroom']
)
def do_lastfm_chart(self, chart_items):
"""
Create a playlist from the most played tracks on last.fm.
:param chart_items: The maximum amount of chart items.
:type chart_items: str | int
"""
if self.is_client_mod:
if len(chart_items) == 0 or chart_items is None:
self.send_chat_msg('Please specify the max amount of tracks you want.')
else:
try:
chart_items = int(chart_items)
except ValueError:
self.send_chat_msg('Only numbers allowed.')
else:
if 0 < chart_items < 30:
self.send_chat_msg('Please wait while creating a playlist...')
_items = lastfm.chart(chart_items)
if _items is not None:
self.playlist.add_list(self.active_user.nick, _items)
self.send_chat_msg('Added ' + str(len(_items)) + 'tracks from last.fm chart.')
if not self.playlist.has_active_track:
track = self.playlist.next_track
self.send_yut_play(track.id, track.time, track.title)
self.timer(track.time)
else:
self.send_chat_msg('Failed to retrieve a result from last.fm.')
else:
self.send_chat_msg('No more than 30 tracks.')
def do_lastfm_random_tunes(self, max_tracks):
"""
Creates a playlist from what other people are listening to on last.fm
:param max_tracks: The miximum amount of tracks.
:type max_tracks: str | int
"""
if self.is_client_mod:
if len(max_tracks) == 0 or max_tracks is None:
self.send_chat_msg('Please specify the max amount of tunes you want.')
else:
try:
max_tracks = int(max_tracks)
except ValueError:
self.send_chat_msg('Only numbers allowed.')
else:
if 0 < max_tracks < 50:
self.send_chat_msg('Please wait while creating playlist...')
_items = lastfm.listening_now(max_tracks)
if _items is not None:
self.playlist.add_list(self.active_user.nick, _items)
self.send_chat_msg('Added ' + str(len(_items)) + 'tracks from last.fm')
if not self.playlist.has_active_track:
track = self.playlist.next_track
self.send_yut_play(track.id, track.time, track.title)
self.timer(track.time)
else:
self.send_chat_msg('Failed to retrieve a result from last.fm.')
else:
self.send_chat_msg('No more than 50 tracks.')
def do_search_lastfm_by_tag(self, search_str):
"""
Search last.fm for tunes matching a tag.
:param search_str: The search tag to search for.
:type search_str: str
"""
if self.is_client_mod:
if len(search_str) == 0 or search_str is None:
self.send_chat_msg('Missing search string.')
else:
self.send_chat_msg('Please wait while creating playlist..')
_items = lastfm.tag_search(search_str)
if _items is not None:
self.playlist.add_list(self.active_user.nick, _items)
self.send_chat_msg('Added ' + str(len(_items)) + 'tracks from last.fm')
if not self.playlist.has_active_track:
track = self.playlist.next_track
self.send_yut_play(track.id, track.time, track.title)
self.timer(track.time)
else:
self.send_chat_msg('Failed to retrieve a result from last.fm.')
def do_youtube_playlist_search(self, search_str):
"""
Search youtube for a playlist.
:param search_str: The search term to search for.
:type search_str: str
"""
if self.is_client_mod:
if len(search_str) == 0:
self.send_chat_msg('Missing search string.')
else:
self.search_list = youtube.playlist_search(search_str)
if len(self.search_list) > 0:
self.is_search_list_yt_playlist = True
_ = '\n'.join('(%s) %s' % (i, d['playlist_title']) for i, d in enumerate(self.search_list))
self.send_chat_msg(_)
else:
self.send_chat_msg('Failed to find playlist matching search term: %s' % search_str)
def do_play_youtube_playlist(self, int_choice):
"""
Play a previous searched playlist.
:param int_choice: The index of the playlist.
:type int_choice: str | int
"""
if self.is_client_mod:
if self.is_search_list_yt_playlist:
try:
int_choice = int(int_choice)
except ValueError:
self.send_chat_msg('Only numbers allowed.')
else:
if 0 <= int_choice <= len(self.search_list) - 1:
self.send_chat_msg('Please wait while creating playlist..')
tracks = youtube.playlist_videos(self.search_list[int_choice])
if len(tracks) > 0:
self.playlist.add_list(self.active_user.nick, tracks)
self.send_chat_msg('Added %s tracks from youtube playlist.' % len(tracks))
if not self.playlist.has_active_track:
track = self.playlist.next_track
self.send_yut_play(track.id, track.time, track.title)
self.timer(track.time)
else:
self.send_chat_msg('Failed to retrieve videos from youtube playlist.')
else:
self.send_chat_msg('Please make a choice between 0-%s' % str(len(self.search_list) - 1))
else:
self.send_chat_msg('The search list does not contain any youtube playlist id\'s.')
def do_show_search_list(self):
""" Show what the search list contains. """
if self.is_client_mod:
if len(self.search_list) == 0:
self.send_chat_msg('The search list is empty.')
elif self.is_search_list_yt_playlist:
_ = '\n'.join('(%s) - %s' % (i, d['playlist_title']) for i, d in enumerate(self.search_list))
self.send_chat_msg('Youtube Playlist\'s\n' + _)
else:
_ = '\n'.join('(%s) %s %s' % (i, d['video_title'], self.format_time(d['video_time']))
for i, d in enumerate(self.search_list))
self.send_chat_msg('Youtube Tracks\n' + _)
# Level 4 Command Methods.
def do_skip(self):
""" Skip to the next item in the playlist. """
if self.is_client_mod:
if self.playlist.is_last_track is None:
self.send_chat_msg('No tunes to skip. The playlist is empty.')
elif self.playlist.is_last_track:
self.send_chat_msg('This is the last track in the playlist.')
else:
self.cancel_timer()
next_track = self.playlist.next_track
self.send_yut_play(next_track.id, next_track.time, next_track.title)
self.timer(next_track.time)
def do_delete_playlist_item(self, to_delete): # TODO: Make sure this is working.
"""
Delete items from the playlist.
:param to_delete: Item indexes to delete.
:type to_delete: str
"""
if self.is_client_mod:
if len(self.playlist.track_list) == 0:
self.send_chat_msg('The playlist is empty.')
elif len(to_delete) == 0:
self.send_chat_msg('No indexes provided.')
else:
indexes = None
by_range = False
try:
if ':' in to_delete:
range_indexes = map(int, to_delete.split(':'))
temp_indexes = range(range_indexes[0], range_indexes[1] + 1)
if len(temp_indexes) > 1:
by_range = True
else:
temp_indexes = map(int, to_delete.split(','))
except ValueError as ve:
log.error('wrong format: %s' % ve)
else:
indexes = []
for i in temp_indexes:
if i < len(self.playlist.track_list) and i not in indexes:
indexes.append(i)
if indexes is not None and len(indexes) > 0:
result = self.playlist.delete(indexes, by_range)
if result is not None:
if by_range:
self.send_chat_msg('Deleted from index: %s to index: %s' %
(result['from'], result['to']))
elif result['deleted_indexes_len'] is 1:
self.send_chat_msg('Deleted %s' % result['track_title'])
else:
self.send_chat_msg('Deleted tracks at index: %s' %
', '.join(result['deleted_indexes']))
else:
self.send_chat_msg('Nothing was deleted.')
def do_media_replay(self):
""" Replay the currently playing track. """
if self.is_client_mod:
if self.playlist.track is not None:
self.cancel_timer()
track = self.playlist.replay()
self.send_yut_play(track.id, track.time, track.title)
self.timer(track.time)
def do_play_media(self):
""" Play a track on pause . """
if self.is_client_mod:
if self.playlist.track is not None:
if self.playlist.has_active_track:
self.cancel_timer()
if self.playlist.is_paused:
self.playlist.play(self.playlist.elapsed)
self.send_yut_play(self.playlist.track.id, self.playlist.track.time,
self.playlist.track.title, self.playlist.elapsed) #
self.timer(self.playlist.remaining)
def do_media_pause(self):
""" Pause a track. """
if self.is_client_mod:
track = self.playlist.track
if track is not None:
if self.playlist.has_active_track:
self.cancel_timer()
self.playlist.pause()
self.send_yut_pause(track.id, track.time, self.playlist.elapsed)
def do_close_media(self):
""" Close a track playing. """
if self.is_client_mod:
if self.playlist.has_active_track:
self.cancel_timer()
self.playlist.stop()
self.send_yut_stop(self.playlist.track.id, self.playlist.track.time, self.playlist.elapsed)
def do_seek_media(self, time_point):
"""
Time search a track.
:param time_point: The time point in which to search to.
:type time_point: str
"""
if self.is_client_mod:
if ('h' in time_point) or ('m' in time_point) or ('s' in time_point):
offset = pinylib.string_util.convert_to_seconds(time_point)
if offset == 0:
self.send_chat_msg('Invalid seek time.')
else:
track = self.playlist.track
if track is not None:
if 0 < offset < track.time:
if self.playlist.has_active_track:
self.cancel_timer()
if self.playlist.is_paused:
self.playlist.pause(offset=offset) #
self.send_yut_pause(track.id, track.time, offset)
else:
self.playlist.play(offset)
self.send_yut_play(track.id, track.time, track.title, offset)
self.timer(self.playlist.remaining)
def do_clear_playlist(self):
""" Clear the playlist for items."""
if self.is_client_mod:
if len(self.playlist.track_list) > 0:
pl_length = str(len(self.playlist.track_list))
self.playlist.clear()
self.send_chat_msg('Deleted %s items in the playlist.' % pl_length)
else:
self.send_chat_msg('The playlist is empty, nothing to delete.')
def do_playlist_info(self): # TODO: this needs more work !
""" Shows the next tracks in the playlist. """
if self.is_client_mod:
if len(self.playlist.track_list) > 0:
tracks = self.playlist.get_tracks()
if len(tracks) > 0:
# If i is 0 then mark that as the next track
_ = '\n'.join('(%s) - %s %s' % (track[0], track[1].title, self.format_time(track[1].time))
for i, track in enumerate(tracks))
self.send_chat_msg(_)
def do_youtube_search(self, search_str):
"""
Search youtube for a list of matching candidates.
:param search_str: The search term to search for.
:type search_str: str
"""
if self.is_client_mod:
if len(search_str) == 0:
self.send_chat_msg('Missing search string.')
else:
self.search_list = youtube.search_list(search_str, results=5)
if len(self.search_list) > 0:
self.is_search_list_yt_playlist = False
_ = '\n'.join('(%s) %s %s' % (i, d['video_title'], self.format_time(d['video_time']))
for i, d in enumerate(self.search_list)) #
self.send_chat_msg(_)
else:
self.send_chat_msg('Could not find anything matching: %s' % search_str)
def do_play_youtube_search(self, int_choice):
"""
Play a track from a previous youtube search list.
:param int_choice: The index of the track in the search.
:type int_choice: str | int
"""
if self.is_client_mod:
if not self.is_search_list_yt_playlist:
if len(self.search_list) > 0:
try:
int_choice = int(int_choice)
except ValueError:
self.send_chat_msg('Only numbers allowed.')
else:
if 0 <= int_choice <= len(self.search_list) - 1:
if self.playlist.has_active_track:
track = self.playlist.add(self.active_user.nick, self.search_list[int_choice])
self.send_chat_msg('Added (%s) %s %s' %
(self.playlist.last_index,
track.title, self.format_time(track.time)))
else:
track = self.playlist.start(self.active_user.nick, self.search_list[int_choice])
self.send_yut_play(track.id, track.time, track.title)
self.timer(track.time)
else:
self.send_chat_msg('Please make a choice between 0-%s' % str(len(self.search_list) - 1))
else:
self.send_chat_msg('No youtube track id\'s in the search list.')
else:
self.send_chat_msg('The search list only contains youtube playlist id\'s.')
def do_clear(self):
""" Clears the chat box. """
self.send_chat_msg('_\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n'
'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n_')
def do_nick(self, new_nick):
"""
Set a new nick for the bot.
:param new_nick: The new nick name.
:type new_nick: str
"""
if len(new_nick) is 0:
self.nickname = pinylib.string_util.create_random_string(5, 25)
self.set_nick()
else:
self.nickname = new_nick
self.set_nick()
def do_kick(self, user_name):
"""
Kick a user out of the room.
:param user_name: The username to kick.
:type user_name: str
"""
if self.is_client_mod:
if len(user_name) is 0:
self.send_chat_msg('Missing username.')
elif user_name == self.nickname:
self.send_chat_msg('Action not allowed.')
else:
if user_name.startswith('*'):
user_name = user_name.replace('*', '')
_users = self.users.search_containing(user_name)
if len(_users) > 0:
for i, user in enumerate(_users):
if user.nick != self.nickname and user.user_level > self.active_user.user_level:
if i <= pinylib.CONFIG.B_MAX_MATCH_BANS - 1: