4 import pymumble.pymumble_py3 as pymumble
10 import variables as var
11 from librb import radiobrowser
12 from database import SettingsDatabase, MusicDatabase
13 from media.item import item_builders, item_loaders, item_id_generators, dict_to_item, dicts_to_items
14 from media.playlist import get_item_wrapper_from_scrap, get_item_wrapper_by_id, get_item_wrappers_by_tags
15 from media.file import FileItem
16 from media.url_from_playlist import PlaylistURLItem, get_playlist_info
17 from media.url import URLItem
18 from media.radio import RadioItem
20 log = logging.getLogger("bot")
22 def register_all_commands(bot):
23 bot.register_command(constants.commands('joinme'), cmd_joinme)
24 bot.register_command(constants.commands('user_ban'), cmd_user_ban)
25 bot.register_command(constants.commands('user_unban'), cmd_user_unban)
26 bot.register_command(constants.commands('url_ban'), cmd_url_ban)
27 bot.register_command(constants.commands('url_unban'), cmd_url_unban)
28 bot.register_command(constants.commands('play'), cmd_play)
29 bot.register_command(constants.commands('pause'), cmd_pause)
30 bot.register_command(constants.commands('play_file'), cmd_play_file)
31 bot.register_command(constants.commands('play_file_match'), cmd_play_file_match)
32 bot.register_command(constants.commands('play_url'), cmd_play_url)
33 bot.register_command(constants.commands('play_playlist'), cmd_play_playlist)
34 bot.register_command(constants.commands('play_radio'), cmd_play_radio)
35 bot.register_command(constants.commands('play_tag'), cmd_play_tags)
36 bot.register_command(constants.commands('rb_query'), cmd_rb_query)
37 bot.register_command(constants.commands('rb_play'), cmd_rb_play)
38 bot.register_command(constants.commands('yt_search'), cmd_yt_search)
39 bot.register_command(constants.commands('yt_play'), cmd_yt_play)
40 bot.register_command(constants.commands('help'), cmd_help)
41 bot.register_command(constants.commands('stop'), cmd_stop)
42 bot.register_command(constants.commands('clear'), cmd_clear)
43 bot.register_command(constants.commands('kill'), cmd_kill)
44 bot.register_command(constants.commands('update'), cmd_update)
45 bot.register_command(constants.commands('stop_and_getout'), cmd_stop_and_getout)
46 bot.register_command(constants.commands('volume'), cmd_volume)
47 bot.register_command(constants.commands('ducking'), cmd_ducking)
48 bot.register_command(constants.commands('ducking_threshold'), cmd_ducking_threshold)
49 bot.register_command(constants.commands('ducking_volume'), cmd_ducking_volume)
50 bot.register_command(constants.commands('current_music'), cmd_current_music)
51 bot.register_command(constants.commands('skip'), cmd_skip)
52 bot.register_command(constants.commands('last'), cmd_last)
53 bot.register_command(constants.commands('remove'), cmd_remove)
54 bot.register_command(constants.commands('list_file'), cmd_list_file)
55 bot.register_command(constants.commands('queue'), cmd_queue)
56 bot.register_command(constants.commands('random'), cmd_random)
57 bot.register_command(constants.commands('repeat'), cmd_repeat)
58 bot.register_command(constants.commands('mode'), cmd_mode)
59 bot.register_command(constants.commands('add_tag'), cmd_add_tag)
60 bot.register_command(constants.commands('remove_tag'), cmd_remove_tag)
61 bot.register_command(constants.commands('find_tagged'), cmd_find_tagged)
62 bot.register_command(constants.commands('search'), cmd_search_library)
63 bot.register_command(constants.commands('add_from_shortlist'), cmd_shortlist)
64 bot.register_command(constants.commands('delete_from_library'), cmd_delete_from_library)
65 bot.register_command(constants.commands('drop_database'), cmd_drop_database, True)
66 bot.register_command(constants.commands('rescan'), cmd_refresh_cache, True)
69 bot.register_command('rtrms', cmd_real_time_rms, True)
70 bot.register_command('loop', cmd_loop_state, True)
71 bot.register_command('item', cmd_item, True)
73 def send_multi_lines(bot, lines, text, linebreak="<br />"):
81 if (len(msg) + len(newline)) > (bot.mumble.get_max_message_length() - 4) != 0: # 4 == len("<br>")
82 bot.send_msg(msg, text)
86 bot.send_msg(msg, text)
88 # ---------------- Variables -----------------
92 # ---------------- Commands ------------------
95 def cmd_joinme(bot, user, text, command, parameter):
98 bot.mumble.users.myself.move_in(
99 bot.mumble.users[text.actor]['channel_id'], token=parameter)
102 def cmd_user_ban(bot, user, text, command, parameter):
105 if bot.is_admin(user):
107 bot.mumble.users[text.actor].send_text_message(util.user_ban(parameter))
109 bot.mumble.users[text.actor].send_text_message(util.get_user_ban())
111 bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
115 def cmd_user_unban(bot, user, text, command, parameter):
118 if bot.is_admin(user):
120 bot.mumble.users[text.actor].send_text_message(util.user_unban(parameter))
122 bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
126 def cmd_url_ban(bot, user, text, command, parameter):
129 if bot.is_admin(user):
131 bot.mumble.users[text.actor].send_text_message(util.url_ban(util.get_url_from_input(parameter)))
133 bot.mumble.users[text.actor].send_text_message(util.get_url_ban())
135 bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
139 def cmd_url_unban(bot, user, text, command, parameter):
142 if bot.is_admin(user):
144 bot.mumble.users[text.actor].send_text_message(util.url_unban(util.get_url_from_input(parameter)))
146 bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
150 def cmd_play(bot, user, text, command, parameter):
153 if len(var.playlist) > 0:
155 if parameter.isdigit() and 1 <= int(parameter) <= len(var.playlist):
156 var.playlist.point_to(int(parameter) - 1 - 1) # First "-1" transfer 12345 to 01234, second "-1"
157 # point to the previous item. the loop will next to
161 bot.send_msg(constants.strings('invalid_index', index=parameter), text)
166 bot.send_msg(var.playlist.current_item().format_current_playing(), text)
169 bot.send_msg(constants.strings('queue_empty'), text)
172 def cmd_pause(bot, user, text, command, parameter):
176 bot.send_msg(constants.strings('paused'))
179 def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=False):
180 global log, song_shortlist
182 # if parameter is {index}
183 if parameter.isdigit():
184 files = var.cache.files
185 if int(parameter) < len(files):
186 music_wrapper = get_item_wrapper_by_id(bot, var.cache.file_id_lookup[files[int(parameter)]], user)
187 var.playlist.append(music_wrapper)
188 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
189 bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
192 # if parameter is {path}
194 # sanitize "../" and so on
195 # path = os.path.abspath(os.path.join(var.music_folder, parameter))
196 # if not path.startswith(os.path.abspath(var.music_folder)):
197 # bot.send_msg(constants.strings('no_file'), text)
200 if parameter in var.cache.files:
201 music_wrapper = get_item_wrapper_from_scrap(bot, type='file', path=parameter, user=user)
202 var.playlist.append(music_wrapper)
203 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
204 bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
207 # if parameter is {folder}
208 files = var.cache.dir.get_files(parameter)
210 msgs = [constants.strings('multiple_file_added')]
215 music_wrapper = get_item_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
216 var.playlist.append(music_wrapper)
217 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
218 msgs.append("{} ({})".format(music_wrapper.item().title, music_wrapper.item().path))
221 send_multi_lines(bot, msgs, text)
225 # try to do a partial match
226 files = var.cache.files
227 matches = [ file for file in files if parameter.lower() in file.lower()]
228 if len(matches) == 1:
230 music_wrapper = get_item_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
231 var.playlist.append(music_wrapper)
232 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
233 bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
235 elif len(matches) > 1:
236 msgs = [ constants.strings('multiple_matches') ]
238 for index, match in enumerate(matches):
239 id = var.cache.file_id_lookup[match]
240 music_dict = var.music_db.query_music_by_id(id)
241 item = dict_to_item(bot, music_dict)
243 song_shortlist.append(music_dict)
245 msgs.append("<b>{:d}</b> - <b>{:s}</b> ({:s})".format(
246 index + 1, item.title, match))
247 send_multi_lines(bot, msgs, text)
250 if do_not_refresh_cache:
251 bot.send_msg(constants.strings("no_file"), text)
253 var.cache.build_dir_cache(bot)
254 cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=True)
257 def cmd_play_file_match(bot, user, text, command, parameter, do_not_refresh_cache=False):
260 music_folder = var.music_folder
262 files = var.cache.files
263 msgs = [ constants.strings('multiple_file_added') + "<ul>"]
268 match = re.search(parameter, file)
269 if match and match[0]:
271 music_wrapper = get_item_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
272 music_wrappers.append(music_wrapper)
273 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
274 msgs.append("<li><b>{}</b> ({})</li>".format(music_wrapper.item().title,
275 file[:match.span()[0]]
276 + "<b style='color:pink'>"
277 + file[match.span()[0]: match.span()[1]]
279 + file[match.span()[1]:]
284 var.playlist.extend(music_wrappers)
285 send_multi_lines(bot, msgs, text, "")
287 if do_not_refresh_cache:
288 bot.send_msg(constants.strings("no_file"), text)
290 var.cache.build_dir_cache(bot)
291 cmd_play_file_match(bot, user, text, command, parameter, do_not_refresh_cache=True)
293 except re.error as e:
294 msg = constants.strings('wrong_pattern', error=str(e))
295 bot.send_msg(msg, text)
297 bot.send_msg(constants.strings('bad_parameter', command=command))
300 def cmd_play_url(bot, user, text, command, parameter):
303 url = util.get_url_from_input(parameter)
305 music_wrapper = get_item_wrapper_from_scrap(bot, type='url', url=url, user=user)
306 var.playlist.append(music_wrapper)
308 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
309 bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
310 if len(var.playlist) == 2:
311 # If I am the second item on the playlist. (I am the next one!)
312 bot.async_download_next()
314 bot.send_msg(constants.strings('bad_parameter', command=command))
318 def cmd_play_playlist(bot, user, text, command, parameter):
321 offset = 0 # if you want to start the playlist at a specific index
323 offset = int(parameter.split(" ")[-1])
327 url = util.get_url_from_input(parameter)
328 log.debug("cmd: fetching media info from playlist url %s" % url)
329 items = get_playlist_info(url=url, start_index=offset, user=user)
331 items = var.playlist.extend(list(map(
332 lambda item: get_item_wrapper_from_scrap(bot, **item), items)))
334 log.info("cmd: add to playlist: " + music.format_debug_string())
336 bot.send_msg(constants.strings("playlist_fetching_failed"), text)
339 def cmd_play_radio(bot, user, text, command, parameter):
343 all_radio = var.config.items('radio')
344 msg = constants.strings('preconfigurated_radio')
347 if len(i[1].split(maxsplit=1)) == 2:
348 comment = " - " + i[1].split(maxsplit=1)[1]
349 msg += "<br />" + i[0] + comment
350 bot.send_msg(msg, text)
352 if var.config.has_option('radio', parameter):
353 parameter = var.config.get('radio', parameter)
354 parameter = parameter.split()[0]
355 url = util.get_url_from_input(parameter)
357 music_wrapper = get_item_wrapper_from_scrap(bot, type='radio', url=url, user=user)
359 var.playlist.append(music_wrapper)
360 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
361 bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
363 bot.send_msg(constants.strings('bad_url'))
366 def cmd_rb_query(bot, user, text, command, parameter):
369 log.info('cmd: Querying radio stations')
371 log.debug('rbquery without parameter')
372 msg = constants.strings('rb_query_empty')
373 bot.send_msg(msg, text)
375 log.debug('cmd: Found query parameter: ' + parameter)
376 # bot.send_msg('Searching for stations - this may take some seconds...', text)
377 rb_stations = radiobrowser.getstations_byname(parameter)
378 msg = constants.strings('rb_query_result')
379 msg += '\n<table><tr><th>!rbplay ID</th><th>Station Name</th><th>Genre</th><th>Codec/Bitrate</th><th>Country</th></tr>'
381 log.debug('cmd: No matches found for rbquery ' + parameter)
382 bot.send_msg('Radio-Browser found no matches for ' + parameter, text)
384 for s in rb_stations:
386 stationname = s['stationname']
387 country = s['country']
389 bitrate = s['bitrate']
391 # msg += f'<tr><td>{stationid}</td><td>{stationname}</td><td>{genre}</td><td>{codec}/{bitrate}</td><td>{country}</td></tr>'
392 msg += '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s/%s</td><td>%s</td></tr>' % (
393 stationid, stationname, genre, codec, bitrate, country)
395 # Full message as html table
397 bot.send_msg(msg, text)
398 # Shorten message if message too long (stage I)
400 log.debug('Result too long stage I')
401 msg = constants.strings('rb_query_result') + ' (shortened L1)'
402 msg += '\n<table><tr><th>!rbplay ID</th><th>Station Name</th></tr>'
403 for s in rb_stations:
405 stationname = s['stationname']
406 # msg += f'<tr><td>{stationid}</td><td>{stationname}</td>'
407 msg += '<tr><td>%s</td><td>%s</td>' % (stationid, stationname)
410 bot.send_msg(msg, text)
411 # Shorten message if message too long (stage II)
413 log.debug('Result too long stage II')
414 msg = constants.strings('rb_query_result') + ' (shortened L2)'
415 msg += '!rbplay ID - Station Name'
416 for s in rb_stations:
418 stationname = s['stationname'][:12]
419 # msg += f'{stationid} - {stationname}'
420 msg += '%s - %s' % (stationid, stationname)
422 bot.send_msg(msg, text)
423 # Message still too long
425 bot.send_msg('Query result too long to post (> 5000 characters), please try another query.',
429 def cmd_rb_play(bot, user, text, command, parameter):
432 log.debug('cmd: Play a station by ID')
434 log.debug('rbplay without parameter')
435 msg = constants.strings('rb_play_empty')
436 bot.send_msg(msg, text)
438 log.debug('cmd: Retreiving url for station ID ' + parameter)
439 rstation = radiobrowser.getstationname_byid(parameter)
440 stationname = rstation[0]['name']
441 country = rstation[0]['country']
442 codec = rstation[0]['codec']
443 bitrate = rstation[0]['bitrate']
444 genre = rstation[0]['tags']
445 homepage = rstation[0]['homepage']
446 msg = 'Radio station added to playlist:'
447 # msg += '<table><tr><th>ID</th><th>Station Name</th><th>Genre</th><th>Codec/Bitrate</th><th>Country</th><th>Homepage</th></tr>' + \
448 # f'<tr><td>{parameter}</td><td>{stationname}</td><td>{genre}</td><td>{codec}/{bitrate}</td><td>{country}</td><td>{homepage}</td></tr></table>'
449 msg += '<table><tr><th>ID</th><th>Station Name</th><th>Genre</th><th>Codec/Bitrate</th><th>Country</th><th>Homepage</th></tr>' + \
450 '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s/%s</td><td>%s</td><td>%s</td></tr></table>' \
451 % (parameter, stationname, genre, codec, bitrate, country, homepage)
452 log.debug('cmd: Added station to playlist %s' % stationname)
453 bot.send_msg(msg, text)
454 url = radiobrowser.geturl_byid(parameter)
456 log.info('cmd: Found url: ' + url)
457 music_wrapper = get_item_wrapper_from_scrap(bot, type='radio', url=url, name=stationname, user=user)
458 var.playlist.append(music_wrapper)
459 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
460 bot.async_download_next()
462 log.info('cmd: No playable url found.')
463 msg += "No playable url found for this station, please try another station."
464 bot.send_msg(msg, text)
467 yt_last_page = 0 # TODO: if we keep adding global variables, we need to consider sealing all commands up into classes.
469 def cmd_yt_search(bot, user, text, command, parameter):
470 global log, yt_last_result, yt_last_page, song_shortlist
475 if parameter.startswith("-n"):
477 if len(yt_last_result) > yt_last_page * item_per_page:
478 song_shortlist = [{'type': 'url',
479 'url': "https://www.youtube.com/watch?v=" + result[0],
481 } for result in yt_last_result[yt_last_page * item_per_page: item_per_page]]
482 msg = _yt_format_result(yt_last_result, yt_last_page * item_per_page, item_per_page)
483 bot.send_msg(constants.strings('yt_result', result_table=msg), text)
485 bot.send_msg(constants.strings('yt_no_more'))
489 results = util.youtube_search(parameter)
491 yt_last_result = results
493 song_shortlist = [{'type': 'url', 'url': "https://www.youtube.com/watch?v=" + result[0]}
494 for result in results[0: item_per_page]]
495 msg = _yt_format_result(results, 0, item_per_page)
496 bot.send_msg(constants.strings('yt_result', result_table=msg), text)
498 bot.send_msg(constants.strings('yt_query_error'))
500 bot.send_msg(constants.strings('bad_parameter', command=command), text)
502 def _yt_format_result(results, start, count):
503 msg = '<table><tr><th width="10%">Index</th><th>Title</th><th width="20%">Uploader</th></tr>'
504 for index, item in enumerate(results[start:start+count]):
505 msg += '<tr><td>{index:d}</td><td>{title}</td><td>{uploader}</td></tr>'.format(
506 index=index + 1, title=item[1], uploader=item[2])
512 def cmd_yt_play(bot, user, text, command, parameter):
513 global log, yt_last_result, yt_last_page
516 results = util.youtube_search(parameter)
518 yt_last_result = results
520 url = "https://www.youtube.com/watch?v=" + yt_last_result[0][0]
521 cmd_play_url(bot, user, text, command, url)
523 bot.send_msg(constants.strings('yt_query_error'))
525 bot.send_msg(constants.strings('bad_parameter', command=command), text)
528 def cmd_help(bot, user, text, command, parameter):
531 bot.send_msg(constants.strings('help'), text)
532 if bot.is_admin(user):
533 bot.send_msg(constants.strings('admin_help'), text)
536 def cmd_stop(bot, user, text, command, parameter):
540 bot.send_msg(constants.strings('stopped'), text)
543 def cmd_clear(bot, user, text, command, parameter):
547 bot.send_msg(constants.strings('cleared'), text)
550 def cmd_kill(bot, user, text, command, parameter):
553 if bot.is_admin(user):
557 bot.mumble.users[text.actor].send_text_message(
558 constants.strings('not_admin'))
561 def cmd_update(bot, user, text, command, parameter):
564 if bot.is_admin(user):
565 bot.mumble.users[text.actor].send_text_message(
566 constants.strings('start_updating'))
567 msg = util.update(bot.version)
568 bot.mumble.users[text.actor].send_text_message(msg)
570 bot.mumble.users[text.actor].send_text_message(
571 constants.strings('not_admin'))
574 def cmd_stop_and_getout(bot, user, text, command, parameter):
579 bot.mumble.channels.find_by_name(bot.channel).move_in()
582 def cmd_volume(bot, user, text, command, parameter):
585 # The volume is a percentage
586 if parameter and parameter.isdigit() and 0 <= int(parameter) <= 100:
587 bot.volume_set = float(float(parameter) / 100)
588 bot.send_msg(constants.strings('change_volume',
589 volume=int(bot.volume_set * 100), user=bot.mumble.users[text.actor]['name']), text)
590 var.db.set('bot', 'volume', str(bot.volume_set))
591 log.info('cmd: volume set to %d' % (bot.volume_set * 100))
593 bot.send_msg(constants.strings('current_volume', volume=int(bot.volume_set * 100)), text)
596 def cmd_ducking(bot, user, text, command, parameter):
599 if parameter == "" or parameter == "on":
600 bot.is_ducking = True
601 var.db.set('bot', 'ducking', True)
602 bot.ducking_volume = var.config.getfloat("bot", "ducking_volume", fallback=0.05)
603 bot.ducking_threshold = var.config.getint("bot", "ducking_threshold", fallback=5000)
604 bot.mumble.callbacks.set_callback(pymumble.constants.PYMUMBLE_CLBK_SOUNDRECEIVED,
605 bot.ducking_sound_received)
606 bot.mumble.set_receive_sound(True)
607 log.info('cmd: ducking is on')
609 bot.send_msg(msg, text)
610 elif parameter == "off":
611 bot.is_ducking = False
612 bot.mumble.set_receive_sound(False)
613 var.db.set('bot', 'ducking', False)
615 log.info('cmd: ducking is off')
616 bot.send_msg(msg, text)
619 def cmd_ducking_threshold(bot, user, text, command, parameter):
622 if parameter and parameter.isdigit():
623 bot.ducking_threshold = int(parameter)
624 var.db.set('bot', 'ducking_threshold', str(bot.ducking_threshold))
625 msg = "Ducking threshold set to %d." % bot.ducking_threshold
626 bot.send_msg(msg, text)
628 msg = "Current ducking threshold is %d." % bot.ducking_threshold
629 bot.send_msg(msg, text)
632 def cmd_ducking_volume(bot, user, text, command, parameter):
635 # The volume is a percentage
636 if parameter and parameter.isdigit() and 0 <= int(parameter) <= 100:
637 bot.ducking_volume = float(float(parameter) / 100)
638 bot.send_msg(constants.strings('change_ducking_volume',
639 volume=int(bot.ducking_volume * 100), user=bot.mumble.users[text.actor]['name']), text)
640 # var.db.set('bot', 'volume', str(bot.volume_set))
641 var.db.set('bot', 'ducking_volume', str(bot.ducking_volume))
642 log.info('cmd: volume on ducking set to %d' % (bot.ducking_volume * 100))
644 bot.send_msg(constants.strings('current_ducking_volume', volume=int(bot.ducking_volume * 100)), text)
647 def cmd_current_music(bot, user, text, command, parameter):
651 if len(var.playlist) > 0:
652 bot.send_msg(var.playlist.current_item().format_current_playing())
654 reply = constants.strings('not_playing')
655 bot.send_msg(reply, text)
658 def cmd_skip(bot, user, text, command, parameter):
663 if len(var.playlist) == 0:
664 bot.send_msg(constants.strings('queue_empty'), text)
667 def cmd_last(bot, user, text, command, parameter):
670 if len(var.playlist) > 0:
672 var.playlist.point_to(len(var.playlist) - 1 - 1)
674 bot.send_msg(constants.strings('queue_empty'), text)
677 def cmd_remove(bot, user, text, command, parameter):
680 # Allow to remove specific music into the queue with a number
681 if parameter and parameter.isdigit() and int(parameter) > 0 \
682 and int(parameter) <= len(var.playlist):
684 index = int(parameter) - 1
687 if index == var.playlist.current_index:
688 removed = var.playlist.remove(index)
690 if index < len(var.playlist):
693 var.playlist.current_index -= 1
694 # then the bot will move to next item
696 else: # if item deleted is the last item of the queue
697 var.playlist.current_index -= 1
701 removed = var.playlist.remove(index)
703 bot.send_msg(constants.strings('removing_item',
704 item=removed.format_short_string()), text)
706 log.info("cmd: delete from playlist: " + removed.format_debug_string())
708 bot.send_msg(constants.strings('bad_parameter', command=command))
711 def cmd_list_file(bot, user, text, command, parameter):
714 files = var.cache.files
715 msgs = [ constants.strings("multiple_file_found") ]
718 for index, file in enumerate(files):
720 match = re.search(parameter, file)
725 msgs.append("<b>{:0>3d}</b> - {:s}".format(index, file))
728 send_multi_lines(bot, msgs, text)
730 bot.send_msg(constants.strings('no_file'), text)
732 except re.error as e:
733 msg = constants.strings('wrong_pattern', error=str(e))
734 bot.send_msg(msg, text)
737 def cmd_queue(bot, user, text, command, parameter):
740 if len(var.playlist) == 0:
741 msg = constants.strings('queue_empty')
742 bot.send_msg(msg, text)
744 msgs = [ constants.strings('queue_contents')]
745 for i, music in enumerate(var.playlist):
748 if len(music.item().tags) > 0:
749 tags = "<sup>{}</sup>".format(", ".join(music.item().tags))
750 if i == var.playlist.current_index:
751 newline = "<b style='color:orange'>{} ({}) {} </b> {}".format(i + 1, music.display_type(),
752 music.format_short_string(), tags)
754 newline = '<b>{}</b> ({}) {} {}'.format(i + 1, music.display_type(),
755 music.format_short_string(), tags)
759 send_multi_lines(bot, msgs, text)
761 def cmd_random(bot, user, text, command, parameter):
765 var.playlist.randomize()
767 def cmd_repeat(bot, user, text, command, parameter):
771 if parameter and parameter.isdigit():
772 repeat = int(parameter)
774 music = var.playlist.current_item()
775 for _ in range(repeat):
777 var.playlist.current_index + 1,
780 log.info("bot: add to playlist: " + music.format_debug_string())
782 bot.send_msg(constants.strings("repeat", song=music.format_song_string(), n=str(repeat)), text)
784 def cmd_mode(bot, user, text, command, parameter):
788 bot.send_msg(constants.strings("current_mode", mode=var.playlist.mode), text)
790 if not parameter in ["one-shot", "repeat", "random", "autoplay"]:
791 bot.send_msg(constants.strings('unknown_mode', mode=parameter), text)
793 var.db.set('playlist', 'playback_mode', parameter)
794 var.playlist = media.playlist.get_playlist(parameter, var.playlist)
795 log.info("command: playback mode changed to %s." % parameter)
796 bot.send_msg(constants.strings("change_mode", mode=var.playlist.mode,
797 user=bot.mumble.users[text.actor]['name']), text)
798 if parameter == "random":
802 def cmd_play_tags(bot, user, text, command, parameter):
804 bot.send_msg(constants.strings('bad_parameter', command=command))
807 msgs = [constants.strings('multiple_file_added') + "<ul>"]
810 tags = parameter.split(",")
811 tags = list(map(lambda t: t.strip(), tags))
812 music_wrappers = get_item_wrappers_by_tags(bot, tags, user)
813 for music_wrapper in music_wrappers:
815 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
816 msgs.append("<li><b>{}</b> (<i>{}</i>)</li>".format(music_wrapper.item().title, ", ".join(music_wrapper.item().tags)))
821 var.playlist.extend(music_wrappers)
822 send_multi_lines(bot, msgs, text, "")
824 bot.send_msg(constants.strings("no_file"), text)
827 def cmd_add_tag(bot, user, text, command, parameter):
830 params = parameter.split()
833 tags = list(map(lambda t: t.strip(), params[1].split(",")))
835 if index.isdigit() and 1 <= int(index) <= len(var.playlist):
836 var.playlist[int(index) - 1].add_tags(tags)
837 log.info("cmd: add tags %s to song %s" % (", ".join(tags),
838 var.playlist[int(index) - 1].format_debug_string()))
839 bot.send_msg(constants.strings("added_tags",
840 tags=", ".join(tags),
841 song=var.playlist[int(index) - 1].format_short_string()), text)
843 for item in var.playlist:
845 log.info("cmd: add tags %s to song %s" % (", ".join(tags),
846 item.format_debug_string()))
847 bot.send_msg(constants.strings("added_tags_to_all", tags=", ".join(tags)), text)
849 bot.send_msg(constants.strings('bad_parameter', command=command), text)
852 def cmd_remove_tag(bot, user, text, command, parameter):
855 params = parameter.split()
856 if len(params) == 2 and params[1]:
859 if index.isdigit() and 1 <= int(index) <= len(var.playlist):
861 tags = list(map(lambda t: t.strip(), params[1].split(",")))
862 var.playlist[int(index) - 1].remove_tags(tags)
863 log.info("cmd: remove tags %s from song %s" % (", ".join(tags),
864 var.playlist[int(index) - 1].format_debug_string()))
865 bot.send_msg(constants.strings("removed_tags",
866 tags=", ".join(tags),
867 song=var.playlist[int(index) - 1].format_short_string()), text)
870 var.playlist[int(index) - 1].clear_tags()
871 log.info("cmd: clear tags from song %s" % (var.playlist[int(index) - 1].format_debug_string()))
872 bot.send_msg(constants.strings("cleared_tags",
873 song=var.playlist[int(index) - 1].format_short_string()), text)
878 tags = list(map(lambda t: t.strip(), params[1].split(",")))
879 for item in var.playlist:
880 item.remove_tags(tags)
881 log.info("cmd: remove tags %s from song %s" % (", ".join(tags),
882 item.format_debug_string()))
883 bot.send_msg(constants.strings("removed_tags_from_all", tags=", ".join(tags)), text)
886 for item in var.playlist:
888 log.info("cmd: clear tags from song %s" % (item.format_debug_string()))
889 bot.send_msg(constants.strings("cleared_tags_from_all"), text)
892 bot.send_msg(constants.strings('bad_parameter', command=command), text)
894 def cmd_find_tagged(bot, user, text, command, parameter):
895 global song_shortlist
898 bot.send_msg(constants.strings('bad_parameter', command=command))
901 msgs = [constants.strings('multiple_file_found') + "<ul>"]
904 tags = parameter.split(",")
905 tags = list(map(lambda t: t.strip(), tags))
907 music_dicts = var.music_db.query_music_by_tags(tags)
908 song_shortlist = music_dicts
909 items = dicts_to_items(bot, music_dicts)
911 for i, item in enumerate(items):
913 msgs.append("<li><b>{:d}</b> - <b>{}</b> (<i>{}</i>)</li>".format(i+1, item.title, ", ".join(item.tags)))
917 msgs.append(constants.strings("shortlist_instruction"))
918 send_multi_lines(bot, msgs, text, "")
920 bot.send_msg(constants.strings("no_file"), text)
922 def cmd_search_library(bot, user, text, command, parameter):
923 global song_shortlist
925 bot.send_msg(constants.strings('bad_parameter', command=command))
928 msgs = [constants.strings('multiple_file_found') + "<ul>"]
931 _keywords = parameter.split(" ")
937 music_dicts = var.music_db.query_music_by_keywords(keywords)
939 items = dicts_to_items(bot, music_dicts)
940 song_shortlist = music_dicts
944 if len(item.tags) > 0:
945 msgs.append("<li><b>{:d}</b> - [{}] <b>{}</b> (<i>{}</i>)</li>".format(count, item.display_type(), item.title, ", ".join(item.tags)))
947 msgs.append("<li><b>{:d}</b> - [{}] <b>{}</b> </li>".format(count, item.display_type(), item.title, ", ".join(item.tags)))
951 msgs.append(constants.strings("shortlist_instruction"))
952 send_multi_lines(bot, msgs, text, "")
954 bot.send_msg(constants.strings("no_file"), text)
956 bot.send_msg(constants.strings("no_file"), text)
959 def cmd_shortlist(bot, user, text, command, parameter):
960 global song_shortlist, log
963 indexes = [ int(i) for i in parameter.split(" ") ]
965 bot.send_msg(constants.strings('bad_parameter', command=command), text)
969 msgs = [constants.strings('multiple_file_added') + "<ul>"]
970 for index in indexes:
971 if 1 <= index <= len(song_shortlist):
972 kwargs = song_shortlist[index - 1]
973 kwargs['user'] = user
974 music_wrapper = get_item_wrapper_from_scrap(bot, **kwargs)
975 var.playlist.append(music_wrapper)
976 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
977 msgs.append("<li>[{}] <b>{}</b></li>".format(music_wrapper.item().type, music_wrapper.item().title))
979 bot.send_msg(constants.strings('bad_parameter', command=command), text)
983 send_multi_lines(bot, msgs, text, "")
985 elif len(indexes) == 1:
987 if 1 <= index <= len(song_shortlist):
988 kwargs = song_shortlist[index - 1]
989 kwargs['user'] = user
990 music_wrapper = get_item_wrapper_from_scrap(bot, **kwargs)
991 var.playlist.append(music_wrapper)
992 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
993 bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
996 bot.send_msg(constants.strings('bad_parameter', command=command), text)
999 def cmd_delete_from_library(bot, user, text, command, parameter):
1000 global song_shortlist, log
1003 indexes = [ int(i) for i in parameter.split(" ") ]
1005 bot.send_msg(constants.strings('bad_parameter', command=command), text)
1008 if len(indexes) > 1:
1009 msgs = [constants.strings('multiple_file_added') + "<ul>"]
1011 for index in indexes:
1012 if 1 <= index <= len(song_shortlist):
1013 music_dict = song_shortlist[index - 1]
1014 if 'id' in music_dict:
1015 music_wrapper = get_item_wrapper_by_id(bot, music_dict['id'], user)
1016 log.info("cmd: remove from library: " + music_wrapper.format_debug_string())
1017 msgs.append("<li>[{}] <b>{}</b></li>".format(music_wrapper.item().type ,music_wrapper.item().title))
1018 var.playlist.remove_by_id(music_dict['id'])
1019 var.cache.free_and_delete(music_dict['id'])
1022 bot.send_msg(constants.strings('bad_parameter', command=command), text)
1026 bot.send_msg(constants.strings('bad_parameter', command=command), text)
1029 msgs.append("</ul>")
1030 send_multi_lines(bot, msgs, text, "")
1032 elif len(indexes) == 1:
1034 if 1 <= index <= len(song_shortlist):
1035 music_dict = song_shortlist[index - 1]
1036 if 'id' in music_dict:
1037 music_wrapper = get_item_wrapper_by_id(bot, music_dict['id'], user)
1038 bot.send_msg(constants.strings('file_deleted', item=music_wrapper.format_song_string()), text)
1039 log.info("cmd: remove from library: " + music_wrapper.format_debug_string())
1040 var.playlist.remove_by_id(music_dict['id'])
1041 var.cache.free_and_delete(music_dict['id'])
1044 bot.send_msg(constants.strings('bad_parameter', command=command), text)
1046 def cmd_drop_database(bot, user, text, command, parameter):
1049 if bot.is_admin(user):
1051 var.db = SettingsDatabase(var.dbfile)
1052 var.music_db.drop_table()
1053 var.music_db = MusicDatabase(var.dbfile)
1054 log.info("command: database dropped.")
1055 bot.send_msg(constants.strings('database_dropped'), text)
1057 bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
1059 def cmd_refresh_cache(bot, user, text, command, parameter):
1061 if bot.is_admin(user):
1062 var.cache.build_dir_cache(bot)
1063 log.info("command: Local file cache refreshed.")
1064 bot.send_msg(constants.strings('cache_refreshed'), text)
1066 bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
1068 # Just for debug use
1069 def cmd_real_time_rms(bot, user, text, command, parameter):
1070 bot._display_rms = not bot._display_rms
1072 def cmd_loop_state(bot, user, text, command, parameter):
1073 print(bot._loop_status)
1075 def cmd_item(bot, user, text, command, parameter):
1076 print(bot.wait_for_downloading)
1077 print(var.playlist.current_item().to_dict())