From 8340bb3c61932a1b7589bf4a1bdf58ec84270236 Mon Sep 17 00:00:00 2001 From: dave Date: Sat, 7 Apr 2018 15:24:02 -0700 Subject: [PATCH] Support recently/most played albums views --- pysonic/api.py | 15 ++++++++++++--- pysonic/database.py | 12 ++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/pysonic/api.py b/pysonic/api.py index fd33a7f..ba1c052 100644 --- a/pysonic/api.py +++ b/pysonic/api.py @@ -17,8 +17,11 @@ class PysonicSubsonicApi(object): self.options = options @cherrypy.expose + @formatresponse def index(self): - return str(self.library.db.get_stats()) + response = ApiResponse() + response.add_child("totals", **self.library.db.get_stats()) + return response @cherrypy.expose @formatresponse @@ -73,8 +76,10 @@ class PysonicSubsonicApi(object): qargs.update(sortby="name", order="asc") elif type == "newest": qargs.update(sortby="added", order="desc") - else: - raise NotImplemented() + elif type == "recent": + qargs.update(sortby="played", order="desc") + elif type == "frequent": + qargs.update(sortby="plays", order="desc") qargs.update(limit=(offset, size)) @@ -415,6 +420,10 @@ class PysonicSubsonicApi(object): @cherrypy.expose def savePlayQueue_view(self, id, current, position, **kwargs): print("TODO save playqueue with items {} current {} position {}".format(id, current, position)) + + song = self.library.get_song(int(current)) + self.library.db.update_album_played(song['albumid'], time()) + self.library.db.increment_album_plays(song['albumid']) # TODO save playlist with items ['378', '386', '384', '380', '383'] current 383 position 4471 # id entries are strings! diff --git a/pysonic/database.py b/pysonic/database.py index c473033..cdcc2f7 100644 --- a/pysonic/database.py +++ b/pysonic/database.py @@ -82,6 +82,8 @@ class PysonicDatabase(object): 'dir' INTEGER, 'name' TEXT, 'added' INTEGER NOT NULL DEFAULT -1, + 'played' INTEGER, + 'plays' INTEGER NOT NULL DEFAULT 0, UNIQUE (artistid, dir));""", """CREATE TABLE 'songs' ( 'id' INTEGER PRIMARY KEY AUTOINCREMENT, @@ -463,6 +465,16 @@ class PysonicDatabase(object): cursor.execute("DELETE FROM playlists WHERE id=?", (playlist_id, )) cursor.execute("COMMIT") + @readcursor + def update_album_played(self, cursor, album_id, last_played=None): + cursor.execute("UPDATE albums SET played=? WHERE id=?", (last_played, album_id, )) + cursor.execute("COMMIT") + + @readcursor + def increment_album_plays(self, cursor, album_id): + cursor.execute("UPDATE albums SET plays = plays + 1 WHERE id=?", (album_id, )) + cursor.execute("COMMIT") + # User related @readcursor def add_user(self, cursor, username, password, is_admin=False):