1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-13 21:52:18 +00:00
supysonic/api/search.py

123 lines
5.5 KiB
Python
Raw Normal View History

2013-06-24 16:43:50 +00:00
# coding: utf-8
2014-03-02 17:31:32 +00:00
# This file is part of Supysonic.
#
# Supysonic is a Python implementation of the Subsonic server API.
# Copyright (C) 2013 Alban 'spl0k' Féron
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2013-06-24 16:43:50 +00:00
from flask import request
2014-04-05 20:23:23 +00:00
from storm.info import ClassAlias
from web import app, store
2013-06-24 16:43:50 +00:00
from db import Folder, Track, Artist, Album
2013-06-25 20:07:49 +00:00
@app.route('/rest/search.view', methods = [ 'GET', 'POST' ])
2013-06-24 16:43:50 +00:00
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:
2014-04-05 20:23:23 +00:00
parent = ClassAlias(Folder)
query = store.find(parent, Folder.parent_id == parent.id, Track.folder_id == Folder.id, parent.name.contains_string(artist)).config(distinct = True)
2013-06-24 16:43:50 +00:00
elif album:
2014-04-05 20:23:23 +00:00
query = store.find(Folder, Track.folder_id == Folder.id, Folder.name.contains_string(album)).config(distinct = True)
2013-06-24 16:43:50 +00:00
elif title:
2014-04-05 20:23:23 +00:00
query = store.find(Track, Track.title.contains_string(title))
2013-06-24 16:43:50 +00:00
elif anyf:
2014-04-05 20:23:23 +00:00
folders = store.find(Folder, Folder.name.contains_string(anyf))
tracks = store.find(Track, Track.title.contains_string(anyf))
res = list(folders[offset : offset + count])
2013-06-24 16:43:50 +00:00
if offset + count > folders.count():
toff = max(0, offset - folders.count())
tend = offset + count - folders.count()
2014-04-05 20:23:23 +00:00
res += list(tracks[toff : tend])
2013-06-24 16:43:50 +00:00
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')
return request.formatter({ 'searchResult': {
'totalHits': query.count(),
'offset': offset,
2014-04-05 20:23:23 +00:00
'match': [ r.as_subsonic_child(request.user) for r in query[offset : offset + count] ]
2013-06-24 16:43:50 +00:00
}})
2013-06-25 20:07:49 +00:00
@app.route('/rest/search2.view', methods = [ 'GET', 'POST' ])
2013-06-24 16:43:50 +00:00
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:
2014-04-05 20:23:23 +00:00
artist_count = int(artist_count) if artist_count else 20
2013-06-24 16:43:50 +00:00
artist_offset = int(artist_offset) if artist_offset else 0
2014-04-05 20:23:23 +00:00
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
2013-06-24 16:43:50 +00:00
except:
return request.error_formatter(0, 'Invalid parameter')
if not query:
return request.error_formatter(10, 'Missing query parameter')
2014-04-05 20:23:23 +00:00
parent = ClassAlias(Folder)
artist_query = store.find(parent, Folder.parent_id == parent.id, Track.folder_id == Folder.id, parent.name.contains_string(query)).config(distinct = True, offset = artist_offset, limit = artist_count)
album_query = store.find(Folder, Track.folder_id == Folder.id, Folder.name.contains_string(query)).config(distinct = True, offset = album_offset, limit = album_count)
song_query = store.find(Track, Track.title.contains_string(query))[song_offset : song_offset + song_count]
2013-06-24 16:43:50 +00:00
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 ]
}})
2013-06-25 20:07:49 +00:00
@app.route('/rest/search3.view', methods = [ 'GET', 'POST' ])
2013-06-24 16:43:50 +00:00
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:
2014-04-05 20:23:23 +00:00
artist_count = int(artist_count) if artist_count else 20
2013-06-24 16:43:50 +00:00
artist_offset = int(artist_offset) if artist_offset else 0
2014-04-05 20:23:23 +00:00
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
2013-06-24 16:43:50 +00:00
except:
return request.error_formatter(0, 'Invalid parameter')
if not query:
return request.error_formatter(10, 'Missing query parameter')
2014-04-05 20:23:23 +00:00
artist_query = store.find(Artist, Artist.name.contains_string(query))[artist_offset : artist_offset + artist_count]
album_query = store.find(Album, Album.name.contains_string(query))[album_offset : album_offset + album_count]
song_query = store.find(Track, Track.title.contains_string(query))[song_offset : song_offset + song_count]
2013-06-24 16:43:50 +00:00
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 ]
}})