2012-10-20 16:05:37 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from flask import request
|
|
|
|
from web import app
|
|
|
|
from db import Track
|
|
|
|
import random
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
@app.route('/rest/getRandomSongs.view')
|
|
|
|
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) ]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|