Browse Source

faster search

master
dave 2 years ago
parent
commit
f3d888be35
  1. 21
      pysonic/api.py
  2. 15
      pysonic/database.py

21
pysonic/api.py

@ -418,19 +418,15 @@ class PysonicSubsonicApi(object):
query = query.replace("*", "") # TODO handle this
artists = 0
for item in self.library.get_artists():
if query in item["name"].lower():
response.add_child("artist", _parent="searchResult2", id=item["id"], name=item["name"])
artists += 1
if artists >= artistCount:
break
for item in self.library.get_artists(name_contains=query):
response.add_child("artist", _parent="searchResult2", id=item["id"], name=item["name"])
artists += 1
if artists >= artistCount:
break
# TODO make this more efficient
albums = 0
for album in self.library.get_albums():
if query not in album["name"].lower():
continue
for album in self.library.get_albums(name_contains=query):
response.add_child("album", _parent="searchResult2",
id=album["dir"],
parent=album["artistdir"],
@ -449,10 +445,7 @@ class PysonicSubsonicApi(object):
# TODO make this more efficient
songs = 0
for song in self.library.db.get_songs(): #TODO Meeee too
if query not in song["title"].lower():
continue
for song in self.library.db.get_songs(title_contains=query):
response.add_child("song", _parent="searchResult2",
id=song["id"],
parent=song["albumdir"],

15
pysonic/database.py

@ -192,7 +192,7 @@ class PysonicDatabase(object):
return libs
@readcursor
def get_artists(self, cursor, id=None, dirid=None, sortby="name", order=None):
def get_artists(self, cursor, id=None, dirid=None, sortby="name", order=None, name_contains=None):
assert order in ["asc", "desc", None]
artists = []
q = "SELECT * FROM artists"
@ -204,6 +204,9 @@ class PysonicDatabase(object):
if dirid:
conditions.append("dir = ?")
params.append(dirid)
if name_contains:
conditions.append("name LIKE ?")
params.append("%{}%".format(name_contains))
if conditions:
q += " WHERE " + " AND ".join(conditions)
if sortby:
@ -214,7 +217,7 @@ class PysonicDatabase(object):
return artists
@readcursor
def get_albums(self, cursor, id=None, artist=None, sortby="name", order=None, limit=None):
def get_albums(self, cursor, id=None, artist=None, sortby="name", order=None, limit=None, name_contains=None):
"""
:param limit: int or tuple of int, int. translates directly to sql logic.
"""
@ -246,6 +249,9 @@ class PysonicDatabase(object):
if artist:
conditions.append("artistid = ?")
params.append(artist)
if name_contains:
conditions.append("alb.name LIKE ?")
params.append("%{}%".format(name_contains))
if conditions:
q += " WHERE " + " AND ".join(conditions)
@ -264,7 +270,7 @@ class PysonicDatabase(object):
return albums
@readcursor
def get_songs(self, cursor, id=None, genre=None, sortby="title", order=None, limit=None):
def get_songs(self, cursor, id=None, genre=None, sortby="title", order=None, limit=None, title_contains=None):
# TODO make this query massively uglier by joining albums and artists so that artistid etc can be a filter
# or maybe lookup those IDs in the library layer?
if order:
@ -306,6 +312,9 @@ class PysonicDatabase(object):
if genre:
conditions.append("g.name = ?")
params.append(genre)
if title_contains:
conditions.append("s.title LIKE ?")
params.append("%{}%".format(title_contains))
if conditions:
q += " WHERE " + " AND ".join(conditions)

Loading…
Cancel
Save