From f8cfb163ed460183e2a5b4ef3c5f5d008d4751b2 Mon Sep 17 00:00:00 2001 From: Terry Geng Date: Sun, 8 Mar 2020 11:31:34 +0800 Subject: [PATCH] feat: add tags, remove tags, play tags, find tags #91 --- command.py | 108 ++++++++++++++++++++++++++++++++++---- configuration.default.ini | 12 +++++ media/item.py | 22 +++++--- media/library.py | 11 ++-- media/playlist.py | 14 +++-- 5 files changed, 140 insertions(+), 27 deletions(-) diff --git a/command.py b/command.py index b8e9eed..f94300e 100644 --- a/command.py +++ b/command.py @@ -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 = "{}".format(", ".join(music.item().tags)) if i == var.playlist.current_index: - newline = "{} ({}) {} ".format(i + 1, music.display_type(), - music.format_short_string()) + newline = "{} ({}) {} {}".format(i + 1, music.display_type(), + music.format_short_string(), tags) else: - newline = '{} ({}) {}'.format(i + 1, music.display_type(), - music.format_short_string()) + newline = '{} ({}) {} {}'.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') + "") + 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 diff --git a/configuration.default.ini b/configuration.default.ini index 532e73a..508cbd8 100644 --- a/configuration.default.ini +++ b/configuration.default.ini @@ -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 {tags} to {song}. +added_tags_to_all = Added tags {tags} to songs on the playlist. +removed_tags = Removed tags {tags} from {song}. +removed_tags_from_all = Removed tags {tags} from songs on the playlist. +cleared_tags = Removed all tags from {song}. +cleared_tags_from_all = Removed all tags from songs on the playlist. help =

Commands

Control diff --git a/media/item.py b/media/item.py index 059cfbf..2ca6b7f 100644 --- a/media/item.py +++ b/media/item.py @@ -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): diff --git a/media/library.py b/media/library.py index 229634c..6329f98 100644 --- a/media/library.py +++ b/media/library.py @@ -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 diff --git a/media/playlist.py b/media/playlist.py index 1cb61dd..dc489fe 100644 --- a/media/playlist.py +++ b/media/playlist.py @@ -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)