mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-23 01:16:18 +00:00
Some factoring...
This commit is contained in:
parent
5895e1c3dd
commit
7477e549f7
@ -3,6 +3,7 @@
|
|||||||
from flask import request
|
from flask import request
|
||||||
import simplejson
|
import simplejson
|
||||||
import cgi
|
import cgi
|
||||||
|
import uuid
|
||||||
|
|
||||||
from web import app
|
from web import app
|
||||||
from user_manager import UserManager
|
from user_manager import UserManager
|
||||||
@ -171,3 +172,19 @@ def hexdecode(enc):
|
|||||||
enc = enc[2:]
|
enc = enc[2:]
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def get_entity(req, ent, param = 'id'):
|
||||||
|
eid = req.args.get(param)
|
||||||
|
if not eid:
|
||||||
|
return False, req.error_formatter(10, 'Missing %s id' % ent.__name__)
|
||||||
|
|
||||||
|
try:
|
||||||
|
eid = uuid.UUID(eid)
|
||||||
|
except:
|
||||||
|
return False, req.error_formatter(0, 'Invalid %s id' % ent.__name__)
|
||||||
|
|
||||||
|
entity = ent.query.get(eid)
|
||||||
|
if not entity:
|
||||||
|
return False, (req.error_formatter(70, '%s not found' % ent.__name__), 404)
|
||||||
|
|
||||||
|
return True, entity
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
from flask import request
|
from flask import request
|
||||||
from web import app
|
from web import app
|
||||||
from db import Folder, Artist, Album, Track
|
from db import Folder, Artist, Album, Track
|
||||||
|
from api import get_entity
|
||||||
import uuid, time, string
|
import uuid, time, string
|
||||||
|
|
||||||
@app.route('/rest/getMusicFolders.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/getMusicFolders.view', methods = [ 'GET', 'POST' ])
|
||||||
@ -85,31 +86,23 @@ def list_indexes():
|
|||||||
|
|
||||||
@app.route('/rest/getMusicDirectory.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/getMusicDirectory.view', methods = [ 'GET', 'POST' ])
|
||||||
def show_directory():
|
def show_directory():
|
||||||
did = request.args.get('id')
|
status, res = get_entity(request, Folder)
|
||||||
if not did:
|
if not status:
|
||||||
return request.error_formatter(10, 'Missing directory id')
|
return res
|
||||||
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 = {
|
directory = {
|
||||||
'id': str(folder.id),
|
'id': str(res.id),
|
||||||
'name': folder.name,
|
'name': res.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()) ]
|
'child': [ f.as_subsonic_child() for f in sorted(res.children, key = lambda c: c.name.lower()) ] + [ t.as_subsonic_child() for t in sorted(res.tracks, key = lambda t: t.sort_key()) ]
|
||||||
}
|
}
|
||||||
if not folder.root:
|
if not res.root:
|
||||||
directory['parent'] = str(folder.parent_id)
|
directory['parent'] = str(res.parent_id)
|
||||||
|
|
||||||
return request.formatter({ 'directory': directory })
|
return request.formatter({ 'directory': directory })
|
||||||
|
|
||||||
@app.route('/rest/getArtists.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/getArtists.view', methods = [ 'GET', 'POST' ])
|
||||||
def list_artists():
|
def list_artists():
|
||||||
# According to the API page, there are no parameter?
|
# According to the API page, there are no parameters?
|
||||||
indexes = {}
|
indexes = {}
|
||||||
for artist in Artist.query.all():
|
for artist in Artist.query.all():
|
||||||
index = artist.name[0].upper()
|
index = artist.name[0].upper()
|
||||||
@ -134,60 +127,33 @@ def list_artists():
|
|||||||
|
|
||||||
@app.route('/rest/getArtist.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/getArtist.view', methods = [ 'GET', 'POST' ])
|
||||||
def artist_info():
|
def artist_info():
|
||||||
id = request.args.get('id')
|
status, res = get_entity(request, Artist)
|
||||||
if not id:
|
if not status:
|
||||||
return request.error_formatter(10, 'Missing artist id')
|
return res
|
||||||
|
|
||||||
try:
|
info = res.as_subsonic_artist()
|
||||||
aid = uuid.UUID(id)
|
info['album'] = [ a.as_subsonic_album() for a in sorted(res.albums, key = lambda a: a.sort_key()) ]
|
||||||
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 })
|
return request.formatter({ 'artist': info })
|
||||||
|
|
||||||
@app.route('/rest/getAlbum.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/getAlbum.view', methods = [ 'GET', 'POST' ])
|
||||||
def album_info():
|
def album_info():
|
||||||
id = request.args.get('id')
|
status, res = get_entity(request, Album)
|
||||||
if not id:
|
if not status:
|
||||||
return request.error_formatter(10, 'Missing album id')
|
return res
|
||||||
|
|
||||||
try:
|
info = res.as_subsonic_album()
|
||||||
aid = uuid.UUID(id)
|
info['song'] = [ t.as_subsonic_child() for t in sorted(res.tracks, key = lambda t: t.sort_key()) ]
|
||||||
except:
|
|
||||||
return request.error_formatter(0, 'Invalid album id')
|
|
||||||
|
|
||||||
album = Album.query.get(aid)
|
return request.formatter({ 'album': info })
|
||||||
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 })
|
|
||||||
|
|
||||||
@app.route('/rest/getSong.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/getSong.view', methods = [ 'GET', 'POST' ])
|
||||||
def track_info():
|
def track_info():
|
||||||
id = request.args.get('id')
|
status, res = get_entity(request, Track)
|
||||||
if not id:
|
if not status:
|
||||||
return request.error_formatter(10, 'Missing media id')
|
return res
|
||||||
|
|
||||||
try:
|
return request.formatter({ 'song': res.as_subsonic_child() })
|
||||||
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() })
|
|
||||||
|
|
||||||
@app.route('/rest/getVideos.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/getVideos.view', methods = [ 'GET', 'POST' ])
|
||||||
def list_videos():
|
def list_videos():
|
||||||
|
65
api/media.py
65
api/media.py
@ -1,29 +1,23 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
from flask import request, send_file
|
from flask import request, send_file
|
||||||
import os.path, uuid
|
import os.path
|
||||||
import Image
|
import Image
|
||||||
from time import time as now
|
from time import time as now
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from web import app
|
from web import app
|
||||||
from db import Track, Folder, User
|
from db import Track, Folder, User
|
||||||
|
from api import get_entity
|
||||||
from lastfm import LastFm
|
from lastfm import LastFm
|
||||||
|
|
||||||
@app.route('/rest/stream.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/stream.view', methods = [ 'GET', 'POST' ])
|
||||||
def stream_media():
|
def stream_media():
|
||||||
id, maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'id', 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ])
|
status, res = get_entity(request, Track)
|
||||||
if not id:
|
if not status:
|
||||||
return request.error_formatter(10, 'Missing media id')
|
return res
|
||||||
|
|
||||||
try:
|
maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ])
|
||||||
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
|
|
||||||
|
|
||||||
if maxBitRate:
|
if maxBitRate:
|
||||||
try:
|
try:
|
||||||
@ -31,7 +25,7 @@ def stream_media():
|
|||||||
except:
|
except:
|
||||||
return request.error_formatter(0, 'Invalid bitrate value')
|
return request.error_formatter(0, 'Invalid bitrate value')
|
||||||
|
|
||||||
if track.bitrate > maxBitRate:
|
if res.bitrate > maxBitRate:
|
||||||
# TODO transcode
|
# TODO transcode
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -40,21 +34,17 @@ def stream_media():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if estimateContentLength == 'true':
|
if estimateContentLength == 'true':
|
||||||
return send_file(track.path), 200, { 'Content-Length': os.path.getsize(track.path) }
|
return send_file(res.path), 200, { 'Content-Length': os.path.getsize(res.path) }
|
||||||
|
|
||||||
return send_file(track.path)
|
return send_file(res.path)
|
||||||
|
|
||||||
@app.route('/rest/getCoverArt.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/getCoverArt.view', methods = [ 'GET', 'POST' ])
|
||||||
def cover_art():
|
def cover_art():
|
||||||
id = request.args.get('id')
|
status, res = get_entity(request, Folder)
|
||||||
if not id:
|
if not status:
|
||||||
return request.error_formatter(10, 'Missing cover art id')
|
return res
|
||||||
try:
|
|
||||||
fid = uuid.UUID(id)
|
if not res.has_cover_art or not os.path.isfile(os.path.join(res.path, 'cover.jpg')):
|
||||||
except:
|
|
||||||
return request.error_formatter(0, 'Invalid cover art id')
|
|
||||||
folder = Folder.query.get(fid)
|
|
||||||
if not folder or not folder.has_cover_art or not os.path.isfile(os.path.join(folder.path, 'cover.jpg')):
|
|
||||||
return request.error_formatter(70, 'Cover art not found')
|
return request.error_formatter(70, 'Cover art not found')
|
||||||
|
|
||||||
size = request.args.get('size')
|
size = request.args.get('size')
|
||||||
@ -64,14 +54,14 @@ def cover_art():
|
|||||||
except:
|
except:
|
||||||
return request.error_formatter(0, 'Invalid size value')
|
return request.error_formatter(0, 'Invalid size value')
|
||||||
else:
|
else:
|
||||||
return send_file(os.path.join(folder.path, 'cover.jpg'))
|
return send_file(os.path.join(res.path, 'cover.jpg'))
|
||||||
|
|
||||||
im = Image.open(os.path.join(folder.path, 'cover.jpg'))
|
im = Image.open(os.path.join(res.path, 'cover.jpg'))
|
||||||
if size > im.size[0] and size > im.size[1]:
|
if size > im.size[0] and size > im.size[1]:
|
||||||
return send_file(os.path.join(folder.path, 'cover.jpg'))
|
return send_file(os.path.join(res.path, 'cover.jpg'))
|
||||||
|
|
||||||
size_path = os.path.join(config.get('CACHE_DIR'), str(size))
|
size_path = os.path.join(config.get('CACHE_DIR'), str(size))
|
||||||
path = os.path.join(size_path, id)
|
path = os.path.join(size_path, str(res.id))
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
return send_file(path)
|
return send_file(path)
|
||||||
if not os.path.exists(size_path):
|
if not os.path.exists(size_path):
|
||||||
@ -83,16 +73,11 @@ def cover_art():
|
|||||||
|
|
||||||
@app.route('/rest/scrobble.view', methods = [ 'GET', 'POST' ])
|
@app.route('/rest/scrobble.view', methods = [ 'GET', 'POST' ])
|
||||||
def scrobble():
|
def scrobble():
|
||||||
tid, time, submission, u = map(request.args.get, [ 'id', 'time', 'submission', 'u' ])
|
status, res = get_entity(request, Track)
|
||||||
if not tid:
|
if not status:
|
||||||
return request.error_formatter(10, 'Missing file id')
|
return res
|
||||||
try:
|
|
||||||
tid = uuid.UUID(tid)
|
time, submission, u = map(request.args.get, [ 'time', 'submission', 'u' ])
|
||||||
except:
|
|
||||||
return request.error_formatter(0, 'Invalid file id')
|
|
||||||
track = Track.query.get(tid)
|
|
||||||
if not track:
|
|
||||||
return request.error_formatter(70, 'File not found')
|
|
||||||
|
|
||||||
if time:
|
if time:
|
||||||
try:
|
try:
|
||||||
@ -106,9 +91,9 @@ def scrobble():
|
|||||||
lfm = LastFm(user, app.logger)
|
lfm = LastFm(user, app.logger)
|
||||||
|
|
||||||
if submission in (None, '', True, 'true', 'True', 1, '1'):
|
if submission in (None, '', True, 'true', 'True', 1, '1'):
|
||||||
lfm.scrobble(track, time)
|
lfm.scrobble(res, time)
|
||||||
else:
|
else:
|
||||||
lfm.now_playing(track)
|
lfm.now_playing(res)
|
||||||
|
|
||||||
return request.formatter({})
|
return request.formatter({})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user