1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00

getNowPlaying.view

Code is specific to a SQLite database
This commit is contained in:
spl0k 2013-06-10 20:56:20 +02:00
parent 57e95bfc30
commit d9caa025fd
3 changed files with 27 additions and 3 deletions

View File

@ -1,13 +1,13 @@
# coding: utf-8
from flask import request
from sqlalchemy import desc, func
from sqlalchemy import desc, func, Interval
from sqlalchemy.orm import aliased
import random
import uuid
from web import app
from db import Track, Folder, Album, Artist
from db import Track, Folder, Album, Artist, User, now
@app.route('/rest/getRandomSongs.view', methods = [ 'GET', 'POST' ])
def rand_songs():
@ -121,3 +121,16 @@ def album_list():
}
})
@app.route('/rest/getNowPlaying.view', methods = [ 'GET', 'POST' ])
def now_playing():
# SQLite specific
query = User.query.join(Track).filter(func.strftime('%s', now()) - func.strftime('%s', User.last_play_date) < Track.duration * 2)
return request.formatter({
'nowPlaying': {
'entry': [ dict(
u.last_play.as_subsonic_child().items() +
{ 'username': u.name, 'minutesAgo': (now() - u.last_play_date).seconds / 60, 'playerId': 0 }.items()
) for u in query ]
}
})

View File

@ -17,7 +17,7 @@ def stream_media():
if not status:
return res
maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ])
maxBitRate, format, timeOffset, size, estimateContentLength, u = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength', 'u' ])
if maxBitRate:
try:
@ -33,8 +33,15 @@ def stream_media():
# TODO transcode
pass
if u:
user = User.query.filter(User.name == u).one()
else:
user = User.query.filter(User.name == request.authorization.username).one()
res.play_count = res.play_count + 1
res.last_play = now()
user.last_play = res
user.last_play_date = now()
session.commit()
if estimateContentLength == 'true':

4
db.py
View File

@ -60,6 +60,10 @@ class User(Base):
lastfm_session = Column(String(32), nullable = True)
lastfm_status = Column(Boolean, default = True) # True: ok/unlinked, False: invalid session
last_play_id = Column(UUID, ForeignKey('track.id'), nullable = True)
last_play = relationship('Track')
last_play_date = Column(DateTime, nullable = True)
class Folder(Base):
__tablename__ = 'folder'