music-kraken-core/src/music_kraken/database/objects/song.py

477 lines
14 KiB
Python
Raw Normal View History

2022-12-06 13:45:18 +00:00
import os
from typing import List
2023-01-13 13:37:15 +00:00
import pycountry
import copy
2022-12-06 13:45:18 +00:00
2023-01-12 22:01:19 +00:00
from .metadata import (
Mapping as id3Mapping,
2023-01-30 13:41:02 +00:00
ID3Timestamp,
MetadataAttribute
2023-01-12 22:01:19 +00:00
)
2022-12-06 13:45:18 +00:00
from ...utils.shared import (
MUSIC_DIR,
DATABASE_LOGGER as logger
)
2023-01-12 15:25:50 +00:00
from .parents import (
2022-12-06 13:45:18 +00:00
DatabaseObject,
2023-01-12 15:25:50 +00:00
Reference,
SongAttribute
2022-12-06 13:45:18 +00:00
)
2023-01-20 22:05:15 +00:00
from .source import (
Source,
SourceTypes,
2023-01-27 11:56:59 +00:00
SourcePages,
SourceAttribute
2023-01-20 22:05:15 +00:00
)
2023-02-06 08:16:28 +00:00
from .formatted_text import FormattedText
2023-02-08 12:16:48 +00:00
from .collection import Collection
2022-12-06 13:45:18 +00:00
2022-12-07 14:51:38 +00:00
"""
All Objects dependent
"""
2022-12-08 08:28:28 +00:00
2022-12-06 22:44:42 +00:00
class Target(DatabaseObject, SongAttribute):
2022-12-06 13:45:18 +00:00
"""
create somehow like that
```python
2022-12-06 22:44:42 +00:00
# I know path is pointless, and I will change that (don't worry about backwards compatibility there)
2022-12-06 13:45:18 +00:00
Target(file="~/Music/genre/artist/album/song.mp3", path="~/Music/genre/artist/album")
```
"""
2022-12-06 22:44:42 +00:00
def __init__(self, id_: str = None, file: str = None, path: str = None) -> None:
DatabaseObject.__init__(self, id_=id_)
SongAttribute.__init__(self)
2022-12-06 13:45:18 +00:00
self._file = file
self._path = path
def set_file(self, _file: str):
self._file = _file
def get_file(self) -> str | None:
if self._file is None:
return None
return os.path.join(MUSIC_DIR, self._file)
def set_path(self, _path: str):
self._path = _path
def get_path(self) -> str | None:
if self._path is None:
return None
return os.path.join(MUSIC_DIR, self._path)
def get_exists_on_disc(self) -> bool:
"""
returns True when file can be found on disc
returns False when file can't be found on disc or no filepath is set
"""
if not self.is_set():
return False
return os.path.exists(self.file)
2022-12-06 22:44:42 +00:00
2022-12-06 13:45:18 +00:00
def is_set(self) -> bool:
return not (self._file is None or self._path is None)
file = property(fget=get_file, fset=set_file)
path = property(fget=get_path, fset=set_path)
exists_on_disc = property(fget=get_exists_on_disc)
2023-01-30 13:41:02 +00:00
class Lyrics(DatabaseObject, SongAttribute, SourceAttribute, MetadataAttribute):
2023-01-27 11:56:59 +00:00
def __init__(
self,
text: str,
language: str,
2023-01-27 11:56:59 +00:00
id_: str = None,
source_list: List[Source] = None
) -> None:
2022-12-06 22:44:42 +00:00
DatabaseObject.__init__(self, id_=id_)
SongAttribute.__init__(self)
2022-12-06 13:45:18 +00:00
self.text = text
self.language = language
2023-01-27 11:56:59 +00:00
if source_list is not None:
self.source_list = source_list
2022-12-06 13:45:18 +00:00
2023-01-30 13:41:02 +00:00
def get_metadata(self) -> MetadataAttribute.Metadata:
return super().get_metadata()
2023-01-27 11:56:59 +00:00
2023-01-30 13:41:02 +00:00
class Song(DatabaseObject, SourceAttribute, MetadataAttribute):
2022-12-06 13:45:18 +00:00
def __init__(
2022-12-06 22:44:42 +00:00
self,
id_: str = None,
mb_id: str = None,
title: str = None,
2022-12-07 14:51:38 +00:00
album_name: str = None,
2022-12-06 22:44:42 +00:00
isrc: str = None,
length: int = None,
2022-12-09 17:24:58 +00:00
tracksort: int = None,
2023-01-16 13:31:43 +00:00
genre: str = None,
2023-01-27 11:56:59 +00:00
source_list: List[Source] = None,
2022-12-06 22:44:42 +00:00
target: Target = None,
2023-02-10 12:52:18 +00:00
lyrics_list: List[Lyrics] = None,
2022-12-16 15:59:21 +00:00
album=None,
2023-01-16 13:23:33 +00:00
main_artist_list: list = None,
2023-02-10 12:52:18 +00:00
feature_artist_list: list = None,
**kwargs
2022-12-06 22:44:42 +00:00
) -> None:
2022-12-06 13:45:18 +00:00
"""
id: is not NECESARRILY the musicbrainz id, but is DISTINCT for every song
mb_id: is the musicbrainz_id
target: Each Song can have exactly one target which can be either full or empty
lyrics: There can be multiple lyrics. Each Lyrics object can me added to multiple lyrics
"""
2023-02-10 12:52:18 +00:00
super().__init__(id_=id_, **kwargs)
2022-12-06 13:45:18 +00:00
# attributes
2023-01-10 18:21:11 +00:00
# *private* attributes
2023-01-16 13:23:33 +00:00
self.title: str = title
self.isrc: str = isrc
self.length: int = length
2022-12-06 13:45:18 +00:00
self.mb_id: str | None = mb_id
2023-02-10 12:52:18 +00:00
self.tracksort: int = tracksort or 0
2023-01-16 13:31:43 +00:00
self.genre: str = genre
2023-02-10 12:52:18 +00:00
self.source_list = source_list or []
2022-12-06 13:45:18 +00:00
2023-02-10 12:52:18 +00:00
self.target = target or Target()
self.lyrics_list = lyrics_list or []
2022-12-06 13:45:18 +00:00
2023-02-10 12:52:18 +00:00
# initialize with either a passed in album, or an empty one,
# so it can at least properly generate dynamic attributes
self._album = album or Album(empty=True)
2023-01-12 23:32:35 +00:00
self.album = album
2022-12-06 13:45:18 +00:00
2023-02-10 12:52:18 +00:00
self.main_artist_collection = Collection(
data=main_artist_list or [],
map_attributes=["title"],
element_type=Artist
)
self.feature_artist_collection = Collection(
data=feature_artist_list or [],
map_attributes=["title"],
element_type=Artist
)
2022-12-09 14:50:44 +00:00
2022-12-08 08:28:28 +00:00
def __eq__(self, other):
if type(other) != type(self):
return False
return self.id == other.id
2022-12-13 10:16:19 +00:00
def get_artist_credits(self) -> str:
2023-02-10 12:52:18 +00:00
main_artists = ", ".join([artist.name for artist in self.main_artist_collection])
feature_artists = ", ".join([artist.name for artist in self.feature_artist_collection])
if len(feature_artists) == 0:
return main_artists
return f"{main_artists} feat. {feature_artists}"
2022-12-13 10:16:19 +00:00
2022-12-06 13:45:18 +00:00
def __str__(self) -> str:
2022-12-13 10:16:19 +00:00
artist_credit_str = ""
artist_credits = self.get_artist_credits()
if artist_credits != "":
artist_credit_str = f" by {artist_credits}"
return f"\"{self.title}\"{artist_credit_str}"
2022-12-06 13:45:18 +00:00
def __repr__(self) -> str:
2023-02-10 12:52:18 +00:00
return f"Song(\"{self.title}\")"
2022-12-09 17:24:58 +00:00
2023-01-30 14:12:30 +00:00
def get_tracksort_str(self):
2023-02-10 12:52:18 +00:00
"""
if the album tracklist is empty, it sets it length to 1, this song has to be in the Album
:returns id3_tracksort: {song_position}/{album.length_of_tracklist}
"""
return f"{self.tracksort}/{len(self.album.tracklist) or 1}"
2023-01-30 14:12:30 +00:00
2023-01-30 13:41:02 +00:00
def get_metadata(self) -> MetadataAttribute.Metadata:
metadata = MetadataAttribute.Metadata({
id3Mapping.TITLE: [self.title],
id3Mapping.ISRC: [self.isrc],
2023-01-30 22:54:21 +00:00
id3Mapping.LENGTH: [self.length],
id3Mapping.GENRE: [self.genre],
id3Mapping.TRACKNUMBER: [self.tracksort_str]
2023-01-30 13:41:02 +00:00
})
2023-01-16 13:23:33 +00:00
2023-01-30 13:41:02 +00:00
metadata.merge_many([s.get_song_metadata() for s in self.source_list])
2023-02-10 12:52:18 +00:00
if not self.album.empty:
2023-01-30 13:41:02 +00:00
metadata.merge(self.album.metadata)
metadata.merge_many([a.metadata for a in self.main_artist_list])
metadata.merge_many([a.metadata for a in self.feature_artist_list])
metadata.merge_many([l.metadata for l in self.lyrics])
2023-01-16 13:23:33 +00:00
return metadata
2023-01-30 14:12:30 +00:00
2023-02-09 08:40:57 +00:00
def get_options(self) -> list:
options = self.main_artist_list.copy()
options.extend(self.feature_artist_list.copy())
2023-02-10 12:52:18 +00:00
if not self.album.empty:
2023-02-09 08:40:57 +00:00
options.append(self.album)
options.append(self)
return options
def get_option_string(self) -> str:
2023-02-10 12:52:18 +00:00
return f"Song({self.title}) of Album({self.album.title}) from Artists({self.get_artist_credits()})"
2023-02-09 08:40:57 +00:00
2023-01-30 14:12:30 +00:00
tracksort_str = property(fget=get_tracksort_str)
2023-02-10 12:52:18 +00:00
main_artist_list: list = property(fget=lambda self: self.main_artist_collection.copy())
feature_artist_list: list = property(fget=lambda self: self.feature_artist_collection.copy())
2023-01-11 01:25:17 +00:00
2022-12-06 13:45:18 +00:00
2022-12-07 14:51:38 +00:00
"""
2022-12-09 17:24:58 +00:00
All objects dependent on Album
2022-12-07 14:51:38 +00:00
"""
2022-12-08 08:28:28 +00:00
2023-01-30 13:41:02 +00:00
class Album(DatabaseObject, SourceAttribute, MetadataAttribute):
2022-12-07 14:51:38 +00:00
def __init__(
2022-12-08 08:28:28 +00:00
self,
id_: str = None,
title: str = None,
2023-01-13 13:37:15 +00:00
label: str = None,
2022-12-08 08:28:28 +00:00
album_status: str = None,
2023-01-13 13:37:15 +00:00
language: pycountry.Languages = None,
2023-01-14 13:41:40 +00:00
date: ID3Timestamp = None,
2022-12-08 08:28:28 +00:00
country: str = None,
barcode: str = None,
2022-12-12 18:30:18 +00:00
is_split: bool = False,
albumsort: int = None,
2023-01-23 12:49:07 +00:00
dynamic: bool = False,
2023-01-27 11:56:59 +00:00
source_list: List[Source] = None,
2023-02-10 12:52:18 +00:00
artist_list: list = None,
2023-02-08 16:14:51 +00:00
tracklist: List[Song] = None,
2023-02-10 12:52:18 +00:00
album_type: str = None,
**kwargs
2022-12-07 14:51:38 +00:00
) -> None:
2023-02-10 12:52:18 +00:00
DatabaseObject.__init__(self, id_=id_, dynamic=dynamic, **kwargs)
2023-02-08 16:14:51 +00:00
"""
TODO
add to db
"""
self.album_type = album_type
2022-12-07 14:51:38 +00:00
self.title: str = title
self.album_status: str = album_status
2023-01-13 13:37:15 +00:00
self.label = label
self.language: pycountry.Languages = language
2023-02-10 12:52:18 +00:00
self.date: ID3Timestamp = date or ID3Timestamp()
2022-12-07 14:51:38 +00:00
self.country: str = country
2023-01-13 11:05:44 +00:00
"""
TODO
find out the id3 tag for barcode and implement it
2023-01-14 13:41:40 +00:00
maybee look at how mutagen does it with easy_id3
2023-01-13 11:05:44 +00:00
"""
2022-12-07 14:51:38 +00:00
self.barcode: str = barcode
2022-12-12 18:30:18 +00:00
self.is_split: bool = is_split
2023-02-10 12:52:18 +00:00
"""
TODO
implement a function in the Artist class,
to set albumsort with help of the release year
"""
2022-12-12 18:30:18 +00:00
self.albumsort: int | None = albumsort
2022-12-07 14:51:38 +00:00
2023-02-08 12:16:48 +00:00
self._tracklist = Collection(
data=tracklist or [],
map_attributes=["title"],
element_type=Song
)
self.source_list = source_list or []
2023-02-10 12:52:18 +00:00
self.artists = Collection(
data=artist_list or [],
map_attributes=["name"],
element_type=Artist
)
2023-01-23 12:49:07 +00:00
2023-01-24 13:29:23 +00:00
2022-12-09 17:24:58 +00:00
def __str__(self) -> str:
2023-02-08 12:29:01 +00:00
return f"-----{self.title}-----\n{self.tracklist}"
2022-12-09 17:24:58 +00:00
2022-12-16 17:26:05 +00:00
def __repr__(self):
2023-02-08 12:29:01 +00:00
return f"Album(\"{self.title}\")"
2022-12-16 17:26:05 +00:00
2022-12-09 17:24:58 +00:00
def __len__(self) -> int:
return len(self.tracklist)
2023-02-08 12:16:48 +00:00
def set_tracklist(self, tracklist):
tracklist_list = []
if type(tracklist) == Collection:
tracklist_list = tracklist_list
elif type(tracklist) == list:
tracklist_list = tracklist
2022-12-07 14:51:38 +00:00
2023-02-08 12:16:48 +00:00
self._tracklist = Collection(
data=tracklist_list,
map_attributes=["title"],
element_type=Song
)
2022-12-12 18:30:18 +00:00
2023-01-30 13:41:02 +00:00
def get_metadata(self) -> MetadataAttribute.Metadata:
return MetadataAttribute.Metadata({
id3Mapping.ALBUM: [self.title],
id3Mapping.COPYRIGHT: [self.copyright],
id3Mapping.LANGUAGE: [self.iso_639_2_language],
id3Mapping.ALBUM_ARTIST: [a.name for a in self.artists],
id3Mapping.DATE: [self.date.timestamp]
2023-01-30 13:41:02 +00:00
})
2023-01-13 11:05:44 +00:00
2023-01-13 13:37:15 +00:00
def get_copyright(self) -> str:
2023-01-30 22:54:21 +00:00
if self.date is None:
return None
2023-01-13 13:37:15 +00:00
if self.date.year == 1 or self.label is None:
return None
return f"{self.date.year} {self.label}"
def get_iso_639_2_lang(self) -> str:
if self.language is None:
return None
return self.language.alpha_3
2023-02-09 08:40:57 +00:00
def get_options(self) -> list:
options = self.artists.copy()
options.append(self)
for track in self.tracklist:
new_track: Song = copy.copy(track)
new_track.album = self
options.append(new_track)
return options
def get_option_string(self) -> str:
return f"Album: {self.title}; Artists {', '.join([i.name for i in self.artists])}"
2023-02-08 12:16:48 +00:00
2023-01-13 13:37:15 +00:00
copyright = property(fget=get_copyright)
iso_639_2_language = property(fget=get_iso_639_2_lang)
2023-02-08 12:16:48 +00:00
tracklist: Collection = property(fget=lambda self: self._tracklist, fset=set_tracklist)
2023-01-13 13:37:15 +00:00
2023-01-13 11:05:44 +00:00
2022-12-12 18:30:18 +00:00
"""
All objects dependent on Artist
"""
2023-01-30 13:41:02 +00:00
class Artist(DatabaseObject, SourceAttribute, MetadataAttribute):
2022-12-16 15:59:21 +00:00
"""
main_songs
feature_song
albums
"""
2022-12-12 18:30:18 +00:00
def __init__(
self,
id_: str = None,
name: str = None,
2023-01-27 11:56:59 +00:00
source_list: List[Source] = None,
2022-12-16 15:59:21 +00:00
feature_songs: List[Song] = None,
2023-01-24 08:40:01 +00:00
main_albums: List[Album] = None,
2023-02-06 08:16:28 +00:00
notes: FormattedText = None,
2023-02-01 13:26:54 +00:00
lyrical_themes: List[str] = None,
general_genre: str = "",
country=None,
2023-02-01 13:26:54 +00:00
formed_in: ID3Timestamp = None
2022-12-12 18:30:18 +00:00
):
DatabaseObject.__init__(self, id_=id_)
"""
TODO implement album type and notes
"""
2023-02-01 13:26:54 +00:00
self.country: pycountry.Country = country
self.formed_in: ID3Timestamp = formed_in
2023-02-01 13:26:54 +00:00
"""
notes, generall genre, lyrics themes are attributes
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
"""
2023-02-09 14:49:22 +00:00
self.notes: FormattedText = notes or FormattedText()
self.lyrical_themes: List[str] = lyrical_themes or []
2023-02-01 13:26:54 +00:00
self.general_genre = general_genre
2023-01-24 08:40:01 +00:00
2023-02-09 14:49:22 +00:00
self.name: str = name
2022-12-12 18:30:18 +00:00
2023-02-08 16:14:51 +00:00
self.feature_songs = Collection(
data=feature_songs,
map_attributes=["title"],
element_type=Song
)
self.main_albums = Collection(
data=main_albums,
map_attributes=["title"],
element_type=Album
)
2022-12-12 18:30:18 +00:00
2023-01-27 11:56:59 +00:00
if source_list is not None:
self.source_list = source_list
2023-01-20 10:43:35 +00:00
2022-12-12 18:30:18 +00:00
def __str__(self):
2023-02-06 08:16:28 +00:00
string = self.name or ""
plaintext_notes = self.notes.get_plaintext()
if plaintext_notes is not None:
string += "\n" + plaintext_notes
return string
2022-12-12 18:30:18 +00:00
2022-12-16 15:59:21 +00:00
def __repr__(self):
return self.__str__()
2022-12-12 18:30:18 +00:00
def __eq__(self, __o: object) -> bool:
return self.id_ == __o.id_
2022-12-16 15:59:21 +00:00
def get_features(self) -> Album:
2022-12-12 18:30:18 +00:00
feature_release = Album(
title="features",
2022-12-16 15:59:21 +00:00
album_status="dynamic",
2022-12-12 18:30:18 +00:00
is_split=True,
albumsort=666,
dynamic=True
)
2022-12-16 15:59:21 +00:00
for feature in self.feature_songs:
feature_release.add_song(feature)
2022-12-12 18:30:18 +00:00
2022-12-16 15:59:21 +00:00
return feature_release
2022-12-12 18:30:18 +00:00
2023-02-09 14:49:22 +00:00
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
2022-12-12 18:30:18 +00:00
2022-12-16 15:59:21 +00:00
def get_discography(self) -> List[Album]:
2022-12-16 17:26:05 +00:00
flat_copy_discography = self.main_albums.copy()
2022-12-16 15:59:21 +00:00
flat_copy_discography.append(self.get_features())
2022-12-12 18:30:18 +00:00
2022-12-16 15:59:21 +00:00
return flat_copy_discography
2022-12-12 18:30:18 +00:00
2023-01-30 13:41:02 +00:00
def get_metadata(self) -> MetadataAttribute.Metadata:
metadata = MetadataAttribute.Metadata({
id3Mapping.ARTIST: [self.name]
2023-01-30 13:41:02 +00:00
})
metadata.merge_many([s.get_artist_metadata() for s in self.source_list])
2023-01-20 10:43:35 +00:00
2023-01-30 13:41:02 +00:00
return metadata
2023-01-16 13:23:33 +00:00
2023-02-09 08:40:57 +00:00
def get_options(self) -> list:
options = [self]
2023-02-09 14:49:22 +00:00
options.extend(self.main_albums)
options.extend(self.feature_songs)
2023-02-09 08:40:57 +00:00
return options
def get_option_string(self) -> str:
return f"Artist: {self.name}"
2022-12-16 15:59:21 +00:00
discography: List[Album] = property(fget=get_discography)
features: Album = property(fget=get_features)
2023-02-09 14:49:22 +00:00
all_songs: Album = property(fget=get_all_songs)