feat: web interface more tag support
This commit is contained in:
parent
2faa9dc8d3
commit
32b2875e52
19
database.py
19
database.py
@ -185,6 +185,18 @@ class MusicDatabase:
|
|||||||
conn.close()
|
conn.close()
|
||||||
return list(map(lambda i: i[0], results))
|
return list(map(lambda i: i[0], results))
|
||||||
|
|
||||||
|
def query_all_tags(self):
|
||||||
|
conn = sqlite3.connect(self.db_path)
|
||||||
|
cursor = conn.cursor()
|
||||||
|
results = cursor.execute("SELECT tags FROM music").fetchall()
|
||||||
|
tags = []
|
||||||
|
for result in results:
|
||||||
|
for tag in result[0].strip(",").split(","):
|
||||||
|
if tag and tag not in tags:
|
||||||
|
tags.append(tag)
|
||||||
|
conn.close()
|
||||||
|
return tags
|
||||||
|
|
||||||
def query_music(self, **kwargs):
|
def query_music(self, **kwargs):
|
||||||
condition = []
|
condition = []
|
||||||
filler = []
|
filler = []
|
||||||
@ -242,8 +254,11 @@ class MusicDatabase:
|
|||||||
music_dict = json.loads(result[3])
|
music_dict = json.loads(result[3])
|
||||||
music_dict['type'] = result[1]
|
music_dict['type'] = result[1]
|
||||||
music_dict['title'] = result[2]
|
music_dict['title'] = result[2]
|
||||||
music_dict['tags'] = result[4].strip(",").split(",")
|
|
||||||
music_dict['id'] = result[0]
|
music_dict['id'] = result[0]
|
||||||
|
music_dict['tags'] = result[4].strip(",").split(",")
|
||||||
|
if not music_dict['tags'][0]:
|
||||||
|
music_dict['tags'] = []
|
||||||
|
|
||||||
music_dicts.append(music_dict)
|
music_dicts.append(music_dict)
|
||||||
|
|
||||||
return music_dicts
|
return music_dicts
|
||||||
@ -260,7 +275,7 @@ class MusicDatabase:
|
|||||||
if len(results) > 0:
|
if len(results) > 0:
|
||||||
tags = results[0][0].strip(",").split(",")
|
tags = results[0][0].strip(",").split(",")
|
||||||
|
|
||||||
return tags
|
return tags if tags[0] else []
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
53
interface.py
53
interface.py
@ -10,7 +10,7 @@ import shutil
|
|||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
import errno
|
import errno
|
||||||
import media
|
import media
|
||||||
from media.playlist import get_item_wrapper, get_item_wrapper_by_id
|
from media.playlist import get_item_wrapper, get_item_wrapper_by_id, get_item_wrappers_by_tags
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@ -90,20 +90,49 @@ def requires_auth(f):
|
|||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
return decorated
|
return decorated
|
||||||
|
|
||||||
def build_tags_lookup():
|
def tag_color(tag):
|
||||||
lookup = {}
|
num = hash(tag) % 8
|
||||||
for path, id in var.library.file_id_lookup.items():
|
if num == 0:
|
||||||
lookup[path] = var.music_db.query_tags_by_id(id)
|
return "primary"
|
||||||
|
elif num == 1:
|
||||||
|
return "secondary"
|
||||||
|
elif num == 2:
|
||||||
|
return "success"
|
||||||
|
elif num == 3:
|
||||||
|
return "danger"
|
||||||
|
elif num == 4:
|
||||||
|
return "warning"
|
||||||
|
elif num == 5:
|
||||||
|
return "info"
|
||||||
|
elif num == 6:
|
||||||
|
return "light"
|
||||||
|
elif num == 7:
|
||||||
|
return "dark"
|
||||||
|
|
||||||
return lookup
|
def build_tags_color_lookup():
|
||||||
|
color_lookup = {}
|
||||||
|
for tag in var.music_db.query_all_tags():
|
||||||
|
color_lookup[tag] = tag_color(tag)
|
||||||
|
|
||||||
|
|
||||||
|
return color_lookup
|
||||||
|
|
||||||
|
def build_path_tags_lookup():
|
||||||
|
path_lookup = {}
|
||||||
|
for path, id in var.library.file_id_lookup.items():
|
||||||
|
path_lookup[path] = var.music_db.query_tags_by_id(id)
|
||||||
|
|
||||||
|
return path_lookup
|
||||||
|
|
||||||
@web.route("/", methods=['GET'])
|
@web.route("/", methods=['GET'])
|
||||||
@requires_auth
|
@requires_auth
|
||||||
def index():
|
def index():
|
||||||
tags_lookup = build_tags_lookup()
|
tags_color_lookup = build_tags_color_lookup()
|
||||||
|
path_tags_lookup = build_path_tags_lookup()
|
||||||
return render_template('index.html',
|
return render_template('index.html',
|
||||||
all_files=var.library.files,
|
all_files=var.library.files,
|
||||||
tags_lookup=tags_lookup,
|
tags_lookup=path_tags_lookup,
|
||||||
|
tags_color_lookup=tags_color_lookup,
|
||||||
music_library=var.library.dir,
|
music_library=var.library.dir,
|
||||||
os=os,
|
os=os,
|
||||||
playlist=var.playlist,
|
playlist=var.playlist,
|
||||||
@ -121,11 +150,13 @@ def playlist():
|
|||||||
)]
|
)]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tags_color_lookup = build_tags_color_lookup()
|
||||||
items = []
|
items = []
|
||||||
|
|
||||||
for index, item_wrapper in enumerate(var.playlist):
|
for index, item_wrapper in enumerate(var.playlist):
|
||||||
items.append(render_template('playlist.html',
|
items.append(render_template('playlist.html',
|
||||||
index=index,
|
index=index,
|
||||||
|
tags_color_lookup=tags_color_lookup,
|
||||||
m=item_wrapper.item(),
|
m=item_wrapper.item(),
|
||||||
playlist=var.playlist
|
playlist=var.playlist
|
||||||
)
|
)
|
||||||
@ -258,6 +289,12 @@ def post():
|
|||||||
shutil.rmtree(path)
|
shutil.rmtree(path)
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
elif 'add_tag' in request.form:
|
||||||
|
music_wrappers = get_item_wrappers_by_tags(var.bot, [request.form['add_tag']], user)
|
||||||
|
for music_wrapper in music_wrappers:
|
||||||
|
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
|
||||||
|
var.playlist.extend(music_wrappers)
|
||||||
|
|
||||||
elif 'action' in request.form:
|
elif 'action' in request.form:
|
||||||
action = request.form['action']
|
action = request.form['action']
|
||||||
if action == "randomize":
|
if action == "randomize":
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
.bs-docs-section{margin-top:4em}
|
.bs-docs-section{margin-top:4em}
|
||||||
|
.bs-docs-section .page-header h1 { padding: 2rem 0; font-size: 3rem; margin-bottom: 10px }
|
||||||
.btn-space{margin-right:5px}
|
.btn-space{margin-right:5px}
|
||||||
.playlist-title-td{width:60%}
|
.playlist-title-td{width:60%}
|
||||||
.playlist-title{float:left; }
|
.playlist-title{float:left; }
|
||||||
.playlist-artwork{float:left; margin-left:10px;}
|
.playlist-artwork{float:left; margin-left:10px;}
|
||||||
|
.tag-click{cursor:pointer;}
|
||||||
|
@ -79,7 +79,7 @@
|
|||||||
{{ filepath }}
|
{{ filepath }}
|
||||||
</div>
|
</div>
|
||||||
{% for tag in tags_lookup[filepath] %}
|
{% for tag in tags_lookup[filepath] %}
|
||||||
<span class="badge badge-warning">{{ tag }}</span>
|
<span class="badge badge-{{ tags_color_lookup[tag] }}">{{ tag }}</span>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
<div class="btn-group" style="float: right;">
|
<div class="btn-group" style="float: right;">
|
||||||
@ -119,13 +119,9 @@
|
|||||||
<div class="bs-docs-section">
|
<div class="bs-docs-section">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div id="playlist" class="card">
|
<div id="playlist" class="col-lg-12">
|
||||||
<div class="card-header">
|
<div>
|
||||||
<h2 class="card-title"><i class="fa fa-list" aria-hidden="true"></i> Play List</h2>
|
<div class="btn-group" style="margin-bottom: 10px;">
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="card-body">
|
|
||||||
<div class="btn-group" style="margin-bottom: 5px;">
|
|
||||||
<button type="button" id="play-btn" class="btn btn-info btn-space"
|
<button type="button" id="play-btn" class="btn btn-info btn-space"
|
||||||
onclick="request('post', {action : 'resume'})" disabled>
|
onclick="request('post', {action : 'resume'})" disabled>
|
||||||
<i class="fas fa-play" aria-hidden="true"></i>
|
<i class="fas fa-play" aria-hidden="true"></i>
|
||||||
@ -208,9 +204,30 @@
|
|||||||
<div class="bs-docs-section">
|
<div class="bs-docs-section">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
|
<div class="page-header">
|
||||||
|
<h1 id="forms">Music Library</h1>
|
||||||
|
</div>
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<h4 class="card-title">Tags</h4>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
{% for tag in tags_color_lookup.keys() %}
|
||||||
|
<span class="tag-click badge badge-{{ tags_color_lookup[tag] }}"
|
||||||
|
onclick="request('post', {add_tag : '{{ tag }}'})">
|
||||||
|
{{ tag }}</span>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="bs-docs-section">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
<div id="browser" class="card">
|
<div id="browser" class="card">
|
||||||
<div class="card-header">
|
<div class="card-header">
|
||||||
<h2 class="card-title"><i class="fa fa-list" aria-hidden="true"></i> Music Library</h2>
|
<h4 class="card-title">Files</h4>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
|
@ -38,7 +38,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
<br />
|
<br />
|
||||||
{% for tag in m.tags %}
|
{% for tag in m.tags %}
|
||||||
<span class="badge badge-warning">{{ tag }}</span>
|
<span class="badge badge-{{ tags_color_lookup[tag] }}">{{ tag }}</span>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user