From 64f738c5f0041846eb8b8101c5aed4dc29617a61 Mon Sep 17 00:00:00 2001 From: dave Date: Mon, 5 Oct 2020 20:11:58 -0700 Subject: [PATCH] fix search --- pysonic/api.py | 61 ++++++++++++++++++++++++++++++++++++--------- pysonic/database.py | 15 ++++++----- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/pysonic/api.py b/pysonic/api.py index ebac7cf..fd23e96 100644 --- a/pysonic/api.py +++ b/pysonic/api.py @@ -394,21 +394,58 @@ class PysonicSubsonicApi(object): # TODO make this more efficient albums = 0 - for item in self.library.get_artists(): - if query in item["name"].lower(): - response.add_child("album", _parent="searchResult2", **self.render_node(item, item["metadata"], {}, {})) - albums += 1 - if albums >= albumCount: - break + for album in self.library.get_albums(): + if query not in album["name"].lower(): + continue + + response.add_child("album", _parent="searchResult2", + id=album["dir"], + parent=album["artistdir"], + isDir="true", + title=album["name"], + album=album["name"], + artist=album["artistname"], + coverArt=album["coverid"] + #year=TODO + # playCount="0" + # created="2016-05-08T05:31:31.000Z"/>) + ) + albums += 1 + if albums >= albumCount: + break # TODO make this more efficient songs = 0 - for item in self.library.get_songs(limit=9999999, shuffle=False): - if query in item["name"].lower(): - response.add_child("song", _parent="searchResult2", **self.render_node(item, item["metadata"], {}, {})) - songs += 1 - if songs > songCount: - break + for song in self.library.db.get_songs(): + if query not in song["title"].lower(): + continue + + response.add_child("song", _parent="searchResult2", + id=song["id"], + parent=song["albumdir"], + isDir="false", + title=song["title"], + album=song["albumname"], + artist=song["artistname"], + track=song["track"], + year=song["year"], + genre=song["genrename"], + coverArt=song["albumcoverid"], + size=song["size"], + contentType=song["format"], + duration=song["length"], + bitRate=song["bitrate"], + path=song["file"], + playCount=420, + albumId=song["albumid"], + type="music" + # suffix="mp3" + # created="2012-09-17T22:35:19.000Z" + ) + + songs += 1 + if songs > songCount: + break return response diff --git a/pysonic/database.py b/pysonic/database.py index eb072d5..d387ee9 100644 --- a/pysonic/database.py +++ b/pysonic/database.py @@ -191,7 +191,7 @@ class PysonicDatabase(object): return libs @readcursor - def get_artists(self, cursor, id=None, dirid=None, sortby=None, order=None): + def get_artists(self, cursor, id=None, dirid=None, sortby="name", order=None): assert order in ["asc", "desc", None] artists = [] q = "SELECT * FROM artists" @@ -213,14 +213,14 @@ class PysonicDatabase(object): return artists @readcursor - def get_albums(self, cursor, id=None, artist=None, sortby=None, order=None, limit=None): + def get_albums(self, cursor, id=None, artist=None, sortby="name", order=None, limit=None): """ :param limit: int or tuple of int, int. translates directly to sql logic. """ if order: order = {"asc": "ASC", "desc": "DESC"}[order] - if sortby and sortby == "random": + if sortby == "random": sortby = "RANDOM()" albums = [] @@ -263,13 +263,13 @@ class PysonicDatabase(object): return albums @readcursor - def get_songs(self, cursor, id=None, genre=None, sortby=None, order=None, limit=None): + def get_songs(self, cursor, id=None, genre=None, sortby="title", order=None, limit=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: order = {"asc": "ASC", "desc": "DESC"}[order] - if sortby and sortby == "random": + if sortby == "random": sortby = "RANDOM()" songs = [] @@ -280,10 +280,13 @@ class PysonicDatabase(object): alb.name as albumname, alb.coverid as albumcoverid, art.name as artistname, - g.name as genrename + g.name as genrename, + albdir.id as albumdir FROM songs as s INNER JOIN albums as alb on s.albumid == alb.id + INNER JOIN dirs as albdir + on albdir.id = alb.dir INNER JOIN artists as art on alb.artistid = art.id LEFT JOIN genres as g