2012-10-20 16:05:37 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from flask import request
|
2012-12-09 17:39:27 +00:00
|
|
|
from sqlalchemy import desc
|
|
|
|
from sqlalchemy.orm import aliased
|
2012-10-20 16:05:37 +00:00
|
|
|
import random
|
|
|
|
import uuid
|
|
|
|
|
2012-12-09 17:39:27 +00:00
|
|
|
from web import app
|
|
|
|
from db import Track, Folder
|
|
|
|
|
2012-11-22 13:51:43 +00:00
|
|
|
@app.route('/rest/getRandomSongs.view', methods = [ 'GET', 'POST' ])
|
2012-10-20 16:05:37 +00:00
|
|
|
def rand_songs():
|
|
|
|
size = request.args.get('size', '10')
|
|
|
|
genre, fromYear, toYear, musicFolderId = map(request.args.get, [ 'genre', 'fromYear', 'toYear', 'musicFolderId' ])
|
|
|
|
|
|
|
|
try:
|
|
|
|
size = int(size) if size else 10
|
|
|
|
fromYear = int(fromYear) if fromYear else None
|
|
|
|
toYear = int(toYear) if toYear else None
|
|
|
|
fid = uuid.UUID(musicFolderId) if musicFolderId else None
|
|
|
|
except:
|
2012-10-20 18:23:38 +00:00
|
|
|
return request.error_formatter(0, 'Invalid parameter format')
|
2012-10-20 16:05:37 +00:00
|
|
|
|
|
|
|
query = Track.query
|
|
|
|
if fromYear:
|
|
|
|
query = query.filter(Track.year >= fromYear)
|
|
|
|
if toYear:
|
|
|
|
query = query.filter(Track.year <= toYear)
|
|
|
|
if genre:
|
|
|
|
query = query.filter(Track.genre == genre)
|
|
|
|
if fid:
|
2012-11-10 22:02:30 +00:00
|
|
|
query = query.filter(Track.root_folder_id == fid)
|
2012-10-20 16:05:37 +00:00
|
|
|
tracks = query.all()
|
|
|
|
|
|
|
|
if not tracks:
|
|
|
|
return request.formatter({ 'randomSongs': {} })
|
|
|
|
|
|
|
|
return request.formatter({
|
|
|
|
'randomSongs': {
|
|
|
|
'song': [ random.choice(tracks).as_subsonic_child() for x in xrange(size) ]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2012-12-09 17:39:27 +00:00
|
|
|
@app.route('/rest/getAlbumList.view', methods = [ 'GET', 'POST' ])
|
|
|
|
def album_list():
|
|
|
|
ltype, size, offset = map(request.args.get, [ 'type', 'size', 'offset' ])
|
|
|
|
try:
|
|
|
|
size = int(size) if size else 10
|
|
|
|
offset = int(offset) if offset else 0
|
|
|
|
except:
|
|
|
|
return request.error_formatter(0, 'Invalid parameter format')
|
|
|
|
|
|
|
|
query = Folder.query.filter(Folder.tracks.any())
|
|
|
|
if ltype == 'random':
|
|
|
|
albums = query.all()
|
|
|
|
return request.formatter({
|
|
|
|
'albumList': {
|
|
|
|
'album': [ random.choice(albums).as_subsonic_child() for x in xrange(size) ]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
elif ltype == 'newest':
|
|
|
|
query = query.order_by(desc(Folder.created))
|
|
|
|
elif ltype == 'highest':
|
|
|
|
return request.error_formatter(0, 'Not implemented')
|
|
|
|
elif ltype == 'frequent':
|
|
|
|
return request.error_formatter(0, 'Not implemented')
|
|
|
|
elif ltype == 'recent':
|
|
|
|
return request.error_formatter(0, 'Not implemented')
|
|
|
|
elif ltype == 'starred':
|
|
|
|
return request.error_formatter(0, 'Not implemented')
|
|
|
|
elif ltype == 'alphabeticalByName':
|
|
|
|
query = query.order_by(Folder.name)
|
|
|
|
elif ltype == 'alphabeticalByArtist':
|
|
|
|
parent = aliased(Folder)
|
|
|
|
query = query.join(parent, Folder.parent).order_by(parent.name).order_by(Folder.name)
|
|
|
|
else:
|
|
|
|
return request.error_formatter(0, 'Unknown search type')
|
|
|
|
|
|
|
|
return request.formatter({
|
|
|
|
'albumList': {
|
|
|
|
'album': [ f.as_subsonic_child() for f in query.limit(size).offset(offset) ]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|