From b4e3b220898f38ef7a40d8ab89a176dc64e3b682 Mon Sep 17 00:00:00 2001 From: spl0k Date: Thu, 13 Jun 2013 18:44:56 +0200 Subject: [PATCH] Added starred info to responses --- api/albums_songs.py | 22 +++++++++++----------- api/browse.py | 16 ++++++++-------- db.py | 31 +++++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/api/albums_songs.py b/api/albums_songs.py index 54669bf..afcf487 100755 --- a/api/albums_songs.py +++ b/api/albums_songs.py @@ -38,7 +38,7 @@ def rand_songs(): return request.formatter({ 'randomSongs': { - 'song': [ random.choice(tracks).as_subsonic_child() for x in xrange(size) ] + 'song': [ random.choice(tracks).as_subsonic_child(request.user) for x in xrange(size) ] } }) @@ -56,7 +56,7 @@ def album_list(): albums = query.all() return request.formatter({ 'albumList': { - 'album': [ random.choice(albums).as_subsonic_child() for x in xrange(size) ] + 'album': [ random.choice(albums).as_subsonic_child(request.user) for x in xrange(size) ] } }) elif ltype == 'newest': @@ -79,7 +79,7 @@ def album_list(): return request.formatter({ 'albumList': { - 'album': [ f.as_subsonic_child() for f in query.limit(size).offset(offset) ] + 'album': [ f.as_subsonic_child(request.user) for f in query.limit(size).offset(offset) ] } }) @@ -97,7 +97,7 @@ def album_list_id3(): albums = query.all() return request.formatter({ 'albumList2': { - 'album': [ random.choice(albums).as_subsonic_album() for x in xrange(size) ] + 'album': [ random.choice(albums).as_subsonic_album(request.user) for x in xrange(size) ] } }) elif ltype == 'newest': @@ -117,7 +117,7 @@ def album_list_id3(): return request.formatter({ 'albumList2': { - 'album': [ f.as_subsonic_album() for f in query.limit(size).offset(offset) ] + 'album': [ f.as_subsonic_album(request.user) for f in query.limit(size).offset(offset) ] } }) @@ -128,7 +128,7 @@ def now_playing(): return request.formatter({ 'nowPlaying': { 'entry': [ dict( - u.last_play.as_subsonic_child().items() + + u.last_play.as_subsonic_child(request.user).items() + { 'username': u.name, 'minutesAgo': (now() - u.last_play_date).seconds / 60, 'playerId': 0 }.items() ) for u in query ] } @@ -139,8 +139,8 @@ def get_starred(): return request.formatter({ 'starred': { 'artist': [ { 'id': sf.starred.name, 'name': sf.starred_id } for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(~ Folder.tracks.any()) ], - 'album': [ sf.starred.as_subsonic_child() for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(Folder.tracks.any()) ], - 'song': [ st.starred.as_subsonic_child() for st in StarredTrack.query.join(User).filter(User.name == request.username) ] + 'album': [ sf.starred.as_subsonic_child(request.user) for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(Folder.tracks.any()) ], + 'song': [ st.starred.as_subsonic_child(request.user) for st in StarredTrack.query.join(User).filter(User.name == request.username) ] } }) @@ -148,9 +148,9 @@ def get_starred(): def get_starred_id3(): return request.formatter({ 'starred2': { - 'artist': [ sa.starred.as_subsonic_artist() for sa in StarredArtist.query.join(User).filter(User.name == request.username) ], - 'album': [ sa.starred.as_subsonic_album() for sa in StarredAlbum.query.join(User).filter(User.name == request.username) ], - 'song': [ st.starred.as_subsonic_child() for st in StarredTrack.query.join(User).filter(User.name == request.username) ] + 'artist': [ sa.starred.as_subsonic_artist(request.user) for sa in StarredArtist.query.join(User).filter(User.name == request.username) ], + 'album': [ sa.starred.as_subsonic_album(request.user) for sa in StarredAlbum.query.join(User).filter(User.name == request.username) ], + 'song': [ st.starred.as_subsonic_child(request.user) for st in StarredTrack.query.join(User).filter(User.name == request.username) ] } }) diff --git a/api/browse.py b/api/browse.py index 593360f..625238a 100755 --- a/api/browse.py +++ b/api/browse.py @@ -80,7 +80,7 @@ def list_indexes(): 'name': a.name } for a in sorted(v, key = lambda a: a.name.lower()) ] } for k, v in sorted(indexes.iteritems()) ], - 'child': [ c.as_subsonic_child() for c in sorted(childs, key = lambda t: t.sort_key()) ] + 'child': [ c.as_subsonic_child(request.user) for c in sorted(childs, key = lambda t: t.sort_key()) ] } }) @@ -93,7 +93,7 @@ def show_directory(): directory = { 'id': str(res.id), 'name': res.name, - 'child': [ f.as_subsonic_child() for f in sorted(res.children, key = lambda c: c.name.lower()) ] + [ t.as_subsonic_child() for t in sorted(res.tracks, key = lambda t: t.sort_key()) ] + 'child': [ f.as_subsonic_child(request.user) for f in sorted(res.children, key = lambda c: c.name.lower()) ] + [ t.as_subsonic_child(request.user) for t in sorted(res.tracks, key = lambda t: t.sort_key()) ] } if not res.root: directory['parent'] = str(res.parent_id) @@ -120,7 +120,7 @@ def list_artists(): 'artists': { 'index': [ { 'name': k, - 'artist': [ a.as_subsonic_artist() for a in sorted(v, key = lambda a: a.name.lower()) ] + 'artist': [ a.as_subsonic_artist(request.user) for a in sorted(v, key = lambda a: a.name.lower()) ] } for k, v in sorted(indexes.iteritems()) ] } }) @@ -131,8 +131,8 @@ def artist_info(): if not status: return res - info = res.as_subsonic_artist() - info['album'] = [ a.as_subsonic_album() for a in sorted(res.albums, key = lambda a: a.sort_key()) ] + info = res.as_subsonic_artist(request.user) + info['album'] = [ a.as_subsonic_album(request.user) for a in sorted(res.albums, key = lambda a: a.sort_key()) ] return request.formatter({ 'artist': info }) @@ -142,8 +142,8 @@ def album_info(): if not status: return res - info = res.as_subsonic_album() - info['song'] = [ t.as_subsonic_child() for t in sorted(res.tracks, key = lambda t: t.sort_key()) ] + info = res.as_subsonic_album(request.user) + info['song'] = [ t.as_subsonic_child(request.user) for t in sorted(res.tracks, key = lambda t: t.sort_key()) ] return request.formatter({ 'album': info }) @@ -153,7 +153,7 @@ def track_info(): if not status: return res - return request.formatter({ 'song': res.as_subsonic_child() }) + return request.formatter({ 'song': res.as_subsonic_child(request.user) }) @app.route('/rest/getVideos.view', methods = [ 'GET', 'POST' ]) def list_videos(): diff --git a/db.py b/db.py index cd09959..54fe6a5 100755 --- a/db.py +++ b/db.py @@ -78,7 +78,7 @@ class Folder(Base): parent_id = Column(UUID, ForeignKey('folder.id'), nullable = True) children = relationship('Folder', backref = backref('parent', remote_side = [ id ])) - def as_subsonic_child(self): + def as_subsonic_child(self, user): info = { 'id': str(self.id), 'isDir': True, @@ -92,6 +92,10 @@ class Folder(Base): if self.has_cover_art: info['coverArt'] = str(self.id) + starred = StarredFolder.query.filter(StarredFolder.starred_id == self.id).filter(StarredFolder.user_id == user.id).first() + if starred: + info['starred'] = starred.date.isoformat() + return info class Artist(Base): @@ -101,15 +105,20 @@ class Artist(Base): name = Column(String, unique = True) albums = relationship('Album', backref = 'artist') - def as_subsonic_artist(self): - return { + def as_subsonic_artist(self, user): + info = { 'id': str(self.id), 'name': self.name, # coverArt 'albumCount': len(self.albums) - # starred } + starred = StarredArtist.query.filter(StarredArtist.starred_id == self.id).filter(StarredArtist.user_id == user.id).first() + if starred: + info['starred'] = starred.date.isoformat() + + return info + class Album(Base): __tablename__ = 'album' @@ -118,7 +127,7 @@ class Album(Base): artist_id = Column(UUID, ForeignKey('artist.id')) tracks = relationship('Track', backref = 'album') - def as_subsonic_album(self): + def as_subsonic_album(self, user): info = { 'id': str(self.id), 'name': self.name, @@ -127,11 +136,14 @@ class Album(Base): 'songCount': len(self.tracks), 'duration': sum(map(lambda t: t.duration, self.tracks)), 'created': min(map(lambda t: t.created, self.tracks)).isoformat() - # starred } if self.tracks[0].folder.has_cover_art: info['coverArt'] = str(self.tracks[0].folder_id) + starred = StarredAlbum.query.filter(StarredAlbum.starred_id == self.id).filter(StarredAlbum.user_id == user.id).first() + if starred: + info['starred'] = starred.date.isoformat() + return info def sort_key(self): @@ -163,7 +175,7 @@ class Track(Base): folder_id = Column(UUID, ForeignKey('folder.id')) folder = relationship('Folder', primaryjoin = Folder.id == folder_id, backref = 'tracks') - def as_subsonic_child(self): + def as_subsonic_child(self, user): info = { 'id': str(self.id), 'parent': str(self.folder.id), @@ -193,11 +205,14 @@ class Track(Base): if self.folder.has_cover_art: info['coverArt'] = str(self.folder_id) + starred = StarredTrack.query.filter(StarredTrack.starred_id == self.id).filter(StarredTrack.user_id == user.id).first() + if starred: + info['starred'] = starred.date.isoformat() + # transcodedContentType # transcodedSuffix # userRating # averageRating - # starred return info