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:
parent
57e95bfc30
commit
d9caa025fd
@ -1,13 +1,13 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
from flask import request
|
from flask import request
|
||||||
from sqlalchemy import desc, func
|
from sqlalchemy import desc, func, Interval
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
import random
|
import random
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from web import app
|
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' ])
|
@app.route('/rest/getRandomSongs.view', methods = [ 'GET', 'POST' ])
|
||||||
def rand_songs():
|
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 ]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ def stream_media():
|
|||||||
if not status:
|
if not status:
|
||||||
return res
|
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:
|
if maxBitRate:
|
||||||
try:
|
try:
|
||||||
@ -33,8 +33,15 @@ def stream_media():
|
|||||||
# TODO transcode
|
# TODO transcode
|
||||||
pass
|
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.play_count = res.play_count + 1
|
||||||
res.last_play = now()
|
res.last_play = now()
|
||||||
|
user.last_play = res
|
||||||
|
user.last_play_date = now()
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
if estimateContentLength == 'true':
|
if estimateContentLength == 'true':
|
||||||
|
4
db.py
4
db.py
@ -60,6 +60,10 @@ class User(Base):
|
|||||||
lastfm_session = Column(String(32), nullable = True)
|
lastfm_session = Column(String(32), nullable = True)
|
||||||
lastfm_status = Column(Boolean, default = True) # True: ok/unlinked, False: invalid session
|
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):
|
class Folder(Base):
|
||||||
__tablename__ = 'folder'
|
__tablename__ = 'folder'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user