This commit is contained in:
Hellow 2022-12-16 16:59:21 +01:00
parent 0c2b604b92
commit 23b3ab0101
5 changed files with 178 additions and 88 deletions

Binary file not shown.

View File

@ -21,6 +21,8 @@ main_artist = Artist(
name="I'm in a coffin"
)
artist_ref = main_artist.reference
split_artist = Artist(
name="split"
)
@ -94,3 +96,11 @@ div()
album_output_list = cache.pull_albums(song_ref=song_ref)
print(album_output_list)
print("len of album ->", len(album_output_list[0]), album_output_list[0], sep=" | ")
# get artist
div()
artist_output = cache.pull_artists(artist_ref=artist_ref)[0]
print(artist_output)
print(artist_output.main_albums)
print(artist_output.main_songs)
print(artist_output.feature_songs)

View File

@ -148,6 +148,12 @@ class Database:
self.cursor.execute(query, values)
self.connection.commit()
for song in album.tracklist:
self.push_song(song)
for artist in album.artists:
self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference)
self.push_artist(artist)
def push_song(self, song: Song):
# ADDING THE DATA FOR THE SONG OBJECT
"""
@ -184,7 +190,7 @@ 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)
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)
@ -257,6 +263,24 @@ class Database:
self.cursor.execute(query, values)
self.connection.commit()
def push_artist_album(self, artist_ref: Reference, album_ref: Reference):
table = "AlbumArtist"
# checking if already exists
query = f"SELECT * FROM {table} WHERE album_id=\"{album_ref.id}\" AND artist_id=\"{artist_ref.id}\""
self.cursor.execute(query)
if len(self.cursor.fetchall()) > 0:
# join already exists
return
query = f"INSERT OR REPLACE INTO {table} (album_id, artist_id) VALUES (?, ?);"
values = (
album_ref.id,
artist_ref.id
)
self.cursor.execute(query, values)
self.connection.commit()
def push_artist(self, artist: Artist):
table = "Artist"
query = f"INSERT OR REPLACE INTO {table} (id, name) VALUES (?, ?);"
@ -268,6 +292,16 @@ class Database:
self.cursor.execute(query, values)
self.connection.commit()
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)
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)
for album in artist.main_albums:
self.push_artist_album(artist_ref=artist.reference, album_ref=album.reference)
def pull_lyrics(self, song_ref: Reference = None, lyrics_ref: Reference = None) -> List[Lyrics]:
"""
@ -331,7 +365,7 @@ class Database:
where_str = ""
if len(wheres) > 0:
where_str = "WHERE " + " AND ".join(wheres)
query = f"SELECT * FROM {table} {where_str};"
self.cursor.execute(query)
joins = self.cursor.fetchall()
@ -342,17 +376,71 @@ class Database:
bool(join["is_feature"])
) for join in joins]
def get_artist_from_row(self, artist_row, exclude_relations: set = None) -> Artist:
def pull_artist_album(self, album_ref: Reference = None, artist_ref: Reference = None) -> List[tuple]:
table = "AlbumArtist"
wheres = []
if album_ref is not None:
wheres.append(f"album_id=\"{album_ref.id}\"")
if artist_ref is not None:
wheres.append(f"artist_id=\"{artist_ref.id}\"")
where_str = ""
if len(wheres) > 0:
where_str = "WHERE " + " AND ".join(wheres)
query = f"SELECT * FROM {table} {where_str};"
self.cursor.execute(query)
joins = self.cursor.fetchall()
return [(
Reference(join["album_id"]),
Reference(join["artist_id"])
) for join in joins]
def get_artist_from_row(self, artist_row, exclude_relations: set = None, flat: bool = False) -> Artist:
if exclude_relations is None:
exclude_relations = set()
new_exclude_relations: set = set(exclude_relations)
new_exclude_relations.add(Song)
return Artist(
id_=artist_row['artist_id'],
new_exclude_relations.add(Artist)
artist_id = artist_row['artist_id']
artist_obj = Artist(
id_=artist_id,
name=artist_row['artist_name']
)
if flat:
return artist_obj
# fetch songs :D
for song_ref, _, is_feature in self.pull_artist_song(artist_ref=Reference(id_=artist_id)):
new_songs = self.pull_songs(song_ref=song_ref, exclude_relations=new_exclude_relations)
if len(new_songs) < 1:
continue
new_song = new_songs[0]
if is_feature:
artist_obj.feature_songs.append(new_song)
else:
artist_obj.main_songs.append(new_song)
# fetch albums
for album_ref, _ in self.pull_artist_album(artist_ref=Reference(id_=artist_id)):
new_albums = self.pull_albums(album_ref=album_ref, exclude_relations=new_exclude_relations)
if len(new_albums) < 1:
continue
artist_obj.main_albums.append(new_albums[0])
return artist_obj
def pull_artists(self, artist_ref: Reference = None, exclude_relations: set = None, flat: bool = False) -> List[Artist]:
"""
:param artist_ref:
:param exclude_relations:
:param flat: if it is true it ONLY fetches the artist data
:return:
"""
def pull_artists(self, artist_ref: Reference = None, exclude_relations: set = None) -> List[Artist]:
where = "1=1"
if artist_ref is not None:
where = f"Artist.id=\"{artist_ref.id}\""
@ -362,10 +450,10 @@ class Database:
artist_rows = self.cursor.fetchall()
return [(
self.get_artist_from_row(artist_row)
self.get_artist_from_row(artist_row, exclude_relations=exclude_relations, flat=flat)
) for artist_row in artist_rows]
def get_song_from_row(self, song_result, exclude_relations: set = set()) -> Song:
def get_song_from_row(self, song_result, exclude_relations: set = None) -> Song:
if exclude_relations is None:
exclude_relations = set()
new_exclude_relations: set = set(exclude_relations)
@ -396,19 +484,18 @@ class Database:
if len(album_obj) > 0:
song_obj.album = album_obj[0]
flat_artist = Artist in exclude_relations
main_artists = []
feature_artists = []
if Artist not in exclude_relations:
for song_ref, artist_ref, is_feature in self.pull_artist_song(song_ref=Reference(song_id)):
print(artist_ref)
if is_feature:
feature_artists.extend(self.pull_artists(artist_ref=artist_ref))
else:
main_artists.extend(self.pull_artists(artist_ref=artist_ref))
song_obj.main_artist_list = main_artists
song_obj.feature_artist_list = feature_artists
for song_ref, artist_ref, is_feature in self.pull_artist_song(song_ref=Reference(song_id)):
if is_feature:
feature_artists.extend(self.pull_artists(artist_ref=artist_ref, flat=flat_artist))
else:
main_artists.extend(self.pull_artists(artist_ref=artist_ref, flat=flat_artist))
song_obj.main_artist_list = main_artists
song_obj.feature_artist_list = feature_artists
return song_obj
@ -472,9 +559,17 @@ class Database:
)
album_obj.set_tracklist(tracklist=tracklist)
flat_artist = Artist in exclude_relations
for _, artist_ref in self.pull_artist_album(album_ref=Reference(id_=album_id)):
artists = self.pull_artists(artist_ref, flat=flat_artist, exclude_relations=new_exclude_relations)
if len(artists) < 1:
continue
album_obj.artists.append(artists[0])
return album_obj
def pull_albums(self, album_ref: Reference = None, song_ref: Reference = None, exclude_relations: set = None) -> List[Album]:
def pull_albums(self, album_ref: Reference = None, song_ref: Reference = None, exclude_relations: set = None) -> \
List[Album]:
"""
This function is used to get matching albums/releses
from one song id (a reference object)

View File

@ -17,7 +17,7 @@ All Objects dependent
class SongAttribute:
def __init__(self, song = None):
def __init__(self, song=None):
# the reference to the song the lyrics belong to
self.song = song
@ -150,7 +150,7 @@ class Song(DatabaseObject):
target: Target = None,
lyrics: List[Lyrics] = None,
metadata: dict = {},
album = None,
album=None,
main_artist_list: list = [],
feature_artist_list: list = []
) -> None:
@ -293,6 +293,7 @@ class Album(DatabaseObject):
self.albumsort: int | None = albumsort
self.tracklist: List[Song] = []
self.artists: List[Artist] = []
def __str__(self) -> str:
return f"Album: \"{self.title}\""
@ -321,94 +322,78 @@ All objects dependent on Artist
class Artist(DatabaseObject):
"""
main_songs
feature_song
albums
"""
def __init__(
self,
id_: str = None,
name: str = None,
discography: List[Album] = [],
features: List[Song] = []
main_songs: List[Song] = None,
feature_songs: List[Song] = None,
main_albums: List[Song] = None
):
DatabaseObject.__init__(self, id_=id_)
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 = []
self.feature_songs = []
self.main_songs = main_songs
self.feature_songs = feature_songs
self.main_albums = []
self.feature_albums = []
self.songs: List[Song] = []
self.album_refs: List[Album] = []
self.set_discography(discography)
self.set_features(features)
self.main_albums = main_albums
def __str__(self):
return self.name or ""
def add_song(self, song: Song, is_feature: bool):
"""
it is reccomendet, that the song object already contains the album,
else you will have to add it youreself
"""
def __repr__(self):
return self.__str__()
if is_feature:
self.feature_songs.append(song)
if song.album is not None:
self.feature_albums.append(song.album)
else:
self.main_songs.append(song)
if song.album is not None:
self.main_albums(song.album)
def add_album(self, album: Album):
self.album_refs.append(album)
for song in album.tracklist:
song.__class__ = ArtistSong
song.is_feature = False
self.songs.append(song)
def set_discography(self, discography: List[Album]):
"""
:param discography:
:return:
"""
for album in discography:
self.add_album(album)
def get_discography(self) -> List[Album]:
flat_copy_discography = self.discography.copy()
def get_features(self) -> Album:
feature_release = Album(
title="features",
copyright_=self.name,
album_status="dynamically generated",
album_status="dynamic",
is_split=True,
albumsort=666,
dynamic=True
)
for song in self.songs:
if song.is_feature:
feature_release.add_song(song)
for feature in self.feature_songs:
feature_release.add_song(feature)
return feature_release
def get_songs(self) -> Album:
song_release = Album(
title="song collection",
copyright_=self.name,
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_discography(self) -> List[Album]:
flat_copy_discography = self.discography.copy()
flat_copy_discography.append(self.get_features())
flat_copy_discography.append(feature_release)
return flat_copy_discography
def set_features(self, feature_tracks: List[Song]):
for song in feature_tracks:
song.__class__ = ArtistSong
song.is_feature = True
self.songs.append(song)
def get_features(self) -> List[ArtistSong]:
feature_releases = []
for song in self.songs:
if song.is_feature:
feature_releases.append(song)
return feature_releases
discography = property(fget=get_discography, fset=set_discography)
features = property(fget=get_features, fset=set_features)
discography: List[Album] = property(fget=get_discography)
features: Album = property(fget=get_features)
songs: Album = property(fget=get_songs)

Binary file not shown.