feat: add tags, remove tags, play tags, find tags #91

This commit is contained in:
Terry Geng 2020-03-08 11:31:34 +08:00
parent 487b5b9616
commit f8cfb163ed
5 changed files with 140 additions and 27 deletions

View File

@ -31,6 +31,7 @@ def register_all_commands(bot):
bot.register_command(constants.commands('play_url'), cmd_play_url)
bot.register_command(constants.commands('play_playlist'), cmd_play_playlist)
bot.register_command(constants.commands('play_radio'), cmd_play_radio)
bot.register_command(constants.commands('play_tag'), cmd_play_tags)
bot.register_command(constants.commands('rb_query'), cmd_rb_query)
bot.register_command(constants.commands('rb_play'), cmd_rb_play)
bot.register_command(constants.commands('yt_search'), cmd_yt_search)
@ -54,6 +55,9 @@ def register_all_commands(bot):
bot.register_command(constants.commands('random'), cmd_random)
bot.register_command(constants.commands('repeat'), cmd_repeat)
bot.register_command(constants.commands('mode'), cmd_mode)
bot.register_command(constants.commands('add_tag'), cmd_add_tag)
bot.register_command(constants.commands('remove_tag'), cmd_remove_tag)
bot.register_command(constants.commands('find_tagged'), cmd_find_tagged)
bot.register_command(constants.commands('drop_database'), cmd_drop_database, True)
bot.register_command(constants.commands('recache'), cmd_refresh_cache, True)
@ -719,12 +723,15 @@ def cmd_queue(bot, user, text, command, parameter):
msgs = [ constants.strings('queue_contents')]
for i, music in enumerate(var.playlist):
newline = ''
tags = ''
if len(music.item().tags) > 0:
tags = "<sup>{}</sup>".format(", ".join(music.item().tags))
if i == var.playlist.current_index:
newline = "<b style='color:orange'>{} ({}) {} </b>".format(i + 1, music.display_type(),
music.format_short_string())
newline = "<b style='color:orange'>{} ({}) {} </b> {}".format(i + 1, music.display_type(),
music.format_short_string(), tags)
else:
newline = '<b>{}</b> ({}) {}'.format(i + 1, music.display_type(),
music.format_short_string())
newline = '<b>{}</b> ({}) {} {}'.format(i + 1, music.display_type(),
music.format_short_string(), tags)
msgs.append(newline)
@ -781,7 +788,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)
music_wrappers = get_item_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())
@ -796,14 +803,93 @@ def cmd_play_tags(bot, user, text, command, parameter):
bot.send_msg(constants.strings("no_file"), text)
def cmd_tag(bot, user, text, command, parameter):
pass
def cmd_add_tag(bot, user, text, command, parameter):
global log
def cmd_untag(bot, user, text, command, parameter):
pass
params = parameter.split()
if len(params) == 2:
index = params[0]
tags = list(map(lambda t: t.strip(), params[1].split(",")))
def cmd_list_tagged(bot, user, text, command, parameter):
pass
if index.isdigit() and 1 <= int(index) <= len(var.playlist):
var.playlist[int(index) - 1].add_tags(tags)
log.info("cmd: add tags %s to song %s" % (", ".join(tags),
var.playlist[int(index) - 1].format_debug_string()))
bot.send_msg(constants.strings("added_tags",
tags=", ".join(tags),
song=var.playlist[int(index) - 1].format_short_string()), text)
elif index == "*":
for item in var.playlist:
item.add_tags(tags)
log.info("cmd: add tags %s to song %s" % (", ".join(tags),
item.format_debug_string()))
bot.send_msg(constants.strings("added_tags_to_all", tags=", ".join(tags)), text)
else:
bot.send_msg(constants.strings('bad_parameter', command=command), text)
def cmd_remove_tag(bot, user, text, command, parameter):
global log
params = parameter.split()
if len(params) == 2 and params[1]:
index = params[0]
if index.isdigit() and 1 <= int(index) <= len(var.playlist):
if params[1] != "*":
tags = list(map(lambda t: t.strip(), params[1].split(",")))
var.playlist[int(index) - 1].remove_tags(tags)
log.info("cmd: remove tags %s from song %s" % (", ".join(tags),
var.playlist[int(index) - 1].format_debug_string()))
bot.send_msg(constants.strings("removed_tags",
tags=", ".join(tags),
song=var.playlist[int(index) - 1].format_short_string()), text)
return
else:
var.playlist[int(index) - 1].clear_tags()
log.info("cmd: clear tags from song %s" % (var.playlist[int(index) - 1].format_debug_string()))
bot.send_msg(constants.strings("cleared_tags",
song=var.playlist[int(index) - 1].format_short_string()), text)
return
elif index == "*":
if params[1] != "*":
tags = list(map(lambda t: t.strip(), params[1].split(",")))
for item in var.playlist:
item.remove_tags(tags)
log.info("cmd: remove tags %s from song %s" % (", ".join(tags),
item.format_debug_string()))
bot.send_msg(constants.strings("removed_tags_from_all", tags=", ".join(tags)), text)
return
else:
for item in var.playlist:
item.clear_tags()
log.info("cmd: clear tags from song %s" % (item.format_debug_string()))
bot.send_msg(constants.strings("cleared_tags_from_all"), text)
return
bot.send_msg(constants.strings('bad_parameter', command=command), text)
def cmd_find_tagged(bot, user, text, command, parameter):
if not parameter:
bot.send_msg(constants.strings('bad_parameter', command=command))
return
msgs = [constants.strings('multiple_file_found') + "<ul>"]
count = 0
tags = parameter.split(",")
tags = list(map(lambda t: t.strip(), tags))
music_wrappers = get_item_wrappers_by_tags(bot, tags, user)
for music_wrapper in music_wrappers:
count += 1
msgs.append("<li><b>{}</b> (<i>{}</i>)</li>".format(music_wrapper.item().title, ", ".join(music_wrapper.item().tags)))
if count != 0:
msgs.append("</ul>")
send_multi_lines(bot, msgs, text, "")
else:
bot.send_msg(constants.strings("no_file"), text)
def cmd_drop_database(bot, user, text, command, parameter):
global log

View File

@ -156,6 +156,11 @@ mode = mode
update = update
list_file = listfile
play_tag = tag
add_tag = addtag
remove_tag = untag
find_tagged = findtagged, ft
user_ban = userban
user_unban = userunban
url_ban = urlban
@ -182,6 +187,7 @@ no_file = File not found.
wrong_pattern = Invalid regex: {error}.
file_added = Added: {item}.
multiple_file_added = Multiple files added:
multiple_file_found = Found:
bad_url = Bad URL requested.
preconfigurated_radio = Preconfigurated Radio available:
unable_download = Error while downloading music...
@ -225,6 +231,12 @@ yt_no_more = No more results!
yt_query_error = Unable to query youtube!
playlist_fetching_failed = Unable to fetch the playlist!
cache_refreshed = Cache refreshed!
added_tags = Added tags <i>{tags}</i> to <b>{song}</b>.
added_tags_to_all = Added tags <i>{tags}</i> to songs on the playlist.
removed_tags = Removed tags <i>{tags}</i> from <b>{song}</b>.
removed_tags_from_all = Removed tags <i>{tags}</i> from songs on the playlist.
cleared_tags = Removed all tags from <b>{song}</b>.
cleared_tags_from_all = Removed all tags from songs on the playlist.
help = <h3>Commands</h3>
<b>Control</b>

View File

@ -44,6 +44,7 @@ class BaseItem:
else:
self.id = from_dict['id']
self.ready = from_dict['ready']
self.tags = from_dict['tags']
def is_ready(self):
return True if self.ready == "yes" else False
@ -60,14 +61,21 @@ class BaseItem:
def prepare(self):
return True
def add_tag(self, tag):
if tag not in self.tags:
self.tags.append(tag)
self.version += 1
def add_tags(self, tags):
for tag in tags:
if tag not in self.tags:
self.tags.append(tag)
self.version += 1
def remove_tag(self, tag):
if tag not in self.tags:
self.tags.remove(tag)
def remove_tags(self, tags):
for tag in tags:
if tag in self.tags:
self.tags.remove(tag)
self.version += 1
def clear_tags(self):
if len(self.tags) > 0:
self.tags = []
self.version += 1
def format_song_string(self, user):

View File

@ -51,11 +51,12 @@ class MusicLibrary(dict):
def get_items_by_tags(self, bot, tags):
music_dicts = self.db.query_music_by_tags(tags)
items = []
for music_dict in music_dicts:
id = music_dicts['id']
type = music_dict['type']
self[id] = item_loaders[type](bot, music_dict)
items.append(self[id])
if music_dicts:
for music_dict in music_dicts:
id = music_dict['id']
type = music_dict['type']
self[id] = item_loaders[type](bot, music_dict)
items.append(self[id])
return items

View File

@ -55,14 +55,20 @@ class PlaylistItemWrapper:
def uri(self):
return self.item().uri()
def add_tag(self, tag):
self.item().add_tag(tag)
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_tag(self, tag):
self.item().remove_tag(tag)
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)