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

88 lines
2.3 KiB
Python
Raw Normal View History

2022-11-22 08:21:28 +00:00
from typing import List
from ..utils.shared import *
2022-11-22 08:21:28 +00:00
import os
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-21 14:40:41 +00:00
class Artist:
def __init__(self, artist_data) -> None:
self.artist_data
self.id = self.artist_data['id']
self.name = self.artist_data['name']
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-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))
# initialize the target
self.target = Target()
self.target.file = self.json_data['file']
self.target.path = self.json_data['path']
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-21 14:40:41 +00:00
return [a.name for a in self.aritsts]
def __getitem__(self, item):
print(item)
print(self.json_data)
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
print(item, value)
self.json_data[item] = value