1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00

Implemented search

This commit is contained in:
spl0k 2013-06-24 18:43:50 +02:00
parent 08b9b1b293
commit 1bd353a8ee
3 changed files with 106 additions and 1 deletions

View File

@ -138,7 +138,7 @@ def now_playing():
def get_starred(): def get_starred():
return request.formatter({ return request.formatter({
'starred': { 'starred': {
'artist': [ { 'id': sf.starred.name, 'name': sf.starred_id } for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(~ Folder.tracks.any()) ], 'artist': [ { 'id': str(sf.starred_id), 'name': sf.starred.name } for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(~ Folder.tracks.any()) ],
'album': [ sf.starred.as_subsonic_child(request.user) for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(Folder.tracks.any()) ], 'album': [ sf.starred.as_subsonic_child(request.user) for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(Folder.tracks.any()) ],
'song': [ st.starred.as_subsonic_child(request.user) for st in StarredTrack.query.join(User).filter(User.name == request.username) ] 'song': [ st.starred.as_subsonic_child(request.user) for st in StarredTrack.query.join(User).filter(User.name == request.username) ]
} }

104
api/search.py Executable file
View File

@ -0,0 +1,104 @@
# coding: utf-8
from flask import request
from web import app
from db import Folder, Track, Artist, Album
@app.route('/rest/search.view')
def old_search():
artist, album, title, anyf, count, offset, newer_than = map(request.args.get, [ 'artist', 'album', 'title', 'any', 'count', 'offset', 'newerThan' ])
try:
count = int(count) if count else 20
offset = int(offset) if offset else 0
newer_than = int(newer_than) if newer_than else 0
except:
return request.error_formatter(0, 'Invalid parameter')
if artist:
query = Folder.query.filter(~ Folder.tracks.any(), Folder.name.contains(artist))
elif album:
query = Folder.query.filter(Folder.tracks.any(), Folder.name.contains(album))
elif title:
query = Track.query.filter(Track.title.contains(title))
elif anyf:
folders = Folder.query.filter(Folder.name.contains(anyf))
tracks = Track.query.filter(Track.title.contains(anyf))
res = folders.slice(offset, offset + count).all()
if offset + count > folders.count():
toff = max(0, offset - folders.count())
tend = offset + count - folders.count()
res += tracks.slice(toff, tend).all()
return request.formatter({ 'searchResult': {
'totalHits': folders.count() + tracks.count(),
'offset': offset,
'match': [ r.as_subsonic_child(request.user) for r in res ]
}})
else:
return request.error_formatter(10, 'Missing search parameter')
print "---"
print query
return request.formatter({ 'searchResult': {
'totalHits': query.count(),
'offset': offset,
'match': [ r.as_subsonic_child(request.user) for r in query.slice(offset, offset + count) ]
}})
@app.route('/rest/search2.view')
def new_search():
query, artist_count, artist_offset, album_count, album_offset, song_count, song_offset = map(
request.args.get, [ 'query', 'artistCount', 'artistOffset', 'albumCount', 'albumOffset', 'songCount', 'songOffset' ])
try:
artist_count = int(artist_count) if artist_count else 20
artist_offset = int(artist_offset) if artist_offset else 0
album_count = int(album_count) if album_count else 20
album_offset = int(album_offset) if album_offset else 0
song_count = int(song_count) if song_count else 20
song_offset = int(song_offset) if song_offset else 0
except:
return request.error_formatter(0, 'Invalid parameter')
if not query:
return request.error_formatter(10, 'Missing query parameter')
artist_query = Folder.query.filter(~ Folder.tracks.any(), Folder.name.contains(query)).slice(artist_offset, artist_offset + artist_count)
album_query = Folder.query.filter(Folder.tracks.any(), Folder.name.contains(query)).slice(album_offset, album_offset + album_count)
song_query = Track.query.filter(Track.title.contains(query)).slice(song_offset, song_offset + song_count)
return request.formatter({ 'searchResult2': {
'artist': [ { 'id': str(a.id), 'name': a.name } for a in artist_query ],
'album': [ f.as_subsonic_child(request.user) for f in album_query ],
'song': [ t.as_subsonic_child(request.user) for t in song_query ]
}})
@app.route('/rest/search3.view')
def search_id3():
query, artist_count, artist_offset, album_count, album_offset, song_count, song_offset = map(
request.args.get, [ 'query', 'artistCount', 'artistOffset', 'albumCount', 'albumOffset', 'songCount', 'songOffset' ])
try:
artist_count = int(artist_count) if artist_count else 20
artist_offset = int(artist_offset) if artist_offset else 0
album_count = int(album_count) if album_count else 20
album_offset = int(album_offset) if album_offset else 0
song_count = int(song_count) if song_count else 20
song_offset = int(song_offset) if song_offset else 0
except:
return request.error_formatter(0, 'Invalid parameter')
if not query:
return request.error_formatter(10, 'Missing query parameter')
artist_query = Artist.query.filter(Artist.name.contains(query)).slice(artist_offset, artist_offset + artist_count)
album_query = Album.query.filter(Album.name.contains(query)).slice(album_offset, album_offset + album_count)
song_query = Track.query.filter(Track.title.contains(query)).slice(song_offset, song_offset + song_count)
return request.formatter({ 'searchResult2': {
'artist': [ a.as_subsonic_artist(request.user) for a in artist_query ],
'album': [ a.as_subsonic_album(request.user) for a in album_query ],
'song': [ t.as_subsonic_child(request.user) for t in song_query ]
}})

1
web.py
View File

@ -61,4 +61,5 @@ import api.albums_songs
import api.media import api.media
import api.annotation import api.annotation
import api.chat import api.chat
import api.search