fix: optimize database logic, fixed #93
This commit is contained in:
parent
d653eceb2a
commit
a39199d54d
20
database.py
20
database.py
@ -257,19 +257,23 @@ class MusicDatabase:
|
|||||||
|
|
||||||
return self._result_to_dict(results)
|
return self._result_to_dict(results)
|
||||||
|
|
||||||
def query_tags_by_id(self, id):
|
def query_tags_by_ids(self, ids):
|
||||||
|
condition_str = " OR ".join(['id=?'] * len(ids))
|
||||||
|
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
results = cursor.execute("SELECT tags FROM music "
|
results = cursor.execute("SELECT id, tags FROM music "
|
||||||
"WHERE id=?", (id, )).fetchall()
|
"WHERE %s" % condition_str, ids).fetchall()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
lookup = {}
|
||||||
if len(results) > 0:
|
if len(results) > 0:
|
||||||
tags = results[0][0].strip(",").split(",")
|
for result in results:
|
||||||
|
id = result[0]
|
||||||
|
tags = result[1].strip(",").split(",")
|
||||||
|
lookup[id] = tags if tags[0] else []
|
||||||
|
|
||||||
return tags if tags[0] else []
|
return lookup
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def query_random_music(self, count):
|
def query_random_music(self, count):
|
||||||
conn = sqlite3.connect(self.db_path)
|
conn = sqlite3.connect(self.db_path)
|
||||||
@ -297,7 +301,7 @@ class MusicDatabase:
|
|||||||
|
|
||||||
return music_dicts
|
return music_dicts
|
||||||
else:
|
else:
|
||||||
return None
|
return []
|
||||||
|
|
||||||
def delete_music(self, **kwargs):
|
def delete_music(self, **kwargs):
|
||||||
condition = []
|
condition = []
|
||||||
|
11
interface.py
11
interface.py
@ -118,12 +118,13 @@ def build_tags_color_lookup():
|
|||||||
return color_lookup
|
return color_lookup
|
||||||
|
|
||||||
def build_path_tags_lookup():
|
def build_path_tags_lookup():
|
||||||
path_lookup = {}
|
id_tags_lookup = var.music_db.query_tags_by_ids(list(var.cache.file_id_lookup.values()))
|
||||||
items = var.cache.file_id_lookup.items()
|
|
||||||
for path, id in items:
|
|
||||||
path_lookup[path] = var.music_db.query_tags_by_id(id)
|
|
||||||
|
|
||||||
return path_lookup
|
path_tags_lookup = {}
|
||||||
|
for path, id in var.cache.file_id_lookup.items():
|
||||||
|
path_tags_lookup[path] = id_tags_lookup[id]
|
||||||
|
|
||||||
|
return path_tags_lookup
|
||||||
|
|
||||||
def recur_dir(dirobj):
|
def recur_dir(dirobj):
|
||||||
for name, dir in dirobj.get_subdirs().items():
|
for name, dir in dirobj.get_subdirs().items():
|
||||||
|
@ -456,8 +456,9 @@ class AutoPlaylist(OneshotPlaylist):
|
|||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
dicts = var.music_db.query_random_music(var.config.getint("bot", "autoplay_length", fallback=5))
|
dicts = var.music_db.query_random_music(var.config.getint("bot", "autoplay_length", fallback=5))
|
||||||
_list = [get_item_wrapper_from_dict(var.bot, _dict, "AutoPlay") for _dict in dicts]
|
if dicts:
|
||||||
self.from_list(_list, -1)
|
_list = [get_item_wrapper_from_dict(var.bot, _dict, "AutoPlay") for _dict in dicts]
|
||||||
|
self.from_list(_list, -1)
|
||||||
|
|
||||||
# def from_list(self, _list, current_index):
|
# def from_list(self, _list, current_index):
|
||||||
# self.version += 1
|
# self.version += 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user