]> git.0d.be Git - botaradio.git/commitdiff
feat: add tags, remove tags, play tags, find tags #91
authorTerry Geng <gengyanda@gmail.com>
Sun, 8 Mar 2020 03:31:34 +0000 (11:31 +0800)
committerTerry Geng <gengyanda@gmail.com>
Sun, 8 Mar 2020 03:31:34 +0000 (11:31 +0800)
command.py
configuration.default.ini
media/item.py
media/library.py
media/playlist.py

index b8e9eed7e2d9d85e9906adf4a45bc7956bca7cb2..f94300ea0626e6169dc0ec0678c622e9d00323bd 100644 (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
+
+    params = parameter.split()
+    if len(params) == 2:
+        index = params[0]
+        tags = list(map(lambda t: t.strip(), params[1].split(",")))
 
-def cmd_untag(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_list_tagged(bot, user, text, command, parameter):
-    pass
+
+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
index 532e73a88a958a168867ff0c3da7a80821fc04ab..508cbd80350364b2c7940f0c31d9345ecc2fac98 100644 (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>
index 059cfbff235690fea3567e37f69e00e37819fdca..2ca6b7fbfa8b107a7a886373e4b09ad5e2877dd7 100644 (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 remove_tag(self, tag):
-        if tag not in self.tags:
-            self.tags.remove(tag)
+    def add_tags(self, tags):
+        for tag in tags:
+            if tag not in self.tags:
+                self.tags.append(tag)
+                self.version += 1
+
+    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):
index 229634c22dd180c1eb3f0fa367f9f37a7f13d041..6329f989d39997b5c446dbee7ace8b1b1af16ffb 100644 (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
 
index 1cb61ddd494c3f8ce496eccfe96a37367ac580c8..dc489feed40497afc99601bd3c0d22ad40bfed70 100644 (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)