From f891d8e7f6e2ccbc9681da0c7380e38538050197 Mon Sep 17 00:00:00 2001 From: Ian Esten Date: Fri, 21 Jul 2023 10:26:15 -0700 Subject: [PATCH] fix issue #254. albums now store their path, which is unique to an album. this means that the album artist does not have to be set for a compilation to be correctly grouped as one album instead of one album per artist. --- supysonic/db.py | 2 ++ supysonic/scanner.py | 17 +++++++++++++---- supysonic/schema/mysql.sql | 4 +++- supysonic/schema/postgres.sql | 4 +++- supysonic/schema/sqlite.sql | 4 +++- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/supysonic/db.py b/supysonic/db.py index 0ad4908..af3f7d4 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -258,6 +258,7 @@ class Album(_Model): id = PrimaryKeyField() name = CharField() artist = ForeignKeyField(Artist, backref="albums") + folder = ForeignKeyField(Folder, backref="albums") def as_subsonic_album(self, user): # "AlbumID3" type in XSD duration, created, year = self.tracks.select( @@ -268,6 +269,7 @@ class Album(_Model): "id": str(self.id), "name": self.name, "artist": self.artist.name, + "folderId": str(self.folder_id), "artistId": str(self.artist.id), "songCount": self.tracks.count(), "duration": duration, diff --git a/supysonic/scanner.py b/supysonic/scanner.py index d29399e..e257c57 100644 --- a/supysonic/scanner.py +++ b/supysonic/scanner.py @@ -247,7 +247,7 @@ class Scanner(Thread): trdict["bitrate"] = tag.bitrate // 1000 trdict["last_modification"] = mtime - tralbum = self.__find_album(albumartist, album) + tralbum = self.__find_album(albumartist, album, path) trartist = self.__find_artist(artist) if tr is None: @@ -363,14 +363,23 @@ class Scanner(Thread): folder.cover_art = cover_name folder.save() - def __find_album(self, artist, album): + def __find_album(self, artist, album, track_path): ar = self.__find_artist(artist) al = ar.albums.where(Album.name == album).first() if al: return al - + try: + folder=self.__find_folder(track_path) + folder_id=folder.id + al = folder.albums.first() + if al: + if(al.folder.path == folder.path and al.folder.name == folder.name): + return al + self.__stats.added.albums += 1 + return Album.create(name=album, artist=ar, folder_id=folder_id) + except Album.DoesNotExist: self.__stats.added.albums += 1 - return Album.create(name=album, artist=ar) + return Album.create(name=album, artist=ar, folder_id=folder_id) def __find_artist(self, artist): try: diff --git a/supysonic/schema/mysql.sql b/supysonic/schema/mysql.sql index 4c85bba..75cd486 100644 --- a/supysonic/schema/mysql.sql +++ b/supysonic/schema/mysql.sql @@ -19,9 +19,11 @@ CREATE TABLE IF NOT EXISTS artist ( CREATE TABLE IF NOT EXISTS album ( id CHAR(32) PRIMARY KEY, name VARCHAR(256) NOT NULL, - artist_id CHAR(32) NOT NULL REFERENCES artist(id) + artist_id CHAR(32) NOT NULL REFERENCES artist(id), + folder_id INTEGER NOT NULL REFERENCES folder ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE INDEX index_album_artist_id_fk ON album(artist_id); +CREATE INDEX index_album_folder_id_fk ON album(folder_id); CREATE TABLE IF NOT EXISTS track ( id CHAR(32) PRIMARY KEY, diff --git a/supysonic/schema/postgres.sql b/supysonic/schema/postgres.sql index 1984266..b319b7e 100644 --- a/supysonic/schema/postgres.sql +++ b/supysonic/schema/postgres.sql @@ -19,9 +19,11 @@ CREATE TABLE IF NOT EXISTS artist ( CREATE TABLE IF NOT EXISTS album ( id UUID PRIMARY KEY, name CITEXT NOT NULL, - artist_id UUID NOT NULL REFERENCES artist + artist_id UUID NOT NULL REFERENCES artist, + folder_id INTEGER NOT NULL REFERENCES folder ); CREATE INDEX IF NOT EXISTS index_album_artist_id_fk ON album(artist_id); +CREATE INDEX IF NOT EXISTS index_album_folder_id_fk ON album(folder_id); CREATE TABLE IF NOT EXISTS track ( id UUID PRIMARY KEY, diff --git a/supysonic/schema/sqlite.sql b/supysonic/schema/sqlite.sql index ae39a87..d528e89 100644 --- a/supysonic/schema/sqlite.sql +++ b/supysonic/schema/sqlite.sql @@ -20,9 +20,11 @@ CREATE TABLE IF NOT EXISTS artist ( CREATE TABLE IF NOT EXISTS album ( id CHAR(36) PRIMARY KEY, name VARCHAR(256) NOT NULL COLLATE NOCASE, - artist_id CHAR(36) NOT NULL REFERENCES artist + artist_id CHAR(36) NOT NULL REFERENCES artist, + folder_id INTEGER NOT NULL REFERENCES folder ); CREATE INDEX IF NOT EXISTS index_album_artist_id_fk ON album(artist_id); +CREATE INDEX IF NOT EXISTS index_album_folder_id_fk ON album(folder_id); CREATE TABLE IF NOT EXISTS track ( id CHAR(36) PRIMARY KEY,