diff --git a/command.py b/command.py
index f53fb6a..e8e32b4 100644
--- a/command.py
+++ b/command.py
@@ -11,7 +11,7 @@ import variables as var
from librb import radiobrowser
from database import SettingsDatabase, MusicDatabase
from media.item import item_builders, item_loaders, item_id_generators, dict_to_item, dicts_to_items
-from media.playlist import get_item_wrapper_from_scrap, get_item_wrapper_by_id, get_item_wrappers_by_tags
+from media.cache import get_cached_wrapper_from_scrap, get_cached_wrapper_by_id, get_cached_wrappers_by_tags
from media.file import FileItem
from media.url_from_playlist import PlaylistURLItem, get_playlist_info
from media.url import URLItem
@@ -21,10 +21,11 @@ log = logging.getLogger("bot")
def register_all_commands(bot):
bot.register_command(constants.commands('joinme'), cmd_joinme, no_partial_match=False, access_outside_channel=True)
- bot.register_command(constants.commands('user_ban'), cmd_user_ban)
- bot.register_command(constants.commands('user_unban'), cmd_user_unban)
- bot.register_command(constants.commands('url_ban'), cmd_url_ban)
- bot.register_command(constants.commands('url_unban'), cmd_url_unban)
+ bot.register_command(constants.commands('user_ban'), cmd_user_ban, no_partial_match=True)
+ bot.register_command(constants.commands('user_unban'), cmd_user_unban, no_partial_match=True)
+ bot.register_command(constants.commands('url_ban_list'), cmd_url_ban_list, no_partial_match=True)
+ bot.register_command(constants.commands('url_ban'), cmd_url_ban, no_partial_match=True)
+ bot.register_command(constants.commands('url_unban'), cmd_url_unban, no_partial_match=True)
bot.register_command(constants.commands('play'), cmd_play)
bot.register_command(constants.commands('pause'), cmd_pause)
bot.register_command(constants.commands('play_file'), cmd_play_file)
@@ -41,7 +42,7 @@ def register_all_commands(bot):
bot.register_command(constants.commands('stop'), cmd_stop)
bot.register_command(constants.commands('clear'), cmd_clear)
bot.register_command(constants.commands('kill'), cmd_kill)
- bot.register_command(constants.commands('update'), cmd_update)
+ bot.register_command(constants.commands('update'), cmd_update, no_partial_match=True)
bot.register_command(constants.commands('stop_and_getout'), cmd_stop_and_getout)
bot.register_command(constants.commands('volume'), cmd_volume)
bot.register_command(constants.commands('ducking'), cmd_ducking)
@@ -129,12 +130,28 @@ def cmd_url_ban(bot, user, text, command, parameter):
if bot.is_admin(user):
if parameter:
bot.mumble.users[text.actor].send_text_message(util.url_ban(util.get_url_from_input(parameter)))
+
+ id = item_id_generators['url'](url=parameter)
+ var.cache.free_and_delete(id)
+ var.playlist.remove_by_id(id)
else:
- bot.mumble.users[text.actor].send_text_message(util.get_url_ban())
+ if var.playlist.current_item().type == 'url':
+ item = var.playlist.current_item().item()
+ bot.mumble.users[text.actor].send_text_message(util.url_ban(util.get_url_from_input(item.url)))
+ var.cache.free_and_delete(item.id)
+ var.playlist.remove_by_id(item.id)
+ else:
+ bot.send_msg(constants.strings('bad_parameter', command=command))
else:
bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
return
+def cmd_url_ban_list(bot, user, text, command, parameter):
+ if bot.is_admin(user):
+ bot.mumble.users[text.actor].send_text_message(util.get_url_ban())
+ else:
+ bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
+ return
def cmd_url_unban(bot, user, text, command, parameter):
global log
@@ -183,10 +200,10 @@ def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=Fals
if parameter.isdigit():
files = var.cache.files
if int(parameter) < len(files):
- music_wrapper = get_item_wrapper_by_id(bot, var.cache.file_id_lookup[files[int(parameter)]], user)
+ music_wrapper = get_cached_wrapper_by_id(bot, var.cache.file_id_lookup[files[int(parameter)]], user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
- bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
+ bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
return
# if parameter is {path}
@@ -198,10 +215,10 @@ def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=Fals
# return
if parameter in var.cache.files:
- music_wrapper = get_item_wrapper_from_scrap(bot, type='file', path=parameter, user=user)
+ music_wrapper = get_cached_wrapper_from_scrap(bot, type='file', path=parameter, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
- bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
+ bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
return
# if parameter is {folder}
@@ -212,13 +229,13 @@ def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=Fals
for file in files:
count += 1
- music_wrapper = get_item_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
+ music_wrapper = get_cached_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
msgs.append("{} ({})".format(music_wrapper.item().title, music_wrapper.item().path))
if count != 0:
- send_multi_lines(bot, msgs, text)
+ send_multi_lines(bot, msgs, None)
return
else:
@@ -227,10 +244,10 @@ def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=Fals
matches = [ file for file in files if parameter.lower() in file.lower()]
if len(matches) == 1:
file = matches[0]
- music_wrapper = get_item_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
+ music_wrapper = get_cached_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
- bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
+ bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
return
elif len(matches) > 1:
msgs = [ constants.strings('multiple_matches') ]
@@ -268,7 +285,7 @@ def cmd_play_file_match(bot, user, text, command, parameter, do_not_refresh_cach
match = re.search(parameter, file)
if match and match[0]:
count += 1
- music_wrapper = get_item_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
+ music_wrapper = get_cached_wrapper_by_id(bot, var.cache.file_id_lookup[file], user)
music_wrappers.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
msgs.append("
{} ({})".format(music_wrapper.item().title,
@@ -282,7 +299,7 @@ def cmd_play_file_match(bot, user, text, command, parameter, do_not_refresh_cach
if count != 0:
msgs.append("")
var.playlist.extend(music_wrappers)
- send_multi_lines(bot, msgs, text, "")
+ send_multi_lines(bot, msgs, None, "")
else:
if do_not_refresh_cache:
bot.send_msg(constants.strings("no_file"), text)
@@ -302,11 +319,11 @@ def cmd_play_url(bot, user, text, command, parameter):
url = util.get_url_from_input(parameter)
if url:
- music_wrapper = get_item_wrapper_from_scrap(bot, type='url', url=url, user=user)
+ music_wrapper = get_cached_wrapper_from_scrap(bot, type='url', url=url, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
- bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
+ bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
if len(var.playlist) == 2:
# If I am the second item on the playlist. (I am the next one!)
bot.async_download_next()
@@ -329,7 +346,7 @@ def cmd_play_playlist(bot, user, text, command, parameter):
items = get_playlist_info(url=url, start_index=offset, user=user)
if len(items) > 0:
items = var.playlist.extend(list(map(
- lambda item: get_item_wrapper_from_scrap(bot, **item), items)))
+ lambda item: get_cached_wrapper_from_scrap(bot, **item), items)))
for music in items:
log.info("cmd: add to playlist: " + music.format_debug_string())
else:
@@ -354,11 +371,11 @@ def cmd_play_radio(bot, user, text, command, parameter):
parameter = parameter.split()[0]
url = util.get_url_from_input(parameter)
if url:
- music_wrapper = get_item_wrapper_from_scrap(bot, type='radio', url=url, user=user)
+ music_wrapper = get_cached_wrapper_from_scrap(bot, type='radio', url=url, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
- bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
+ bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
else:
bot.send_msg(constants.strings('bad_url'))
@@ -454,7 +471,7 @@ def cmd_rb_play(bot, user, text, command, parameter):
url = radiobrowser.geturl_byid(parameter)
if url != "-1":
log.info('cmd: Found url: ' + url)
- music_wrapper = get_item_wrapper_from_scrap(bot, type='radio', url=url, name=stationname, user=user)
+ music_wrapper = get_cached_wrapper_from_scrap(bot, type='radio', url=url, name=stationname, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
bot.async_download_next()
@@ -527,7 +544,6 @@ def cmd_yt_play(bot, user, text, command, parameter):
def cmd_help(bot, user, text, command, parameter):
global log
-
bot.send_msg(constants.strings('help'), text)
if bot.is_admin(user):
bot.send_msg(constants.strings('admin_help'), text)
@@ -586,7 +602,7 @@ def cmd_volume(bot, user, text, command, parameter):
if parameter and parameter.isdigit() and 0 <= int(parameter) <= 100:
bot.volume_set = float(float(parameter) / 100)
bot.send_msg(constants.strings('change_volume',
- volume=int(bot.volume_set * 100), user=bot.mumble.users[text.actor]['name']), text)
+ volume=int(bot.volume_set * 100), user=bot.mumble.users[text.actor]['name']))
var.db.set('bot', 'volume', str(bot.volume_set))
log.info('cmd: volume set to %d' % (bot.volume_set * 100))
else:
@@ -647,12 +663,10 @@ def cmd_ducking_volume(bot, user, text, command, parameter):
def cmd_current_music(bot, user, text, command, parameter):
global log
- reply = ""
if len(var.playlist) > 0:
- bot.send_msg(var.playlist.current_item().format_current_playing())
+ bot.send_msg(var.playlist.current_item().format_current_playing(), text)
else:
- reply = constants.strings('not_playing')
- bot.send_msg(reply, text)
+ bot.send_msg(constants.strings('not_playing'), text)
def cmd_skip(bot, user, text, command, parameter):
@@ -705,7 +719,7 @@ def cmd_remove(bot, user, text, command, parameter):
log.info("cmd: delete from playlist: " + removed.format_debug_string())
else:
- bot.send_msg(constants.strings('bad_parameter', command=command))
+ bot.send_msg(constants.strings('bad_parameter', command=command), text)
def cmd_list_file(bot, user, text, command, parameter):
@@ -801,7 +815,7 @@ def cmd_mode(bot, user, text, command, parameter):
def cmd_play_tags(bot, user, text, command, parameter):
if not parameter:
- bot.send_msg(constants.strings('bad_parameter', command=command))
+ bot.send_msg(constants.strings('bad_parameter', command=command), text)
return
msgs = [constants.strings('multiple_file_added') + ""]
@@ -809,7 +823,7 @@ def cmd_play_tags(bot, user, text, command, parameter):
tags = parameter.split(",")
tags = list(map(lambda t: t.strip(), tags))
- music_wrappers = get_item_wrappers_by_tags(bot, tags, user)
+ music_wrappers = get_cached_wrappers_by_tags(bot, tags, user)
for music_wrapper in music_wrappers:
count += 1
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
@@ -819,7 +833,7 @@ def cmd_play_tags(bot, user, text, command, parameter):
if count != 0:
msgs.append("
")
var.playlist.extend(music_wrappers)
- send_multi_lines(bot, msgs, text, "")
+ send_multi_lines(bot, msgs, None, "")
else:
bot.send_msg(constants.strings("no_file"), text)
@@ -916,7 +930,7 @@ def cmd_find_tagged(bot, user, text, command, parameter):
global song_shortlist
if not parameter:
- bot.send_msg(constants.strings('bad_parameter', command=command))
+ bot.send_msg(constants.strings('bad_parameter', command=command), text)
return
msgs = [constants.strings('multiple_file_found') + ""]
@@ -943,7 +957,7 @@ def cmd_find_tagged(bot, user, text, command, parameter):
def cmd_search_library(bot, user, text, command, parameter):
global song_shortlist
if not parameter:
- bot.send_msg(constants.strings('bad_parameter', command=command))
+ bot.send_msg(constants.strings('bad_parameter', command=command), text)
return
msgs = [constants.strings('multiple_file_found') + ""]
@@ -992,7 +1006,7 @@ def cmd_shortlist(bot, user, text, command, parameter):
if 1 <= index <= len(song_shortlist):
kwargs = song_shortlist[index - 1]
kwargs['user'] = user
- music_wrapper = get_item_wrapper_from_scrap(bot, **kwargs)
+ music_wrapper = get_cached_wrapper_from_scrap(bot, **kwargs)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
msgs.append("- [{}] {}
".format(music_wrapper.item().type, music_wrapper.item().title))
@@ -1001,17 +1015,17 @@ def cmd_shortlist(bot, user, text, command, parameter):
return
msgs.append("
")
- send_multi_lines(bot, msgs, text, "")
+ send_multi_lines(bot, msgs, None, "")
return
elif len(indexes) == 1:
index = indexes[0]
if 1 <= index <= len(song_shortlist):
kwargs = song_shortlist[index - 1]
kwargs['user'] = user
- music_wrapper = get_item_wrapper_from_scrap(bot, **kwargs)
+ music_wrapper = get_cached_wrapper_from_scrap(bot, **kwargs)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
- bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
+ bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()))
return
bot.send_msg(constants.strings('bad_parameter', command=command), text)
@@ -1033,7 +1047,7 @@ def cmd_delete_from_library(bot, user, text, command, parameter):
if 1 <= index <= len(song_shortlist):
music_dict = song_shortlist[index - 1]
if 'id' in music_dict:
- music_wrapper = get_item_wrapper_by_id(bot, music_dict['id'], user)
+ music_wrapper = get_cached_wrapper_by_id(bot, music_dict['id'], user)
log.info("cmd: remove from library: " + music_wrapper.format_debug_string())
msgs.append("- [{}] {}
".format(music_wrapper.item().type ,music_wrapper.item().title))
var.playlist.remove_by_id(music_dict['id'])
@@ -1048,14 +1062,14 @@ def cmd_delete_from_library(bot, user, text, command, parameter):
return
msgs.append("
")
- send_multi_lines(bot, msgs, text, "")
+ send_multi_lines(bot, msgs, None, "")
return
elif len(indexes) == 1:
index = indexes[0]
if 1 <= index <= len(song_shortlist):
music_dict = song_shortlist[index - 1]
if 'id' in music_dict:
- music_wrapper = get_item_wrapper_by_id(bot, music_dict['id'], user)
+ music_wrapper = get_cached_wrapper_by_id(bot, music_dict['id'], user)
bot.send_msg(constants.strings('file_deleted', item=music_wrapper.format_song_string()), text)
log.info("cmd: remove from library: " + music_wrapper.format_debug_string())
var.playlist.remove_by_id(music_dict['id'])
diff --git a/configuration.default.ini b/configuration.default.ini
index 0e0a6fd..e42117c 100644
--- a/configuration.default.ini
+++ b/configuration.default.ini
@@ -168,6 +168,7 @@ add_from_shortlist = shortlist, sl
user_ban = userban
user_unban = userunban
url_ban = urlban
+url_ban_list = urlbanlist
url_unban = urlunban
ducking = duck
@@ -299,12 +300,12 @@ help = Commands
admin_help = Admin command
- !kill - kill the bot
- - !update - update the bot
- - !userban {user} - ban a user
- - !userunban {user} - unban a user
- - !urlban {url} - ban an url
- - !urlunban {url} - unban an url
- - !urlunban {url} - unban an url
+ - !update - update the bot
+ - !userban {user} - ban a user
+ - !userunban {user} - unban a user
+ - !urlbanlist {url} - list banned url
+ - !urlban [{url}] - ban {url} (or current item's url by default) and remove this url from the library.
+ - !urlunban {url} - unban {url}
- !rescan {url} - rebuild local music file cache
- !dropdatabase - clear the entire database, you will lose all settings and music library.
diff --git a/interface.py b/interface.py
index c2257dc..d1a350b 100644
--- a/interface.py
+++ b/interface.py
@@ -10,7 +10,7 @@ import shutil
from werkzeug.utils import secure_filename
import errno
import media
-from media.playlist import get_item_wrapper_from_scrap, get_item_wrapper_by_id, get_item_wrappers_by_tags
+from media.cache import get_cached_wrapper_from_scrap, get_cached_wrapper_by_id, get_cached_wrappers_by_tags
import logging
import time
@@ -199,7 +199,7 @@ def post():
if 'add_file_bottom' in request.form and ".." not in request.form['add_file_bottom']:
path = var.music_folder + request.form['add_file_bottom']
if os.path.isfile(path):
- music_wrapper = get_item_wrapper_by_id(var.bot, var.cache.file_id_lookup[request.form['add_file_bottom']], user)
+ music_wrapper = get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[request.form['add_file_bottom']], user)
var.playlist.append(music_wrapper)
log.info('web: add to playlist(bottom): ' + music_wrapper.format_debug_string())
@@ -207,7 +207,7 @@ def post():
elif 'add_file_next' in request.form and ".." not in request.form['add_file_next']:
path = var.music_folder + request.form['add_file_next']
if os.path.isfile(path):
- music_wrapper = get_item_wrapper_by_id(var.bot, var.cache.file_id_lookup[request.form['add_file_next']], user)
+ music_wrapper = get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[request.form['add_file_next']], user)
var.playlist.insert(var.playlist.current_index + 1, music_wrapper)
log.info('web: add to playlist(next): ' + music_wrapper.format_debug_string())
@@ -229,7 +229,7 @@ def post():
music_wrappers = list(map(
lambda file:
- get_item_wrapper_by_id(var.bot, var.cache.file_id_lookup[folder + file], user),
+ get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[folder + file], user),
files))
var.playlist.extend(music_wrappers)
@@ -239,7 +239,7 @@ def post():
elif 'add_url' in request.form:
- music_wrapper = get_item_wrapper_from_scrap(var.bot, type='url', url=request.form['add_url'], user=user)
+ music_wrapper = get_cached_wrapper_from_scrap(var.bot, type='url', url=request.form['add_url'], user=user)
var.playlist.append(music_wrapper)
log.info("web: add to playlist: " + music_wrapper.format_debug_string())
@@ -249,7 +249,7 @@ def post():
elif 'add_radio' in request.form:
url = request.form['add_radio']
- music_wrapper = get_item_wrapper_from_scrap(var.bot, type='radio', url=url, user=user)
+ music_wrapper = get_cached_wrapper_from_scrap(var.bot, type='radio', url=url, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
@@ -301,7 +301,7 @@ def post():
time.sleep(0.1)
elif 'add_tag' in request.form:
- music_wrappers = get_item_wrappers_by_tags(var.bot, [request.form['add_tag']], user)
+ music_wrappers = get_cached_wrappers_by_tags(var.bot, [request.form['add_tag']], user)
for music_wrapper in music_wrappers:
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
var.playlist.extend(music_wrappers)
diff --git a/media/cache.py b/media/cache.py
index 736810e..b2d5e71 100644
--- a/media/cache.py
+++ b/media/cache.py
@@ -1,4 +1,6 @@
import logging
+import os
+
from database import MusicDatabase
import json
import threading
@@ -90,6 +92,8 @@ class MusicCache(dict):
del self.file_id_lookup[item.path]
self.files.remove(item.path)
self.save_dir_cache()
+ elif item.type == 'url':
+ os.remove(item.path)
if item.id in self:
del self[item.id]
@@ -139,3 +143,115 @@ class MusicCache(dict):
self.dir.add_file(file)
self.dir_lock.release()
+
+class CachedItemWrapper:
+ def __init__(self, lib, id, type, user):
+ self.lib = lib
+ self.id = id
+ self.user = user
+ self.type = type
+ self.log = logging.getLogger("bot")
+ self.version = 0
+
+ def item(self):
+ return self.lib[self.id]
+
+ def to_dict(self):
+ dict = self.item().to_dict()
+ dict['user'] = self.user
+ return dict
+
+ def validate(self):
+ ret = self.item().validate()
+ if ret and self.item().version > self.version:
+ self.version = self.item().version
+ self.lib.save(self.id)
+ return ret
+
+ def prepare(self):
+ ret = self.item().prepare()
+ if ret and self.item().version > self.version:
+ self.version = self.item().version
+ self.lib.save(self.id)
+ return ret
+
+ def async_prepare(self):
+ th = threading.Thread(
+ target=self.prepare, name="Prepare-" + self.id[:7])
+ self.log.info(
+ "%s: start preparing item in thread: " % self.item().type + self.format_debug_string())
+ th.daemon = True
+ th.start()
+ return th
+
+ def uri(self):
+ return self.item().uri()
+
+ def add_tags(self, tags):
+ self.item().add_tags(tags)
+ if self.item().version > self.version:
+ self.version = self.item().version
+ self.lib.save(self.id)
+
+ def remove_tags(self, tags):
+ self.item().remove_tags(tags)
+ if self.item().version > self.version:
+ self.version = self.item().version
+ self.lib.save(self.id)
+
+ def clear_tags(self):
+ self.item().clear_tags()
+ if self.item().version > self.version:
+ self.version = self.item().version
+ self.lib.save(self.id)
+
+ def is_ready(self):
+ return self.item().is_ready()
+
+ def is_failed(self):
+ return self.item().is_failed()
+
+ def format_current_playing(self):
+ return self.item().format_current_playing(self.user)
+
+ def format_song_string(self):
+ return self.item().format_song_string(self.user)
+
+ def format_short_string(self):
+ return self.item().format_short_string()
+
+ def format_debug_string(self):
+ return self.item().format_debug_string()
+
+ def display_type(self):
+ return self.item().display_type()
+
+
+# Remember!!! Get wrapper functions will automatically add items into the cache!
+def get_cached_wrapper_from_scrap(bot, **kwargs):
+ item = var.cache.get_item(bot, **kwargs)
+ if 'user' not in kwargs:
+ raise KeyError("Which user added this song?")
+ return CachedItemWrapper(var.cache, item.id, kwargs['type'], kwargs['user'])
+
+
+def get_cached_wrapper_from_dict(bot, dict_from_db, user):
+ item = dict_to_item(bot, dict_from_db)
+ var.cache[dict_from_db['id']] = item
+ return CachedItemWrapper(var.cache, item.id, item.type, user)
+
+
+def get_cached_wrapper_by_id(bot, id, user):
+ item = var.cache.get_item_by_id(bot, id)
+ if item:
+ return CachedItemWrapper(var.cache, item.id, item.type, user)
+ else:
+ return None
+
+
+def get_cached_wrappers_by_tags(bot, tags, user):
+ items = var.cache.get_items_by_tags(bot, tags)
+ ret = []
+ for item in items:
+ ret.append(CachedItemWrapper(var.cache, item.id, item.type, user))
+ return ret
\ No newline at end of file
diff --git a/media/playlist.py b/media/playlist.py
index eb03003..4f49228 100644
--- a/media/playlist.py
+++ b/media/playlist.py
@@ -1,127 +1,12 @@
import json
-import random
import threading
import logging
import random
import time
import variables as var
-from media.file import FileItem
-from media.item import dict_to_item
-from media.url import URLItem
-from media.url_from_playlist import PlaylistURLItem
-from media.radio import RadioItem
-from database import MusicDatabase
-from media.cache import MusicCache
+from media.cache import CachedItemWrapper, get_cached_wrapper_from_dict, get_cached_wrapper_by_id
-class PlaylistItemWrapper:
- def __init__(self, lib, id, type, user):
- self.lib = lib
- self.id = id
- self.user = user
- self.type = type
- self.log = logging.getLogger("bot")
- self.version = 0
-
- def item(self):
- return self.lib[self.id]
-
- def to_dict(self):
- dict = self.item().to_dict()
- dict['user'] = self.user
- return dict
-
- def validate(self):
- ret = self.item().validate()
- if ret and self.item().version > self.version:
- self.version = self.item().version
- self.lib.save(self.id)
- return ret
-
- def prepare(self):
- ret = self.item().prepare()
- if ret and self.item().version > self.version:
- self.version = self.item().version
- self.lib.save(self.id)
- return ret
-
- def async_prepare(self):
- th = threading.Thread(
- target=self.prepare, name="Prepare-" + self.id[:7])
- self.log.info(
- "%s: start preparing item in thread: " % self.item().type + self.format_debug_string())
- th.daemon = True
- th.start()
- return th
-
- def uri(self):
- return self.item().uri()
-
- def add_tags(self, tags):
- self.item().add_tags(tags)
- if self.item().version > self.version:
- self.version = self.item().version
- self.lib.save(self.id)
-
- def remove_tags(self, tags):
- self.item().remove_tags(tags)
- if self.item().version > self.version:
- self.version = self.item().version
- self.lib.save(self.id)
-
- def clear_tags(self):
- self.item().clear_tags()
- if self.item().version > self.version:
- self.version = self.item().version
- self.lib.save(self.id)
-
- def is_ready(self):
- return self.item().is_ready()
-
- def is_failed(self):
- return self.item().is_failed()
-
- def format_current_playing(self):
- return self.item().format_current_playing(self.user)
-
- def format_song_string(self):
- return self.item().format_song_string(self.user)
-
- def format_short_string(self):
- return self.item().format_short_string()
-
- def format_debug_string(self):
- return self.item().format_debug_string()
-
- def display_type(self):
- return self.item().display_type()
-
-
-# Remember!!! Using these three get wrapper functions will automatically add items into the cache!
-def get_item_wrapper_from_scrap(bot, **kwargs):
- item = var.cache.get_item(bot, **kwargs)
- if 'user' not in kwargs:
- raise KeyError("Which user added this song?")
- return PlaylistItemWrapper(var.cache, item.id, kwargs['type'], kwargs['user'])
-
-def get_item_wrapper_from_dict(bot, dict_from_db, user):
- item = dict_to_item(bot, dict_from_db)
- var.cache[dict_from_db['id']] = item
- return PlaylistItemWrapper(var.cache, item.id, item.type, user)
-
-def get_item_wrapper_by_id(bot, id, user):
- item = var.cache.get_item_by_id(bot, id)
- if item:
- return PlaylistItemWrapper(var.cache, item.id, item.type, user)
- else:
- return None
-
-def get_item_wrappers_by_tags(bot, tags, user):
- items = var.cache.get_items_by_tags(bot, tags)
- ret = []
- for item in items:
- ret.append(PlaylistItemWrapper(var.cache, item.id, item.type, user))
- return ret
def get_playlist(mode, _list=None, index=None):
if _list and index is None:
@@ -147,6 +32,7 @@ def get_playlist(mode, _list=None, index=None):
return AutoPlaylist().from_list(_list, index)
raise
+
class BasePlaylist(list):
def __init__(self):
super().__init__()
@@ -168,7 +54,7 @@ class BasePlaylist(list):
return self
- def append(self, item: PlaylistItemWrapper):
+ def append(self, item: CachedItemWrapper):
self.version += 1
super().append(item)
self.pending_items.append(item)
@@ -310,7 +196,7 @@ class BasePlaylist(list):
items.sort(key=lambda v: int(v[0]))
for item in items:
item = json.loads(item[1])
- music_wrapper = get_item_wrapper_by_id(var.bot, item['id'], item['user'])
+ music_wrapper = get_cached_wrapper_by_id(var.bot, item['id'], item['user'])
if music_wrapper:
music_wrappers.append(music_wrapper)
self.from_list(music_wrappers, current_index)
@@ -457,7 +343,7 @@ class AutoPlaylist(OneshotPlaylist):
def refresh(self):
dicts = var.music_db.query_random_music(var.config.getint("bot", "autoplay_length", fallback=5))
if dicts:
- _list = [get_item_wrapper_from_dict(var.bot, _dict, "AutoPlay") for _dict in dicts]
+ _list = [get_cached_wrapper_from_dict(var.bot, _dict, "AutoPlay") for _dict in dicts]
self.from_list(_list, -1)
# def from_list(self, _list, current_index):
diff --git a/mumbleBot.py b/mumbleBot.py
index e8fc45e..17b1bf6 100644
--- a/mumbleBot.py
+++ b/mumbleBot.py
@@ -272,7 +272,7 @@ class MumbleBot:
self.log.info("bot: {:s} matches {:s}".format(command, matches[0]))
command_exc = matches[0]
- if not self.cmd_handle[command]['access_outside_channel'] \
+ if not self.cmd_handle[command_exc]['access_outside_channel'] \
and not self.is_admin(user) \
and not var.config.getboolean('bot', 'allow_other_channel_message') \
and self.mumble.users[text.actor]['channel_id'] != self.mumble.users.myself[
@@ -297,7 +297,7 @@ class MumbleBot:
def send_msg(self, msg, text=None):
msg = msg.encode('utf-8', 'ignore').decode('utf-8')
# text if the object message, contain information if direct message or channel message
- if not text or not text.session:
+ if not text:
own_channel = self.mumble.channels[self.mumble.users.myself['channel_id']]
own_channel.send_text_message(msg)
else:
diff --git a/util.py b/util.py
index f58d332..8738131 100644
--- a/util.py
+++ b/util.py
@@ -153,7 +153,7 @@ def user_unban(user):
def get_url_ban():
- res = "List of ban hash"
+ res = "List of ban:"
for i in var.db.items("url_ban"):
res += "
" + i[0]
return res