diff --git a/command.py b/command.py index d8d98bb..8fe4015 100644 --- a/command.py +++ b/command.py @@ -113,7 +113,7 @@ def cmd_url_unban(bot, user, text, command, parameter): def cmd_play(bot, user, text, command, parameter): if var.playlist.length() > 0: if parameter is not None and parameter.isdigit() and int(parameter) > 0 \ - and int(parameter) <= len(var.playlist.playlist): + and int(parameter) <= len(var.playlist): bot.stop() bot.launch_music(int(parameter) - 1) elif bot.is_pause: @@ -575,12 +575,12 @@ def cmd_list_file(bot, user, text, command, parameter): def cmd_queue(bot, user, text, command, parameter): - if len(var.playlist.playlist) == 0: + if len(var.playlist) == 0: msg = constants.strings('queue_empty') bot.send_msg(msg, text) else: msgs = [ constants.strings('queue_contents')] - for i, value in enumerate(var.playlist.playlist): + for i, value in enumerate(var.playlist): newline = '' if i == var.playlist.current_index: newline = '{} ▶ ({}) {} ◀'.format(i + 1, value['type'], diff --git a/interface.py b/interface.py index bc09191..e92aa1e 100644 --- a/interface.py +++ b/interface.py @@ -122,7 +122,7 @@ def playlist(): items = [] - for index, item in enumerate(var.playlist.playlist): + for index, item in enumerate(var.playlist): items.append(render_template('playlist.html', index=index, m=item, @@ -216,7 +216,7 @@ def post(): logging.info("web: add to playlist: " + util.format_debug_song_string(music)) elif 'delete_music' in request.form: - music = var.playlist.playlist[int(request.form['delete_music'])] + music = var.playlist[int(request.form['delete_music'])] logging.info("web: delete from playlist: " + util.format_debug_song_string(music)) if var.playlist.length() >= int(request.form['delete_music']): @@ -230,10 +230,10 @@ def post(): elif 'play_music' in request.form: - music = var.playlist.playlist[int(request.form['play_music'])] + music = var.playlist[int(request.form['play_music'])] logging.info("web: jump to: " + util.format_debug_song_string(music)) - if len(var.playlist.playlist) >= int(request.form['play_music']): + if len(var.playlist) >= int(request.form['play_music']): var.botamusique.stop() var.botamusique.launch_music(int(request.form['play_music'])) diff --git a/media/playlist.py b/media/playlist.py index 0a284f0..fbb833a 100644 --- a/media/playlist.py +++ b/media/playlist.py @@ -3,16 +3,19 @@ import variables as var import util import random import json +import logging -class PlayList: - playlist = [] +class PlayList(list): current_index = 0 version = 0 # increase by one after each change + def __init__(self, *args): + super().__init__(*args) + def append(self, item): self.version += 1 item = util.get_music_tag_info(item) - self.playlist.append(item) + super().append(item) return item @@ -23,7 +26,7 @@ class PlayList: index = self.current_index item = util.get_music_tag_info(item) - self.playlist.insert(index, item) + super().insert(index, item) if index <= self.current_index: self.current_index += 1 @@ -31,41 +34,46 @@ class PlayList: return item def length(self): - return len(self.playlist) + return len(self) def extend(self, items): self.version += 1 items = list(map( lambda item: util.get_music_tag_info(item), items)) - self.playlist.extend(items) + super().extend(items) return items def next(self): self.version += 1 - if len(self.playlist) == 0: + if len(self) == 0: return False + logging.debug("playlist: Next into the queue") + self.current_index = self.next_index() - return self.playlist[self.current_index] + return self[self.current_index] def update(self, item, index=-1): self.version += 1 if index == -1: index = self.current_index - self.playlist[index] = item + self[index] = item + + def __delitem__(self, key): + return self.remove(key) def remove(self, index=-1): self.version += 1 - if index > len(self.playlist) - 1: + if index > len(self) - 1: return False if index == -1: index = self.current_index - removed = self.playlist[index] - del self.playlist[index] + removed = self[index] + super().__delitem__(index) if self.current_index > index: self.current_index -= 1 @@ -73,48 +81,48 @@ class PlayList: return removed def current_item(self): - return self.playlist[self.current_index] + return self[self.current_index] def next_index(self): - if len(self.playlist) == 0: + if len(self) == 0: return False - if self.current_index < len(self.playlist) - 1: + if self.current_index < len(self) - 1: return self.current_index + 1 else: return 0 def next_item(self): - if len(self.playlist) == 0: + if len(self) == 0: return False - return self.playlist[self.next_index()] + return self[self.next_index()] def jump(self, index): self.version += 1 self.current_index = index - return self.playlist[index] + return self[index] def randomize(self): # current_index will lose track after shuffling, thus we take current music out before shuffling #current = self.current_item() - #del self.playlist[self.current_index] + #del self[self.current_index] - random.shuffle(self.playlist) + random.shuffle(self) - #self.playlist.insert(0, current) + #self.insert(0, current) self.current_index = 0 self.version += 1 def clear(self): self.version += 1 - self.playlist = [] self.current_index = 0 + self.clear() def save(self): var.db.remove_section("playlist_item") var.db.set("playlist", "current_index", self.current_index) - for index, item in enumerate(self.playlist): + for index, item in enumerate(self): var.db.set("playlist_item", str(index), json.dumps(item)) def load(self): @@ -124,7 +132,7 @@ class PlayList: items = list(var.db.items("playlist_item")) items.sort(key=lambda v: int(v[0])) - self.playlist = list(map(lambda v: json.loads(v[1]), items)) + self.extend(list(map(lambda v: json.loads(v[1]), items))) self.current_index = current_index diff --git a/mumbleBot.py b/mumbleBot.py index 7f57ce7..2cafa87 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -422,7 +422,7 @@ class MumbleBot: def download_music(self, index=-1): if index == -1: index = var.playlist.current_index - music = var.playlist.playlist[index] + music = var.playlist[index] if music['type'] != 'url': # then no need to download @@ -496,7 +496,7 @@ class MumbleBot: def check_item_path_or_remove(self, index = -1): if index == -1: index = var.playlist.current_index - music = var.playlist.playlist[index] + music = var.playlist[index] if music['type'] == 'radio': return True @@ -563,8 +563,8 @@ class MumbleBot: if self.is_playing: # get next music self.is_playing = False - if not self.is_pause and len(var.playlist.playlist) > 0: - self.next() + if not self.is_pause and len(var.playlist) > 0: + var.playlist.next() self.launch_music() self.async_download_next() @@ -604,10 +604,6 @@ class MumbleBot: # Play Control # ======================= - def next(self): - logging.debug("bot: Next into the queue") - return var.playlist.next() - def clear(self): # Kill the ffmpeg thread and empty the playlist if self.thread: @@ -626,7 +622,7 @@ class MumbleBot: self.is_pause = True self.song_start_at = -1 self.playhead = 0 - self.next() + var.playlist.next() logging.info("bot: music stopped.") def pause(self):