From 7b3f4a2b818f304ef35ef21c5a636f111e11f276 Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Thu, 9 Feb 2023 15:49:22 +0100 Subject: [PATCH] refactored --- src/music_kraken/database/database.py | 61 +++++++++++++++-------- src/music_kraken/database/objects/song.py | 53 ++++++-------------- 2 files changed, 56 insertions(+), 58 deletions(-) diff --git a/src/music_kraken/database/database.py b/src/music_kraken/database/database.py index 08e1e5b..6a53d0e 100644 --- a/src/music_kraken/database/database.py +++ b/src/music_kraken/database/database.py @@ -101,23 +101,27 @@ class Database: return if type(db_object) == Song: - return self.push_song(song=db_object) + return self.push_song(song=db_object, pushed=set()) + """ if type(db_object) == Lyrics: return self.push_lyrics(lyrics=db_object) if type(db_object) == Target: return self.push_target(target=db_object) + """ if type(db_object) == Artist: - return self.push_artist(artist=db_object) + return self.push_artist(artist=db_object, pushed=set()) + """ if type(db_object) == Source: # needs to have the property type_enum or type_str set return self.push_source(source=db_object) + """ if type(db_object) == Album: - return self.push_album(album=db_object) + return self.push_album(album=db_object, pushed=set()) logger.warning(f"type {type(db_object)} isn't yet supported by the db") @@ -134,10 +138,14 @@ class Database: for db_object in db_object_list: self.push_one(db_object) - def push_album(self, album: Album): + def push_album(self, album: Album, pushed: set): table = "Album" query = f"INSERT OR REPLACE INTO {table} (id, title, label, album_status, language, date, date_format, country, barcode, albumsort, is_split) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" + if album.id in pushed: + return + pushed.add(album.id) + date_format, date = album.date.get_timestamp_w_format() values = ( @@ -157,19 +165,24 @@ class Database: self.connection.commit() for song in album.tracklist: - self.push_song(song) + self.push_song(song, pushed=pushed) for artist in album.artists: self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference) - self.push_artist(artist) + self.push_artist(artist, pushed=pushed) for source in album.source_list: source.type_enum = SourceTypes.ALBUM source.add_song(album) self.push_source(source=source) - def push_song(self, song: Song): + def push_song(self, song: Song, pushed: set): if song.dynamic: return + + if song.id in pushed: + return + pushed.add(song.id) + # ADDING THE DATA FOR THE SONG OBJECT """ db_field - object attribute @@ -210,18 +223,18 @@ class Database: for main_artist in song.main_artist_list: self.push_artist_song(artist_ref=Reference(main_artist.id), song_ref=Reference(song.id), is_feature=False) - self.push_artist(artist=main_artist) + self.push_artist(artist=main_artist, pushed=pushed) for feature_artist in song.feature_artist_list: self.push_artist_song(artist_ref=Reference(feature_artist.id), song_ref=Reference(song.id), is_feature=True) - self.push_artist(artist=feature_artist) + self.push_artist(artist=feature_artist, pushed=pushed) if song.album is not None: - self.push_album(song.album) + self.push_album(song.album, pushed=pushed) - def push_lyrics(self, lyrics: Lyrics, ): - if lyrics.song_ref_id is None: - logger.warning("the Lyrics don't refer to a song") + def push_lyrics(self, lyrics: Lyrics): + if lyrics.dynamic: + return table = "Lyrics" query = f"INSERT OR REPLACE INTO {table} (id, song_id, text, language) VALUES (?, ?, ?, ?);" @@ -236,8 +249,8 @@ class Database: self.connection.commit() def push_source(self, source: Source): - if source.song_ref_id is None: - logger.warning(f"the Source {source} don't refer to a song") + if source.dynamic: + return table = "Source" query = f"INSERT OR REPLACE INTO {table} (id, type, song_id, src, url) VALUES (?, ?, ?, ?, ?);" @@ -253,9 +266,9 @@ class Database: self.connection.commit() def push_target(self, target: Target): - if target.song_ref_id is None: - logger.warning("the Target doesn't refer to a song") - + if target.dynamic: + return + table = "Target" query = f"INSERT OR REPLACE INTO {table} (id, song_id, file, path) VALUES (?, ?, ?, ?);" values = ( @@ -305,7 +318,13 @@ class Database: self.cursor.execute(query, values) self.connection.commit() - def push_artist(self, artist: Artist): + def push_artist(self, artist: Artist, pushed: set): + if artist.dynamic: + return + if artist.id in pushed: + return + pushed.add(artist.id) + table = "Artist" query = f"INSERT OR REPLACE INTO {table} (id, name) VALUES (?, ?);" values = ( @@ -318,11 +337,11 @@ class Database: for song in artist.feature_songs: self.push_artist_song(artist_ref=artist.reference, song_ref=song.reference, is_feature=True) - self.push_song(song=song) + self.push_song(song=song, pushed=pushed) for song in artist.main_songs: self.push_artist_song(artist_ref=artist.reference, song_ref=song.reference, is_feature=False) - self.push_song(song=song) + self.push_song(song=song, pushed=pushed) for album in artist.main_albums: self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference) diff --git a/src/music_kraken/database/objects/song.py b/src/music_kraken/database/objects/song.py index 51f286f..f6d00d9 100644 --- a/src/music_kraken/database/objects/song.py +++ b/src/music_kraken/database/objects/song.py @@ -395,10 +395,8 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): id_: str = None, name: str = None, source_list: List[Source] = None, - main_songs: List[Song] = None, feature_songs: List[Song] = None, main_albums: List[Album] = None, - album_type: str = None, notes: FormattedText = None, lyrical_themes: List[str] = None, general_genre: str = "", @@ -410,7 +408,6 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): """ TODO implement album type and notes """ - self.album_type = album_type self.country: pycountry.Country = country self.formed_in: ID3Timestamp = formed_in """ @@ -418,24 +415,11 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): which are meant to only use in outputs to describe the object i mean do as you want but there aint no strict rule about em so good luck """ - self.notes: FormattedText = notes - if self.notes is None: - self.notes = FormattedText() - self.lyrical_themes: List[str] = lyrical_themes - if self.lyrical_themes is None: - self.lyrical_themes = [] + self.notes: FormattedText = notes or FormattedText() + self.lyrical_themes: List[str] = lyrical_themes or [] self.general_genre = general_genre - if main_albums is None: - main_albums = [] - if feature_songs is None: - feature_songs = [] - if main_songs is None: - main_songs = [] - - self.name: str | None = name - - self.main_songs = main_songs + self.name: str = name self.feature_songs = Collection( data=feature_songs, @@ -478,19 +462,16 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): return feature_release - def get_songs(self) -> Album: - song_release = Album( - title="song collection", - album_status="dynamic", - is_split=False, - albumsort=666, - dynamic=True - ) - for song in self.main_songs: - song_release.add_song(song) - for song in self.feature_songs: - song_release.add_song(song) - return song_release + def get_all_songs(self) -> List[Song]: + """ + returns a list of all Songs. + probaply not that usefull, because it is unsorted + """ + collection = [] + for album in self.discography: + collection.extend(album) + + return collection def get_discography(self) -> List[Album]: flat_copy_discography = self.main_albums.copy() @@ -508,10 +489,8 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): def get_options(self) -> list: options = [self] - for album in self.main_albums: - new_album: Album = copy.copy(album) - new_album.artists.append(self) - options.append(new_album) + options.extend(self.main_albums) + options.extend(self.feature_songs) return options def get_option_string(self) -> str: @@ -519,4 +498,4 @@ class Artist(DatabaseObject, SourceAttribute, MetadataAttribute): discography: List[Album] = property(fget=get_discography) features: Album = property(fget=get_features) - songs: Album = property(fget=get_songs) + all_songs: Album = property(fget=get_all_songs)