1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

Porting supysonic.api.search

This commit is contained in:
Alban Féron 2022-12-30 16:41:00 +01:00
parent 8b93e0bc6e
commit dd2ef2ffeb
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
2 changed files with 89 additions and 70 deletions

View File

@ -28,22 +28,32 @@ def old_search():
min_date = datetime.fromtimestamp(newer_than) min_date = datetime.fromtimestamp(newer_than)
if artist: if artist:
query = select( Child = Folder.alias()
t.folder.parent query = (
for t in Track Folder.select()
if artist in t.folder.parent.name and t.folder.parent.created > min_date .join(Child, on=Child.parent == Folder.id)
.join(Track, on=Track.folder == Child.id)
.where(Folder.name.contains(artist), Folder.created > min_date)
.distinct()
) )
elif album: elif album:
query = select( query = (
t.folder Folder.select()
for t in Track .join(Track, on=Track.folder)
if album in t.folder.name and t.folder.created > min_date .where(Folder.name.contains(album), Folder.created > min_date)
.distinct()
) )
elif title: elif title:
query = Track.select(lambda t: title in t.title and t.created > min_date) query = Track.select().where(
Track.title.contains(title), Track.created > min_date
)
elif anyf: elif anyf:
folders = Folder.select(lambda f: anyf in f.name and f.created > min_date) folders = Folder.select().where(
tracks = Track.select(lambda t: anyf in t.title and t.created > min_date) Folder.name.contains(anyf), Folder.created > min_date
)
tracks = Track.select().where(
Track.title.contains(anyf), Track.created > min_date
)
res = folders[offset : offset + count] res = folders[offset : offset + count]
fcount = folders.count() fcount = folders.count()
if offset + count > fcount: if offset + count > fcount:
@ -114,18 +124,30 @@ def new_search():
song_offset = int(song_offset) if song_offset else 0 song_offset = int(song_offset) if song_offset else 0
root = get_root_folder(mfid) root = get_root_folder(mfid)
artists = select(t.folder.parent for t in Track if query in t.folder.parent.name) Child = Folder.alias()
albums = select(t.folder for t in Track if query in t.folder.name) artists = (
songs = Track.select(lambda t: query in t.title) Folder.select()
.join(Child, on=Child.parent == Folder.id)
.join(Track, on=Track.folder == Child.id)
.where(Folder.name.contains(query))
.distinct()
)
albums = (
Folder.select()
.join(Track, on=Track.folder)
.where(Folder.name.contains(query))
.distinct()
)
songs = Track.select().where(Track.title.contains(query))
if root is not None: if root is not None:
artists = artists.where(lambda t: t.root_folder == root) artists = artists.where(Track.root_folder == root)
albums = albums.where(lambda t: t.root_folder == root) albums = albums.where(Track.root_folder == root)
songs = songs.where(lambda t: t.root_folder == root) songs = songs.where(Track.root_folder == root)
artists = artists.limit(artist_count, artist_offset) artists = artists.limit(artist_count).offset(artist_offset)
albums = albums.limit(album_count, album_offset) albums = albums.limit(album_count).offset(album_offset)
songs = songs.limit(song_count, song_offset) songs = songs.limit(song_count).offset(song_offset)
return request.formatter( return request.formatter(
"searchResult2", "searchResult2",
@ -174,18 +196,18 @@ def search_id3():
song_offset = int(song_offset) if song_offset else 0 song_offset = int(song_offset) if song_offset else 0
root = get_root_folder(mfid) root = get_root_folder(mfid)
artists = Artist.select(lambda a: query in a.name) artists = Artist.select().where(Artist.name.contains(query))
albums = Album.select(lambda a: query in a.name) albums = Album.select().where(Album.name.contains(query))
songs = Track.select(lambda t: query in t.title) songs = Track.select().where(Track.title.contains(query))
if root is not None: if root is not None:
artists = artists.where(lambda a: root in a.tracks.root_folder) artists = artists.join(Track).where(Track.root_folder == root)
albums = albums.where(lambda a: root in a.tracks.root_folder) albums = albums.join(Track).where(Track.root_folder == root)
songs = songs.where(lambda t: t.root_folder == root) songs = songs.where(Track.root_folder == root)
artists = artists.limit(artist_count, artist_offset) artists = artists.limit(artist_count).offset(artist_offset)
albums = albums.limit(album_count, album_offset) albums = albums.limit(album_count).offset(album_offset)
songs = songs.limit(song_count, song_offset) songs = songs.limit(song_count).offset(song_offset)
return request.formatter( return request.formatter(
"searchResult3", "searchResult3",

View File

@ -8,8 +8,6 @@
import time import time
import unittest import unittest
from pony.orm import db_session, commit
from supysonic.db import Folder, Artist, Album, Track from supysonic.db import Folder, Artist, Album, Track
from .apitestbase import ApiTestBase from .apitestbase import ApiTestBase
@ -19,50 +17,49 @@ class SearchTestCase(ApiTestBase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
with db_session: root = Folder.create(root=True, name="Root folder", path="tests/assets")
root = Folder(root=True, name="Root folder", path="tests/assets") Folder.create(root=True, name="Empty", path="/tmp")
Folder(root=True, name="Empty", path="/tmp")
for letter in "ABC": for letter in "ABC":
folder = Folder( folder = Folder.create(
name=letter + "rtist", name=letter + "rtist",
path="tests/assets/{}rtist".format(letter), path="tests/assets/{}rtist".format(letter),
parent=root, root=False,
parent=root,
)
artist = Artist.create(name=letter + "rtist")
for lether in "AB":
afolder = Folder.create(
name=letter + lether + "lbum",
path="tests/assets/{0}rtist/{0}{1}lbum".format(letter, lether),
root=False,
parent=folder,
) )
artist = Artist(name=letter + "rtist")
for lether in "AB": album = Album.create(name=letter + lether + "lbum", artist=artist)
afolder = Folder(
name=letter + lether + "lbum", for num, song in enumerate(["One", "Two", "Three"]):
path="tests/assets/{0}rtist/{0}{1}lbum".format(letter, lether), Track.create(
parent=folder, disc=1,
number=num,
title=song,
duration=2,
album=album,
artist=artist,
bitrate=320,
path="tests/assets/{0}rtist/{0}{1}lbum/{2}".format(
letter, lether, song
),
last_modification=0,
root_folder=root,
folder=afolder,
) )
album = Album(name=letter + lether + "lbum", artist=artist) self.assertEqual(Folder.select().count(), 11)
self.assertEqual(Artist.select().count(), 3)
for num, song in enumerate(["One", "Two", "Three"]): self.assertEqual(Album.select().count(), 6)
Track( self.assertEqual(Track.select().count(), 18)
disc=1,
number=num,
title=song,
duration=2,
album=album,
artist=artist,
bitrate=320,
path="tests/assets/{0}rtist/{0}{1}lbum/{2}".format(
letter, lether, song
),
last_modification=0,
root_folder=root,
folder=afolder,
)
commit()
self.assertEqual(Folder.select().count(), 11)
self.assertEqual(Artist.select().count(), 3)
self.assertEqual(Album.select().count(), 6)
self.assertEqual(Track.select().count(), 18)
def __track_as_pseudo_unique_str(self, elem): def __track_as_pseudo_unique_str(self, elem):
return elem.get("artist") + elem.get("album") + elem.get("title") return elem.get("artist") + elem.get("album") + elem.get("title")