1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00

Converting api:browsing

This commit is contained in:
spl0k 2014-03-24 21:53:19 +01:00
parent 4dd8c28729
commit 8725500ec7
2 changed files with 25 additions and 23 deletions

View File

@ -19,7 +19,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from flask import request from flask import request
from web import app from web import app, store
from db import Folder, Artist, Album, Track from db import Folder, Artist, Album, Track
from . import get_entity from . import get_entity
import uuid, time, string import uuid, time, string
@ -31,7 +31,7 @@ def list_folders():
'musicFolder': [ { 'musicFolder': [ {
'id': str(f.id), 'id': str(f.id),
'name': f.name 'name': f.name
} for f in Folder.query.filter(Folder.root == True).order_by(Folder.name).all() ] } for f in store.find(Folder, Folder.root == True).order_by(Folder.name) ]
} }
}) })
@ -46,25 +46,25 @@ def list_indexes():
return request.error_formatter(0, 'Invalid timestamp') return request.error_formatter(0, 'Invalid timestamp')
if musicFolderId is None: if musicFolderId is None:
folder = Folder.query.filter(Folder.root == True).all() folder = store.find(Folder, Folder.root == True)
else: else:
try: try:
mfid = uuid.UUID(musicFolderId) mfid = uuid.UUID(musicFolderId)
except: except:
return request.error_formatter(0, 'Invalid id') return request.error_formatter(0, 'Invalid id')
folder = Folder.query.get(mfid) folder = store.get(Folder, mfid)
if not folder or (type(folder) is not list and not folder.root): if not folder or (type(folder) is Folder and not folder.root):
return request.error_formatter(70, 'Folder not found') return request.error_formatter(70, 'Folder not found')
last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is not Folder else folder.last_scan
if (not ifModifiedSince is None) and last_modif < ifModifiedSince: if (not ifModifiedSince is None) and last_modif < ifModifiedSince:
return request.formatter({ 'indexes': { 'lastModified': last_modif * 1000 } }) return request.formatter({ 'indexes': { 'lastModified': last_modif * 1000 } })
# The XSD lies, we don't return artists but a directory structure # The XSD lies, we don't return artists but a directory structure
if type(folder) is list: if type(folder) is not Folder:
artists = [] artists = []
childs = [] childs = []
for f in folder: for f in folder:
@ -121,7 +121,7 @@ def show_directory():
def list_artists(): def list_artists():
# According to the API page, there are no parameters? # According to the API page, there are no parameters?
indexes = {} indexes = {}
for artist in Artist.query.all(): for artist in store.find(Artist):
index = artist.name[0].upper() if artist.name else '?' index = artist.name[0].upper() if artist.name else '?'
if index in map(str, xrange(10)): if index in map(str, xrange(10)):
index = '#' index = '#'

32
db.py
View File

@ -26,7 +26,7 @@ from storm.variables import Variable
import uuid, datetime, time import uuid, datetime, time
import os.path import os.path
def now(): def now():
return datetime.datetime.now().replace(microsecond = 0) return datetime.datetime.now().replace(microsecond = 0)
@ -71,14 +71,14 @@ class Folder(object):
if self.has_cover_art: if self.has_cover_art:
info['coverArt'] = str(self.id) info['coverArt'] = str(self.id)
starred = StarredFolder.query.get((user.id, self.id)) starred = Store.of(self).get(StarredFolder, (user.id, self.id))
if starred: if starred:
info['starred'] = starred.date.isoformat() info['starred'] = starred.date.isoformat()
rating = RatingFolder.query.get((user.id, self.id)) rating = Store.of(self).get(RatingFolder, (user.id, self.id))
if rating: if rating:
info['userRating'] = rating.rating info['userRating'] = rating.rating
avgRating = RatingFolder.query.filter(RatingFolder.rated_id == self.id).value(func.avg(RatingFolder.rating)) avgRating = Store.of(self).find(RatingFolder, RatingFolder.rated_id == self.id).avg(RatingFolder.rating)
if avgRating: if avgRating:
info['averageRating'] = avgRating info['averageRating'] = avgRating
@ -95,10 +95,10 @@ class Artist(object):
'id': str(self.id), 'id': str(self.id),
'name': self.name, 'name': self.name,
# coverArt # coverArt
'albumCount': len(self.albums) 'albumCount': self.albums.count()
} }
starred = StarredArtist.query.get((user.id, self.id)) starred = Store.of(self).get(StarredArtist, (user.id, self.id))
if starred: if starred:
info['starred'] = starred.date.isoformat() info['starred'] = starred.date.isoformat()
@ -118,14 +118,16 @@ class Album(object):
'name': self.name, 'name': self.name,
'artist': self.artist.name, 'artist': self.artist.name,
'artistId': str(self.artist_id), 'artistId': str(self.artist_id),
'songCount': len(self.tracks), 'songCount': self.tracks.count(),
'duration': sum(map(lambda t: t.duration, self.tracks)), 'duration': sum(self.tracks.values(Track.duration)),
'created': min(map(lambda t: t.created, self.tracks)).isoformat() 'created': min(self.tracks.values(Track.created)).isoformat()
} }
if self.tracks[0].folder.has_cover_art:
info['coverArt'] = str(self.tracks[0].folder_id)
starred = StarredAlbum.query.get((user.id, self.id)) track_with_cover = self.tracks.find(Track.folder_id == Folder.id, Folder.has_cover_art).any()
if track_with_cover:
info['coverArt'] = str(track_with_cover.folder_id)
starred = Store.of(self).get(StarredAlbum, (user.id, self.id))
if starred: if starred:
info['starred'] = starred.date.isoformat() info['starred'] = starred.date.isoformat()
@ -194,14 +196,14 @@ class Track(object):
if self.folder.has_cover_art: if self.folder.has_cover_art:
info['coverArt'] = str(self.folder_id) info['coverArt'] = str(self.folder_id)
starred = StarredTrack.query.get((user.id, self.id)) starred = Store.of(self).get(StarredTrack, (user.id, self.id))
if starred: if starred:
info['starred'] = starred.date.isoformat() info['starred'] = starred.date.isoformat()
rating = RatingTrack.query.get((user.id, self.id)) rating = Store.of(self).get(RatingTrack, (user.id, self.id))
if rating: if rating:
info['userRating'] = rating.rating info['userRating'] = rating.rating
avgRating = RatingTrack.query.filter(RatingTrack.rated_id == self.id).value(func.avg(RatingTrack.rating)) avgRating = Store.of(self).find(RatingTrack, RatingTrack.rated_id == self.id).avg(RatingTrack.rating)
if avgRating: if avgRating:
info['averageRating'] = avgRating info['averageRating'] = avgRating