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

402 lines
11 KiB
Python
Raw Normal View History

2022-12-06 13:45:18 +00:00
import os
2023-01-10 17:52:47 +00:00
from typing import List, Tuple, Dict
2022-12-06 13:45:18 +00:00
from mutagen.easyid3 import EasyID3
2023-01-12 22:01:19 +00:00
from .metadata import (
Mapping as ID3_MAPPING,
Metadata
)
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,
2023-01-12 16:14:21 +00:00
SongAttribute,
ID3Metadata
2022-12-06 13:45:18 +00:00
)
2023-01-12 15:25:50 +00:00
from .source import Source
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)
2022-12-06 22:44:42 +00:00
class Lyrics(DatabaseObject, SongAttribute):
2022-12-06 13:45:18 +00:00
def __init__(self, text: str, language: str, id_: str = 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
class Song(DatabaseObject):
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
artist_names: List[str] = [],
isrc: str = None,
length: int = None,
2022-12-09 17:24:58 +00:00
tracksort: int = None,
2022-12-06 22:44:42 +00:00
sources: List[Source] = None,
target: Target = None,
lyrics: List[Lyrics] = None,
2022-12-16 15:59:21 +00:00
album=None,
2022-12-13 10:16:19 +00:00
main_artist_list: list = [],
feature_artist_list: list = []
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
"""
super().__init__(id_=id_)
# attributes
2023-01-10 18:21:11 +00:00
# *private* attributes
2023-01-10 17:52:47 +00:00
self._title = None
2023-01-10 18:21:11 +00:00
self._isrc = None
self._length = None
2023-01-11 01:25:17 +00:00
self._sources: List[Source] = []
2023-01-10 18:21:11 +00:00
self.metadata = Metadata()
self.title = title
self.isrc = isrc
self.length = length
2023-01-10 17:52:47 +00:00
2022-12-06 13:45:18 +00:00
self.mb_id: str | None = mb_id
2022-12-07 14:51:38 +00:00
self.album_name: str | None = album_name
2022-12-06 13:45:18 +00:00
self.artist_names = artist_names
2022-12-09 17:24:58 +00:00
self.tracksort: int | None = tracksort
2022-12-06 13:45:18 +00:00
2023-01-11 01:25:17 +00:00
if sources is not None:
2023-01-11 14:28:22 +00:00
self.set_sources(source_list=sources)
2022-12-06 22:44:42 +00:00
2022-12-06 13:45:18 +00:00
if target is None:
target = Target()
self.target: Target = target
2022-12-13 10:16:19 +00:00
self.target.add_song(self)
2022-12-06 13:45:18 +00:00
if lyrics is None:
lyrics = []
self.lyrics: List[Lyrics] = lyrics
2022-12-06 22:44:42 +00:00
for lyrics_ in self.lyrics:
2022-12-13 10:16:19 +00:00
lyrics_.add_song(self)
2022-12-06 13:45:18 +00:00
2022-12-13 10:16:19 +00:00
self.album: Album = album
2022-12-06 13:45:18 +00:00
2022-12-13 10:16:19 +00:00
self.main_artist_list = main_artist_list
self.feature_artist_list = feature_artist_list
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:
feature_str = ""
if len(self.feature_artist_list) > 0:
feature_str = " feat. " + ", ".join([artist.name for artist in self.feature_artist_list])
return ", ".join([artist.name for artist in self.main_artist_list]) + feature_str
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:
return self.__str__()
2023-01-10 17:52:47 +00:00
def set_simple_metadata(self, name: str, value):
"""
this method is for setting values of attributes,
that directly map to an ID3 value.
A good example is the title or the isrc.
for more complex data I will use seperate functions
the naming convention for the name I follow is, to name
the attribute the same as the defined property, but with one underscore infront:
title -> _title
"""
2023-01-10 18:21:11 +00:00
if value is None:
return
2023-01-10 17:52:47 +00:00
attribute_map = {
2023-01-10 18:21:11 +00:00
"_title": ID3_MAPPING.TITLE,
"_isrc": ID3_MAPPING.ISRC,
"_length": ID3_MAPPING.LENGTH
2023-01-10 17:52:47 +00:00
}
2023-01-10 18:21:11 +00:00
# if this crashes/raises an error the function is
# called wrongly. I DO NOT CATCH ERRORS DUE TO PERFORMANCE AND DEBUGGING
2023-01-10 17:52:47 +00:00
self.__setattr__(name, value)
2023-01-10 18:21:11 +00:00
# convert value to id3 value if necessary
id3_value = value
if type(value) == int:
id3_value = str(value)
2023-01-11 14:28:22 +00:00
self.metadata[attribute_map[name].value] = [id3_value]
2023-01-10 17:52:47 +00:00
2023-01-11 01:25:17 +00:00
def add_source(self, source_obj: Source):
source_obj.add_song(self)
print(source_obj)
self._sources.append(source_obj)
self.metadata[ID3_MAPPING.FILE_WEBPAGE_URL.value] = source_obj.url
def set_sources(self, source_list):
2023-01-11 14:28:22 +00:00
self._sources = source_list
for source in self._sources:
source.add_song(self)
2023-01-12 22:01:19 +00:00
2023-01-12 16:14:21 +00:00
# self.metadata[ID3_MAPPING.FILE_WEBPAGE_URL.value] = [s.url for s in self._sources]
self.metadata.add_many_id3_metadata_obj(self._sources)
2023-01-11 01:25:17 +00:00
2022-12-06 13:45:18 +00:00
def get_metadata(self):
return self.metadata.get_all_metadata()
def has_isrc(self) -> bool:
2023-01-10 18:21:11 +00:00
return self._isrc is not None
2022-12-06 13:45:18 +00:00
2023-01-11 01:25:17 +00:00
def add_source(self, source: Source):
pass
2022-12-06 13:45:18 +00:00
def get_artist_names(self) -> List[str]:
2022-12-06 22:44:42 +00:00
return self.artist_names
def get_length(self):
2023-01-10 18:21:11 +00:00
if self._length is None:
2022-12-06 22:44:42 +00:00
return None
2023-01-10 18:21:11 +00:00
return int(self._length)
2022-12-06 22:44:42 +00:00
2022-12-13 10:16:19 +00:00
def get_album_id(self):
if self.album is None:
2022-12-07 14:51:38 +00:00
return None
2022-12-13 10:16:19 +00:00
return self.album.id
2022-12-09 17:24:58 +00:00
2023-01-10 18:21:11 +00:00
title: str = property(fget=lambda self: self._title,
fset=lambda self, value: self.set_simple_metadata("_title", value))
isrc: str = property(fget=lambda self: self._isrc,
fset=lambda self, value: self.set_simple_metadata("_isrc", value))
length: int = property(fget=get_length, fset=lambda self, value: self.set_simple_metadata("_length", value))
2022-12-13 10:16:19 +00:00
album_id: str = property(fget=get_album_id)
2022-12-06 22:44:42 +00:00
2023-01-11 01:25:17 +00:00
sources: List[Source] = property(fget=lambda self: self._sources, fset=set_sources)
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
2022-12-07 14:51:38 +00:00
class Album(DatabaseObject):
"""
-------DB-FIELDS-------
title TEXT,
copyright TEXT,
album_status TEXT,
language TEXT,
year TEXT,
date TEXT,
country TEXT,
barcode TEXT,
song_id BIGINT,
2022-12-12 18:30:18 +00:00
is_split BOOLEAN NOT NULL DEFAULT 0
2022-12-07 14:51:38 +00:00
"""
2022-12-08 08:28:28 +00:00
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,
copyright_: str = None,
album_status: str = None,
language: str = None,
year: str = None,
date: str = None,
country: str = None,
barcode: str = None,
2022-12-12 18:30:18 +00:00
is_split: bool = False,
albumsort: int = None,
dynamic: bool = False
2022-12-07 14:51:38 +00:00
) -> None:
2022-12-12 18:30:18 +00:00
DatabaseObject.__init__(self, id_=id_, dynamic=dynamic)
2022-12-07 14:51:38 +00:00
self.title: str = title
self.copyright: str = copyright_
self.album_status: str = album_status
self.language: str = language
self.year: str = year
self.date: str = date
self.country: str = country
self.barcode: str = barcode
2022-12-12 18:30:18 +00:00
self.is_split: bool = is_split
self.albumsort: int | None = albumsort
2022-12-07 14:51:38 +00:00
2022-12-08 08:28:28 +00:00
self.tracklist: List[Song] = []
2022-12-16 15:59:21 +00:00
self.artists: List[Artist] = []
2022-12-07 14:51:38 +00:00
2022-12-09 17:24:58 +00:00
def __str__(self) -> str:
return f"Album: \"{self.title}\""
2022-12-16 17:26:05 +00:00
def __repr__(self):
return self.__str__()
2022-12-09 17:24:58 +00:00
def __len__(self) -> int:
return len(self.tracklist)
2022-12-08 08:28:28 +00:00
def set_tracklist(self, tracklist: List[Song]):
self.tracklist = tracklist
2022-12-07 14:51:38 +00:00
2022-12-09 17:24:58 +00:00
for i, track in enumerate(self.tracklist):
2022-12-12 18:30:18 +00:00
track.tracksort = i + 1
2022-12-09 17:24:58 +00:00
2022-12-08 08:28:28 +00:00
def add_song(self, song: Song):
for existing_song in self.tracklist:
if existing_song == song:
return
2022-12-09 17:24:58 +00:00
song.tracksort = len(self.tracklist)
2022-12-08 08:28:28 +00:00
self.tracklist.append(song)
2022-12-12 18:30:18 +00:00
"""
All objects dependent on Artist
"""
class Artist(DatabaseObject):
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,
2022-12-16 15:59:21 +00:00
main_songs: List[Song] = None,
feature_songs: List[Song] = None,
2022-12-16 17:26:05 +00:00
main_albums: List[Album] = None
2022-12-12 18:30:18 +00:00
):
DatabaseObject.__init__(self, id_=id_)
2022-12-16 15:59:21 +00:00
if main_albums is None:
main_albums = []
if feature_songs is None:
feature_songs = []
if main_songs is None:
main_songs = []
2022-12-16 11:53:05 +00:00
2022-12-16 15:59:21 +00:00
self.name: str | None = name
2022-12-16 11:53:05 +00:00
2022-12-16 15:59:21 +00:00
self.main_songs = main_songs
self.feature_songs = feature_songs
2022-12-12 18:30:18 +00:00
2022-12-16 15:59:21 +00:00
self.main_albums = main_albums
2022-12-12 18:30:18 +00:00
def __str__(self):
return self.name or ""
2022-12-16 15:59:21 +00:00
def __repr__(self):
return self.__str__()
2022-12-12 18:30:18 +00:00
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",
copyright_=self.name,
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
2022-12-16 15:59:21 +00:00
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
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
2022-12-16 15:59:21 +00:00
discography: List[Album] = property(fget=get_discography)
features: Album = property(fget=get_features)
songs: Album = property(fget=get_songs)