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

125 lines
3.1 KiB
Python
Raw Normal View History

2022-11-22 08:21:28 +00:00
from typing import List
2022-11-22 13:25:01 +00:00
from ..utils.shared import (
MUSIC_DIR
)
2022-11-22 08:21:28 +00:00
import os
from mutagen.easyid3 import EasyID3
class Target:
def __init__(self) -> None:
self._file = None
self._path = None
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)
file = property(fget=get_file, fset=set_file)
path = property(fget=get_path, fset=set_path)
2022-11-22 08:21:28 +00:00
2022-11-22 17:25:35 +00:00
2022-11-21 14:40:41 +00:00
class Artist:
def __init__(self, artist_data) -> None:
2022-11-22 13:25:01 +00:00
self.artist_data = artist_data
2022-11-21 14:40:41 +00:00
self.id = self.artist_data['id']
self.name = self.artist_data['name']
def __str__(self) -> str:
return self.name
2022-11-22 17:25:35 +00:00
2022-11-21 14:44:59 +00:00
class Source:
def __init__(self, src_data) -> None:
self.src_data = src_data
self.src = self.src_data['src']
self.url = self.src_data['url']
2022-11-22 17:25:35 +00:00
class Metadata:
def __init__(self) -> None:
self.data = {}
def get_all_metadata(self):
2022-11-22 17:25:35 +00:00
return list(self.data.items())
def __setitem__(self, item, value):
if item in EasyID3.valid_keys.keys():
self.data[item] = value
def __getitem__(self, item):
if item not in self.data:
return None
return self.data[item]
2022-11-21 14:40:41 +00:00
class Song:
def __init__(self, json_response) -> None:
self.json_data = json_response
# initialize the data
2022-11-21 14:54:13 +00:00
self.title = self.json_data['title']
2022-11-21 14:40:41 +00:00
self.artists = [Artist(a) for a in self.json_data['artists']]
self.isrc = self.json_data['isrc']
2022-11-21 14:54:13 +00:00
# initialize the sources
2022-11-22 08:21:28 +00:00
self.sources: List[Source] = []
2022-11-21 14:44:59 +00:00
for src in self.json_data['source']:
if src['src'] is None:
continue
self.sources.append(Source(src))
2022-11-22 17:25:35 +00:00
# initialize the target
self.target = Target()
self.target.file = self.json_data['file']
self.target.path = self.json_data['path']
# initialize id3 metadata
self.metadata = Metadata()
2022-11-22 17:25:35 +00:00
for key, value in self.json_data.items():
self.metadata[key] = value
self.metadata['artist'] = self.get_artist_names()
# EasyID3.valid_keys.keys()
def __str__(self) -> str:
return f"\"{self.title}\" by {', '.join([str(a) for a in self.artists])}"
2022-11-22 17:25:35 +00:00
def get_metadata(self):
return self.metadata.get_all_metadata()
def has_isrc(self) -> bool:
return self.isrc is not None
2022-11-21 14:40:41 +00:00
def get_artist_names(self) -> List[str]:
2022-11-22 13:25:01 +00:00
return [a.name for a in self.artists]
2022-11-21 14:40:41 +00:00
def __getitem__(self, item):
if item not in self.json_data:
return None
return self.json_data[item]
def __setitem__(self, item, value):
if item == "file":
self.target.file = value
return
if item == "path":
self.target.path = value
return
2022-11-21 14:40:41 +00:00
self.json_data[item] = value