* add more ducking command * fix current music command * provide more controls, like pause, resume, clear. * add more controls in the web interface * refactored and improved: 1. move get_music_tag_info to util, and 2. refined logic related to it. 3. now playlist will check for tag info thus update_music_tag_info is useless and was removed 4. add "add folder" feature to !file asked in #65, 5. fix bugs related to !file * truncate file list if too long * fixed several tiny bugs * fixed several tiny bugs continue * fixed several tiny bugs continue continue * fixed several tiny bugs continue**3 * fixed several tiny bugs continue**4 * added !filematch command to add files that match a regex pattern. * truncate long message * fix web interface delete file issue * refresh after delete file * add regex to listfile command * refactored command part, added partial match support for commands * organized * added random command * typo * typo * Fixed many bugs. * Added workaround for azlux/pymumble#44 to fix the memory leak. * changed logging style. * fixed bugs related to random and resume * fix now playing * fixed issue related to download * fixed issue related to download 2 * fixed thumbnail issue * fixed add url problem in web interface * REFACTORED, turned db.ini into sqlite3 database. * fixed remove song problem * fixed radio get title problem. auto download if tmp file is deleted * fixed current index not loaded from database * fixed: order of songs loaded from the database * fixed: some obscure bugs. beautified error of commands * added a workaround for TerryGeng/botamusique#1. * beautified * fixed: channel not loaded in the config * fixed: auto checked for updates * fixed: mysterious bug: sometimes "now playing" string cannot be properly displayed. The real reason is: do use <br />, do not use <br>. I tried hours to find out this. * chore: unified debug messages that refer to music items * feav: fetch ffmpeg stderr mentioned in #72, reformatted logs. * fix: async download not working * fix: async download not working, still * fix: async download not working, finished * feat: queue command: ▶current playing item◀ * feat: support more than one command prefix * chore: added some WARNINGs into default config file to avoid people to touch it. * refactor: packed all string contants into constants.py, just to avoid people messing it around. * refactor: required by azlux. Added a configuration.example.ini to keep people away from configuration.default.ini
192 lines
6.1 KiB
Python
192 lines
6.1 KiB
Python
import youtube_dl
|
|
import variables as var
|
|
import util
|
|
import random
|
|
import json
|
|
|
|
class PlayList:
|
|
playlist = []
|
|
current_index = 0
|
|
version = 0 # increase by one after each change
|
|
|
|
def append(self, item):
|
|
self.version += 1
|
|
item = util.get_music_tag_info(item)
|
|
self.playlist.append(item)
|
|
|
|
return item
|
|
|
|
def insert(self, index, item):
|
|
self.version += 1
|
|
|
|
if index == -1:
|
|
index = self.current_index
|
|
|
|
item = util.get_music_tag_info(item)
|
|
self.playlist.insert(index, item)
|
|
|
|
if index <= self.current_index:
|
|
self.current_index += 1
|
|
|
|
return item
|
|
|
|
def length(self):
|
|
return len(self.playlist)
|
|
|
|
def extend(self, items):
|
|
self.version += 1
|
|
items = list(map(
|
|
lambda item: util.get_music_tag_info(item),
|
|
items))
|
|
self.playlist.extend(items)
|
|
return items
|
|
|
|
def next(self):
|
|
self.version += 1
|
|
if len(self.playlist) == 0:
|
|
return False
|
|
|
|
self.current_index = self.next_index()
|
|
|
|
return self.playlist[self.current_index]
|
|
|
|
def update(self, item, index=-1):
|
|
self.version += 1
|
|
if index == -1:
|
|
index = self.current_index
|
|
self.playlist[index] = item
|
|
|
|
def remove(self, index=-1):
|
|
self.version += 1
|
|
if index > len(self.playlist) - 1:
|
|
return False
|
|
|
|
if index == -1:
|
|
index = self.current_index
|
|
|
|
removed = self.playlist[index]
|
|
del self.playlist[index]
|
|
|
|
if self.current_index > index:
|
|
self.current_index -= 1
|
|
|
|
return removed
|
|
|
|
def current_item(self):
|
|
return self.playlist[self.current_index]
|
|
|
|
def next_index(self):
|
|
if len(self.playlist) == 0:
|
|
return False
|
|
|
|
if self.current_index < len(self.playlist) - 1:
|
|
return self.current_index + 1
|
|
else:
|
|
return 0
|
|
|
|
def next_item(self):
|
|
if len(self.playlist) == 0:
|
|
return False
|
|
|
|
return self.playlist[self.next_index()]
|
|
|
|
def jump(self, index):
|
|
self.version += 1
|
|
self.current_index = index
|
|
return self.playlist[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]
|
|
|
|
random.shuffle(self.playlist)
|
|
|
|
#self.playlist.insert(0, current)
|
|
self.current_index = 0
|
|
self.version += 1
|
|
|
|
def clear(self):
|
|
self.version += 1
|
|
self.playlist = []
|
|
self.current_index = 0
|
|
|
|
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):
|
|
var.db.set("playlist_item", str(index), json.dumps(item))
|
|
|
|
def load(self):
|
|
current_index = var.db.getint("playlist", "current_index", fallback=-1)
|
|
if current_index == -1:
|
|
return
|
|
|
|
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.current_index = current_index
|
|
|
|
|
|
def get_playlist_info(url, start_index=0, user=""):
|
|
items = []
|
|
ydl_opts = {
|
|
'extract_flat': 'in_playlist'
|
|
}
|
|
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
|
for i in range(2):
|
|
try:
|
|
info = ydl.extract_info(url, download=False)
|
|
# # if url is not a playlist but a video
|
|
# if 'entries' not in info and 'webpage_url' in info:
|
|
# music = {'type': 'url',
|
|
# 'title': info['title'],
|
|
# 'url': info['webpage_url'],
|
|
# 'user': user,
|
|
# 'ready': 'validation'}
|
|
# items.append(music)
|
|
# return items
|
|
|
|
playlist_title = info['title']
|
|
for j in range(start_index, min(len(info['entries']), start_index + var.config.getint('bot', 'max_track_playlist'))):
|
|
# Unknow String if No title into the json
|
|
title = info['entries'][j]['title'] if 'title' in info['entries'][j] else "Unknown Title"
|
|
# Add youtube url if the url in the json isn't a full url
|
|
url = info['entries'][j]['url'] if info['entries'][j]['url'][0:4] == 'http' else "https://www.youtube.com/watch?v=" + info['entries'][j]['url']
|
|
|
|
music = {'type': 'url',
|
|
'title': title,
|
|
'url': url,
|
|
'user': user,
|
|
'from_playlist': True,
|
|
'playlist_title': playlist_title,
|
|
'playlist_url': url,
|
|
'ready': 'validation'}
|
|
items.append(music)
|
|
except:
|
|
pass
|
|
|
|
return items
|
|
|
|
# def get_music_info(index=0):
|
|
# ydl_opts = {
|
|
# 'playlist_items': str(index)
|
|
# }
|
|
# with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
|
# for i in range(2):
|
|
# try:
|
|
# info = ydl.extract_info(var.playlist.playlist[index]['url'], download=False)
|
|
# # Check if the Duration is longer than the config
|
|
# if var.playlist[index]['current_index'] == index:
|
|
# var.playlist[index]['current_duration'] = info['entries'][0]['duration'] / 60
|
|
# var.playlist[index]['current_title'] = info['entries'][0]['title']
|
|
# # Check if the Duration of the next music is longer than the config (async download)
|
|
# elif var.playlist[index]['current_index'] == index - 1:
|
|
# var.playlist[index]['next_duration'] = info['entries'][0]['duration'] / 60
|
|
# var.playlist[index]['next_title'] = info['entries'][0]['title']
|
|
# except youtube_dl.utils.DownloadError:
|
|
# pass
|
|
# else:
|
|
# return True
|
|
# return False
|