1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00
supysonic/api/media.py

103 lines
2.5 KiB
Python
Raw Normal View History

2012-10-20 18:05:39 +00:00
# coding: utf-8
from flask import request, send_file
2012-12-02 15:42:25 +00:00
import os.path
2012-11-11 20:39:26 +00:00
import Image
2012-11-23 16:13:25 +00:00
from time import time as now
2012-11-11 20:39:26 +00:00
import config
2012-11-23 16:13:25 +00:00
from web import app
from db import Track, Folder, User
2012-12-02 15:42:25 +00:00
from api import get_entity
2012-11-23 16:13:25 +00:00
from lastfm import LastFm
2012-10-20 18:05:39 +00:00
2012-11-22 13:51:43 +00:00
@app.route('/rest/stream.view', methods = [ 'GET', 'POST' ])
2012-10-20 18:05:39 +00:00
def stream_media():
2012-12-02 15:42:25 +00:00
status, res = get_entity(request, Track)
if not status:
return res
2012-10-20 18:05:39 +00:00
2012-12-02 15:42:25 +00:00
maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ])
2012-10-20 18:05:39 +00:00
if maxBitRate:
try:
maxBitRate = int(maxBitRate)
except:
return request.error_formatter(0, 'Invalid bitrate value')
2012-10-20 18:05:39 +00:00
2012-12-02 15:42:25 +00:00
if res.bitrate > maxBitRate:
2012-10-20 18:05:39 +00:00
# TODO transcode
pass
if format != 'mp3':
# TODO transcode
pass
if estimateContentLength == 'true':
2012-12-02 15:42:25 +00:00
return send_file(res.path), 200, { 'Content-Length': os.path.getsize(res.path) }
2012-10-20 18:05:39 +00:00
2012-12-02 15:42:25 +00:00
return send_file(res.path)
2012-10-20 18:05:39 +00:00
2012-11-22 13:51:43 +00:00
@app.route('/rest/getCoverArt.view', methods = [ 'GET', 'POST' ])
2012-11-11 20:39:26 +00:00
def cover_art():
2012-12-02 15:42:25 +00:00
status, res = get_entity(request, Folder)
if not status:
return res
if not res.has_cover_art or not os.path.isfile(os.path.join(res.path, 'cover.jpg')):
2012-11-11 20:39:26 +00:00
return request.error_formatter(70, 'Cover art not found')
size = request.args.get('size')
if size:
try:
size = int(size)
except:
return request.error_formatter(0, 'Invalid size value')
else:
2012-12-02 15:42:25 +00:00
return send_file(os.path.join(res.path, 'cover.jpg'))
2012-11-11 20:39:26 +00:00
2012-12-02 15:42:25 +00:00
im = Image.open(os.path.join(res.path, 'cover.jpg'))
2012-11-11 20:39:26 +00:00
if size > im.size[0] and size > im.size[1]:
2012-12-02 15:42:25 +00:00
return send_file(os.path.join(res.path, 'cover.jpg'))
2012-11-11 20:39:26 +00:00
size_path = os.path.join(config.get('CACHE_DIR'), str(size))
2012-12-02 15:42:25 +00:00
path = os.path.join(size_path, str(res.id))
2012-11-11 20:39:26 +00:00
if os.path.exists(path):
return send_file(path)
if not os.path.exists(size_path):
os.makedirs(size_path)
im.thumbnail([size, size], Image.ANTIALIAS)
im.save(path, 'JPEG')
return send_file(path)
2012-11-23 16:13:25 +00:00
@app.route('/rest/scrobble.view', methods = [ 'GET', 'POST' ])
def scrobble():
2012-12-02 15:42:25 +00:00
status, res = get_entity(request, Track)
if not status:
return res
time, submission, u = map(request.args.get, [ 'time', 'submission', 'u' ])
2012-11-23 16:13:25 +00:00
if time:
try:
time = int(time) / 1000
except:
return request.error_formatter(0, 'Invalid time value')
else:
time = int(now())
if u:
user = User.query.filter(User.name == u).one()
else:
user = User.query.filter(User.name == request.authorization.username).one()
2012-11-23 16:13:25 +00:00
lfm = LastFm(user, app.logger)
if submission in (None, '', True, 'true', 'True', 1, '1'):
2012-12-02 15:42:25 +00:00
lfm.scrobble(res, time)
2012-11-23 16:13:25 +00:00
else:
2012-12-02 15:42:25 +00:00
lfm.now_playing(res)
2012-11-23 16:13:25 +00:00
return request.formatter({})