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