2012-10-13 09:29:48 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from flask import request
|
|
|
|
from web import app
|
2012-10-21 14:18:35 +00:00
|
|
|
from db import Folder, Artist, Album, Track
|
2012-10-14 16:06:48 +00:00
|
|
|
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
|
2012-10-21 14:18:35 +00:00
|
|
|
} for f in Folder.query.filter(Folder.root == True).order_by(Folder.name).all() ]
|
2012-10-13 09:29:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2012-11-22 13:51:43 +00:00
|
|
|
@app.route('/rest/getIndexes.view', methods = [ 'GET', 'POST' ])
|
2012-10-14 16:06:48 +00:00
|
|
|
def list_indexes():
|
|
|
|
musicFolderId = request.args.get('musicFolderId')
|
|
|
|
ifModifiedSince = request.args.get('ifModifiedSince')
|
|
|
|
if ifModifiedSince:
|
|
|
|
try:
|
|
|
|
ifModifiedSince = int(ifModifiedSince)
|
|
|
|
except:
|
2012-10-20 18:23:38 +00:00
|
|
|
return request.error_formatter(0, 'Invalid timestamp')
|
2012-10-14 16:06:48 +00:00
|
|
|
|
|
|
|
if musicFolderId is None:
|
2012-10-21 14:18:35 +00:00
|
|
|
folder = Folder.query.filter(Folder.root == True).all()
|
2012-10-14 16:06:48 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
mfid = uuid.UUID(musicFolderId)
|
|
|
|
except:
|
2012-10-20 18:23:38 +00:00
|
|
|
return request.error_formatter(0, 'Invalid id')
|
2012-10-14 16:06:48 +00:00
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
folder = Folder.query.get(mfid)
|
2012-10-14 16:06:48 +00:00
|
|
|
|
2012-11-11 20:39:26 +00:00
|
|
|
if not folder or (type(folder) is not list and not folder.root):
|
2012-10-20 18:23:38 +00:00
|
|
|
return request.error_formatter(70, 'Folder not found')
|
2012-10-14 16:06:48 +00:00
|
|
|
|
|
|
|
last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan
|
|
|
|
last_modif_ts = int(time.mktime(last_modif.timetuple()))
|
|
|
|
|
|
|
|
if (not ifModifiedSince is None) and last_modif_ts < ifModifiedSince:
|
|
|
|
return request.formatter({ 'indexes': { 'lastModified': last_modif_ts } })
|
|
|
|
|
2012-11-10 22:02:30 +00:00
|
|
|
# The XSD lies, we don't return artists but a directory structure
|
2012-10-14 16:06:48 +00:00
|
|
|
if type(folder) is list:
|
2012-11-10 22:02:30 +00:00
|
|
|
artists = []
|
|
|
|
childs = []
|
|
|
|
for f in folder:
|
|
|
|
artists += f.children
|
|
|
|
childs += f.tracks
|
2012-10-14 16:06:48 +00:00
|
|
|
else:
|
2012-11-10 22:02:30 +00:00
|
|
|
artists = folder.children
|
|
|
|
childs = folder.tracks
|
2012-10-14 16:06:48 +00:00
|
|
|
|
|
|
|
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_ts,
|
|
|
|
'index': [ {
|
|
|
|
'name': k,
|
|
|
|
'artist': [ {
|
|
|
|
'id': str(a.id),
|
|
|
|
'name': a.name
|
|
|
|
} for a in sorted(v, key = lambda a: a.name.lower()) ]
|
2012-11-10 22:02:30 +00:00
|
|
|
} for k, v in sorted(indexes.iteritems()) ],
|
2012-11-10 23:01:52 +00:00
|
|
|
'child': [ c.as_subsonic_child() for c in sorted(childs, key = lambda t: t.sort_key()) ]
|
2012-10-14 16:06:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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():
|
|
|
|
did = request.args.get('id')
|
|
|
|
if not did:
|
|
|
|
return request.error_formatter(10, 'Missing directory id')
|
|
|
|
try:
|
|
|
|
fid = uuid.UUID(did)
|
|
|
|
except:
|
|
|
|
return request.error_formatter(0, 'Invalid directory id')
|
|
|
|
|
|
|
|
folder = Folder.query.get(fid)
|
|
|
|
if not folder:
|
|
|
|
return request.error_formatter(70, 'Directory not found')
|
|
|
|
|
|
|
|
directory = {
|
|
|
|
'id': str(folder.id),
|
|
|
|
'name': folder.name,
|
|
|
|
'child': [ f.as_subsonic_child() for f in sorted(folder.children, key = lambda c: c.name) ] + [ t.as_subsonic_child() for t in sorted(folder.tracks, key = lambda t: t.sort_key()) ]
|
|
|
|
}
|
|
|
|
if not folder.root:
|
|
|
|
directory['parent'] = str(folder.parent_id)
|
|
|
|
|
|
|
|
return request.formatter({ 'directory': directory })
|
|
|
|
|
2012-12-01 21:58:17 +00:00
|
|
|
@app.route('/rest/getArtists.view', methods = [ 'GET', 'POST' ])
|
|
|
|
def list_artists():
|
|
|
|
# According to the API page, there are no parameter?
|
|
|
|
indexes = {}
|
|
|
|
for artist in Artist.query.all():
|
|
|
|
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({
|
|
|
|
'artists': {
|
|
|
|
'index': [ {
|
|
|
|
'name': k,
|
|
|
|
'artist': [ a.as_subsonic_artist() 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():
|
|
|
|
id = request.args.get('id')
|
|
|
|
if not id:
|
|
|
|
return request.error_formatter(10, 'Missing artist id')
|
|
|
|
|
|
|
|
try:
|
|
|
|
aid = uuid.UUID(id)
|
|
|
|
except:
|
|
|
|
return request.error_formatter(0, 'Invalid artist id')
|
|
|
|
|
|
|
|
artist = Artist.query.get(aid)
|
|
|
|
if not artist:
|
|
|
|
return request.error_formatter(70, 'Artist not found'), 404
|
|
|
|
|
|
|
|
info = artist.as_subsonic_artist()
|
|
|
|
info['album'] = [ a.as_subsonic_album() for a in sorted(artist.albums, key = lambda a: a.sort_key()) ]
|
|
|
|
|
|
|
|
return request.formatter({ 'artist': info })
|
|
|
|
|
|
|
|
@app.route('/rest/getAlbum.view', methods = [ 'GET', 'POST' ])
|
|
|
|
def album_info():
|
|
|
|
id = request.args.get('id')
|
|
|
|
if not id:
|
|
|
|
return request.error_formatter(10, 'Missing album id')
|
|
|
|
|
|
|
|
try:
|
|
|
|
aid = uuid.UUID(id)
|
|
|
|
except:
|
|
|
|
return request.error_formatter(0, 'Invalid album id')
|
|
|
|
|
|
|
|
album = Album.query.get(aid)
|
|
|
|
if not album:
|
|
|
|
return request.error_formatter(70, 'Album not found'), 404
|
|
|
|
|
|
|
|
info = album.as_subsonic_album()
|
|
|
|
info['song'] = [ t.as_subsonic_child() for t in sorted(album.tracks, key = lambda t: t.sort_key()) ]
|
|
|
|
|
|
|
|
return request.formatter({ 'artist': info })
|
|
|
|
|
2012-12-01 19:12:35 +00:00
|
|
|
@app.route('/rest/getSong.view', methods = [ 'GET', 'POST' ])
|
|
|
|
def track_info():
|
|
|
|
id = request.args.get('id')
|
|
|
|
if not id:
|
|
|
|
return request.error_formatter(10, 'Missing media id')
|
|
|
|
|
|
|
|
try:
|
|
|
|
tid = uuid.UUID(id)
|
|
|
|
except:
|
|
|
|
return request.error_formatter(0, 'Invalid media id')
|
|
|
|
|
|
|
|
track = Track.query.get(tid)
|
|
|
|
if not track:
|
|
|
|
return request.error_formatter(70, 'Media not found'), 404
|
|
|
|
|
|
|
|
return request.formatter({ 'song': track.as_subsonic_child() })
|
2012-12-01 21:58:17 +00:00
|
|
|
|
|
|
|
@app.route('/rest/getVideos.view', methods = [ 'GET', 'POST' ])
|
|
|
|
def list_videos():
|
|
|
|
return request.error_formatter(0, 'Video streaming not supported')
|
|
|
|
|