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

Implementing getIndexes.view

It would be nice to ignore prefixes such as 'The' in there. Will think about it later.
This commit is contained in:
Alban 2012-10-14 18:06:48 +02:00
parent 143098ee9a
commit 4410dec0b7
2 changed files with 79 additions and 3 deletions

View File

@ -2,7 +2,8 @@
from flask import request from flask import request
from web import app from web import app
from db import MusicFolder from db import MusicFolder, Artist, Album, Track
import uuid, time, string
@app.route('/rest/getMusicFolders.view') @app.route('/rest/getMusicFolders.view')
def list_folders(): def list_folders():
@ -15,3 +16,78 @@ def list_folders():
} }
}) })
@app.route('/rest/getIndexes.view')
def list_indexes():
musicFolderId = request.args.get('musicFolderId')
ifModifiedSince = request.args.get('ifModifiedSince')
if ifModifiedSince:
try:
ifModifiedSince = int(ifModifiedSince)
except:
return request.formatter({
'error': {
'code': 0,
'message': 'Invalid timestamp'
}
}, error = True)
if musicFolderId is None:
folder = MusicFolder.query.all()
else:
try:
mfid = uuid.UUID(musicFolderId)
except:
return request.formatter({
'error': {
'code': 0,
'message': 'Invalid id'
}
}, error = True)
folder = MusicFolder.query.get(mfid)
if not folder:
return request.formatter({
'error': {
'code': 70,
'message': 'Folder not found'
}
}, error = True)
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 } })
if type(folder) is list:
artists = Artist.query.all()
else:
artists = Artist.query.join(Album, Track).filter(Track.folder_id == mfid)
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()) ]
} for k, v in sorted(indexes.iteritems()) ]
}
})

4
db.py
View File

@ -10,7 +10,7 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import TypeDecorator from sqlalchemy.types import TypeDecorator
from sqlalchemy import BINARY from sqlalchemy import BINARY
import uuid import uuid, datetime
class UUID(TypeDecorator): class UUID(TypeDecorator):
impl = BINARY impl = BINARY
@ -60,7 +60,7 @@ class MusicFolder(Base):
id = UUID.gen_id_column() id = UUID.gen_id_column()
name = Column(String, unique = True) name = Column(String, unique = True)
path = Column(String) path = Column(String)
last_scan = Column(DateTime, nullable = True) last_scan = Column(DateTime, default = datetime.datetime.min)
class Artist(Base): class Artist(Base):
__tablename__ = 'artist' __tablename__ = 'artist'