diff --git a/pysonic/api.py b/pysonic/api.py index ce2a354..d573c79 100644 --- a/pysonic/api.py +++ b/pysonic/api.py @@ -310,3 +310,13 @@ class PysonicApi(object): tag.append(folder) yield doc.prettify() + @cherrypy.expose + def star_view(self, id, **kwargs): + self.library.set_starred(cherrypy.request.login, int(id), starred=True) + print(cherrypy.request.login) + yield self.response()[0].prettify() + + @cherrypy.expose + def unstar_view(self, id, **kwargs): + self.library.set_starred(cherrypy.request.login, int(id), starred=False) + yield self.response()[0].prettify() diff --git a/pysonic/database.py b/pysonic/database.py index 1d9e677..5bf27ae 100644 --- a/pysonic/database.py +++ b/pysonic/database.py @@ -73,6 +73,15 @@ class PysonicDatabase(object): 'email' TEXT)""" cursor.execute(users_table) version = 1 + if version < 2: + logging.warning("migrating database to v2 from %s", version) + users_table = """CREATE TABLE 'stars' ( + 'userid' INTEGER, + 'nodeid' INTEGER, + primary key ('userid', 'nodeid'))""" + cursor.execute(users_table) + version = 2 + cursor.execute("""UPDATE meta SET value=? WHERE key="db_version";""", (str(version), )) logging.warning("db schema is version {}".format(version)) @@ -154,3 +163,10 @@ class PysonicDatabase(object): except IndexError: raise NotFoundError("User doesn't exist") + def set_starred(self, user_id, node_id, starred=True): + with closing(self.db.cursor()) as cursor: + if starred: + query = "INSERT INTO stars (userid, nodeid) VALUES (?, ?);" + else: + query = "DELETE FROM stars WHERE userid=? and nodeid=?;" + cursor.execute(query, (user_id, node_id)) diff --git a/pysonic/library.py b/pysonic/library.py index 9357d98..99da73c 100644 --- a/pysonic/library.py +++ b/pysonic/library.py @@ -94,6 +94,9 @@ class PysonicLibrary(object): "largeImageUrl": "", "similarArtists": []} + def set_starred(self, username, node_id, starred): + self.db.set_starred(self.db.get_user(username)["id"], node_id, starred) + def get_user(self, user): if type(user) is int: return self.db.get_user(username)