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

179 lines
5.3 KiB
Python
Raw Normal View History

2012-10-13 09:29:48 +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/>.
2012-10-13 09:29:48 +00:00
from flask import request
2014-03-24 20:53:19 +00:00
from web import app, store
2012-10-21 14:18:35 +00:00
from db import Folder, Artist, Album, Track
2014-03-04 21:56:53 +00:00
from . import get_entity
import uuid, time, string
2012-10-13 09:29:48 +00:00
2012-11-22 13:51:43 +00:00
@app.route('/rest/getMusicFolders.view', methods = [ 'GET', 'POST' ])
2012-10-13 09:29:48 +00:00
def list_folders():
return request.formatter({
'musicFolders': {
'musicFolder': [ {
'id': str(f.id),
'name': f.name
2014-03-24 20:53:19 +00:00
} for f in store.find(Folder, Folder.root == True).order_by(Folder.name) ]
2012-10-13 09:29:48 +00:00
}
})
2012-11-22 13:51:43 +00:00
@app.route('/rest/getIndexes.view', methods = [ 'GET', 'POST' ])
def list_indexes():
musicFolderId = request.args.get('musicFolderId')
ifModifiedSince = request.args.get('ifModifiedSince')
if ifModifiedSince:
try:
ifModifiedSince = int(ifModifiedSince) / 1000
except:
return request.error_formatter(0, 'Invalid timestamp')
if musicFolderId is None:
2014-03-24 20:53:19 +00:00
folder = store.find(Folder, Folder.root == True)
else:
try:
mfid = uuid.UUID(musicFolderId)
except:
return request.error_formatter(0, 'Invalid id')
2014-03-24 20:53:19 +00:00
folder = store.get(Folder, mfid)
2014-03-24 20:53:19 +00:00
if not folder or (type(folder) is Folder and not folder.root):
return request.error_formatter(70, 'Folder not found')
2014-03-24 20:53:19 +00:00
last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is not Folder else folder.last_scan
if (not ifModifiedSince is None) and last_modif < ifModifiedSince:
return request.formatter({ 'indexes': { 'lastModified': last_modif * 1000 } })
# The XSD lies, we don't return artists but a directory structure
2014-03-24 20:53:19 +00:00
if type(folder) is not Folder:
artists = []
childs = []
for f in folder:
artists += f.children
childs += f.tracks
else:
artists = folder.children
childs = folder.tracks
indexes = {}
for artist in artists:
index = artist.name[0].upper()
if index in map(str, xrange(10)):
index = '#'
elif index not in string.letters:
index = '?'
if index not in indexes:
indexes[index] = []
indexes[index].append(artist)
return request.formatter({
'indexes': {
'lastModified': last_modif * 1000,
'index': [ {
'name': k,
'artist': [ {
'id': str(a.id),
'name': a.name
} for a in sorted(v, key = lambda a: a.name.lower()) ]
} for k, v in sorted(indexes.iteritems()) ],
2013-06-13 16:44:56 +00:00
'child': [ c.as_subsonic_child(request.user) for c in sorted(childs, key = lambda t: t.sort_key()) ]
}
})
2012-11-22 13:51:43 +00:00
@app.route('/rest/getMusicDirectory.view', methods = [ 'GET', 'POST' ])
2012-11-10 23:01:52 +00:00
def show_directory():
2012-12-02 15:42:25 +00:00
status, res = get_entity(request, Folder)
if not status:
return res
2012-11-10 23:01:52 +00:00
directory = {
2012-12-02 15:42:25 +00:00
'id': str(res.id),
'name': res.name,
2013-06-13 16:44:56 +00:00
'child': [ f.as_subsonic_child(request.user) for f in sorted(res.children, key = lambda c: c.name.lower()) ] + [ t.as_subsonic_child(request.user) for t in sorted(res.tracks, key = lambda t: t.sort_key()) ]
2012-11-10 23:01:52 +00:00
}
2012-12-02 15:42:25 +00:00
if not res.root:
directory['parent'] = str(res.parent_id)
2012-11-10 23:01:52 +00:00
return request.formatter({ 'directory': directory })
@app.route('/rest/getArtists.view', methods = [ 'GET', 'POST' ])
def list_artists():
2012-12-02 15:42:25 +00:00
# According to the API page, there are no parameters?
indexes = {}
2014-03-24 20:53:19 +00:00
for artist in store.find(Artist):
2013-10-26 11:13:19 +00:00
index = artist.name[0].upper() if artist.name else '?'
if index in map(str, xrange(10)):
index = '#'
elif index not in string.letters:
index = '?'
if index not in indexes:
indexes[index] = []
indexes[index].append(artist)
return request.formatter({
'artists': {
'index': [ {
'name': k,
2013-06-13 16:44:56 +00:00
'artist': [ a.as_subsonic_artist(request.user) for a in sorted(v, key = lambda a: a.name.lower()) ]
} for k, v in sorted(indexes.iteritems()) ]
}
})
@app.route('/rest/getArtist.view', methods = [ 'GET', 'POST' ])
def artist_info():
2012-12-02 15:42:25 +00:00
status, res = get_entity(request, Artist)
if not status:
return res
2013-06-13 16:44:56 +00:00
info = res.as_subsonic_artist(request.user)
info['album'] = [ a.as_subsonic_album(request.user) for a in sorted(res.albums, key = lambda a: a.sort_key()) ]
return request.formatter({ 'artist': info })
@app.route('/rest/getAlbum.view', methods = [ 'GET', 'POST' ])
def album_info():
2012-12-02 15:42:25 +00:00
status, res = get_entity(request, Album)
if not status:
return res
2013-06-13 16:44:56 +00:00
info = res.as_subsonic_album(request.user)
info['song'] = [ t.as_subsonic_child(request.user) for t in sorted(res.tracks, key = lambda t: t.sort_key()) ]
2012-12-02 15:42:25 +00:00
return request.formatter({ 'album': info })
2012-12-01 19:12:35 +00:00
@app.route('/rest/getSong.view', methods = [ 'GET', 'POST' ])
def track_info():
2012-12-02 15:42:25 +00:00
status, res = get_entity(request, Track)
if not status:
return res
2012-12-01 19:12:35 +00:00
2013-06-13 16:44:56 +00:00
return request.formatter({ 'song': res.as_subsonic_child(request.user) })
@app.route('/rest/getVideos.view', methods = [ 'GET', 'POST' ])
def list_videos():
return request.error_formatter(0, 'Video streaming not supported')