1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-13 21:52:18 +00:00
supysonic/api/albums_songs.py
2012-12-09 21:38:05 +01:00

42 lines
1.1 KiB
Python
Executable File

# coding: utf-8
from flask import request
from web import app
from db import Track
import random
import uuid
@app.route('/rest/getRandomSongs.view', methods = [ 'GET', 'POST' ])
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:
return request.error_formatter(0, 'Invalid parameter format')
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:
query = query.filter(Track.root_folder_id == fid)
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) ]
}
})