diff --git a/pysonic/api.py b/pysonic/api.py index 14cb9ed..dd77fbc 100644 --- a/pysonic/api.py +++ b/pysonic/api.py @@ -271,8 +271,9 @@ class PysonicApi(object): moreargs.update(name=child["name"], isDir="true", # TODO song files in artist dir parent=entity["id"], - coverArt=child["coverid"], id=child["dir"]) + if child["coverid"]: + moreargs.update(coverArt=child["coverid"]) # album=item["name"], # title=item["name"], # TODO dupe? # artist=artist["name"], @@ -281,13 +282,14 @@ class PysonicApi(object): moreargs.update(name=child["title"], artist=child["_artist"]["name"], contentType=child["format"], - coverArt=entity["coverid"], - id=child["id"], # this is probably fucked ? + id=child["id"], duration=child["length"], isDir="false", parent=entity["dir"], # title=xxx ) + if entity["coverid"]: + moreargs.update(coverArt=entity["coverid"]) # duration="230" size="8409237" suffix="mp3" track="2" year="2005"/> response.add_child("child", _parent="directory", size="4096", @@ -346,11 +348,7 @@ class PysonicApi(object): maxBitRate = int(maxBitRate) assert maxBitRate >= 32 and maxBitRate <= 320 song = self.library.get_song(id) - fpath = "library/" + song["file"] - # import pdb - # from pprint import pprint - # pdb.set_trace() - # meta = self.library.get_file_metadata(id) + fpath = song["_fullpath"] to_bitrate = min(maxBitRate, self.options.max_bitrate, song.get("bitrate", 320 * 1024) / 1024) cherrypy.response.headers['Content-Type'] = 'audio/mpeg' #if "media_length" in meta: @@ -416,7 +414,7 @@ class PysonicApi(object): @cherrypy.expose def getCoverArt_view(self, id, **kwargs): cover = self.library.get_cover(id) - fpath = "library/" + cover["path"] + fpath = cover["_fullpath"] type2ct = { 'jpg': 'image/jpeg', 'png': 'image/png', diff --git a/pysonic/database.py b/pysonic/database.py index c633459..ccfe2a8 100644 --- a/pysonic/database.py +++ b/pysonic/database.py @@ -1,5 +1,3 @@ -import os -import json import sqlite3 import logging from hashlib import sha512 @@ -75,6 +73,7 @@ class PysonicDatabase(object): UNIQUE (artistid, dir));""", """CREATE TABLE 'songs' ( 'id' INTEGER PRIMARY KEY AUTOINCREMENT, + 'library' INTEGER, 'albumid' BOOLEAN, 'file' TEXT UNIQUE, -- path from the library root 'size' INTEGER NOT NULL DEFAULT -1, @@ -88,6 +87,7 @@ class PysonicDatabase(object): )""", """CREATE TABLE 'covers' ( 'id' INTEGER PRIMARY KEY AUTOINCREMENT, + 'library' INTEGER, 'type' TEXT, 'size' TEXT, 'path' TEXT UNIQUE);""", @@ -113,7 +113,6 @@ class PysonicDatabase(object): if len(cursor.fetchall()) == 0: logging.warning("Initializing database") for query in queries: - print(query) cursor.execute(query) cursor.execute("COMMIT") else: @@ -140,9 +139,17 @@ class PysonicDatabase(object): raise DuplicateRootException("Root '{}' already exists".format(path)) @readcursor - def get_libraries(self, cursor): + def get_libraries(self, cursor, id=None): libs = [] - cursor.execute("SELECT * FROM libraries") + q = "SELECT * FROM libraries" + params = [] + conditions = [] + if id: + conditions.append("id = ?") + params.append(id) + if conditions: + q += " WHERE " + " AND ".join(conditions) + cursor.execute(q, params) for row in cursor: libs.append(row) return libs @@ -164,8 +171,6 @@ class PysonicDatabase(object): q += " WHERE " + " AND ".join(conditions) if sortby: q += " ORDER BY {} {}".format(sortby, order.upper() if order else "ASC") - print(q) - print(params) cursor.execute(q, params) for row in cursor: artists.append(row) diff --git a/pysonic/library.py b/pysonic/library.py index 57c9e3b..611214f 100644 --- a/pysonic/library.py +++ b/pysonic/library.py @@ -35,8 +35,8 @@ class PysonicLibrary(object): self.get_libraries = self.db.get_libraries self.get_artists = self.db.get_artists self.get_albums = self.db.get_albums - self.get_song = self.db.get_song - self.get_cover = self.db.get_cover + # self.get_song = self.db.get_song + # self.get_cover = self.db.get_cover self.scanner = PysonicFilesystemScanner(self) logging.info("library ready") @@ -76,12 +76,17 @@ class PysonicLibrary(object): "largeImageUrl": "", "similarArtists": []} - # def get_cover(self, cover_id): - # cover = self.db.get_cover(cover_id) - - - + def get_cover(self, cover_id): + cover = self.db.get_cover(cover_id) + library = self.db.get_libraries(cover["library"])[0] + cover['_fullpath'] = os.path.join(library["path"], cover["path"]) + return cover + def get_song(self, song_id): + song = self.db.get_song(song_id) + library = self.db.get_libraries(song["library"])[0] + song['_fullpath'] = os.path.join(library["path"], song["file"]) + return song # #@memoize # def get_libraries(self): diff --git a/pysonic/scanner.py b/pysonic/scanner.py index a0139e8..87f31ad 100644 --- a/pysonic/scanner.py +++ b/pysonic/scanner.py @@ -136,9 +136,10 @@ class PysonicFilesystemScanner(object): if not cursor.fetchall(): # We leave most fields blank now and return later - cursor.execute("INSERT INTO songs (albumid, file, size, title) " - "VALUES (?, ?, ?, ?)", - (album_id, + cursor.execute("INSERT INTO songs (library, albumid, file, size, title) " + "VALUES (?, ?, ?, ?, ?)", + (pid, + album_id, fpath, os.stat(os.path.join(root, fpath)).st_size, file, )) @@ -153,7 +154,7 @@ class PysonicFilesystemScanner(object): cursor.execute("SELECT id FROM covers WHERE path=?", (fpath, )) if not cursor.fetchall(): # We leave most fields blank now and return later - cursor.execute("INSERT INTO covers (path) VALUES (?);", (fpath, )) + cursor.execute("INSERT INTO covers (library, path) VALUES (?, ?);", (pid, fpath, )) cursor.execute("UPDATE albums SET coverid=? WHERE id=?", (cursor.lastrowid, album_id)) break