From d9caa025fddc9544513bd8bf4c22d3abf054476b Mon Sep 17 00:00:00 2001 From: spl0k Date: Mon, 10 Jun 2013 20:56:20 +0200 Subject: [PATCH] getNowPlaying.view Code is specific to a SQLite database --- api/albums_songs.py | 17 +++++++++++++++-- api/media.py | 9 ++++++++- db.py | 4 ++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/api/albums_songs.py b/api/albums_songs.py index fcd8c6f..9930e0f 100755 --- a/api/albums_songs.py +++ b/api/albums_songs.py @@ -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 ] + } + }) + diff --git a/api/media.py b/api/media.py index 2af667c..b9f4b84 100755 --- a/api/media.py +++ b/api/media.py @@ -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': diff --git a/db.py b/db.py index ef0a35e..4f7ccb2 100755 --- a/db.py +++ b/db.py @@ -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'