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
|
|
|
|
2022-11-22 11:41:05 +00:00
|
|
|
import os
|
2022-11-22 14:55:34 +00:00
|
|
|
from mutagen.easyid3 import EasyID3
|
|
|
|
|
2022-11-22 11:41:05 +00:00
|
|
|
|
|
|
|
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']
|
|
|
|
|
2022-11-23 10:33:24 +00:00
|
|
|
def __eq__(self, __o: object) -> bool:
|
|
|
|
if type(__o) != type(self):
|
|
|
|
return False
|
|
|
|
return self.id == __o.id
|
|
|
|
|
2022-11-22 12:19:54 +00:00
|
|
|
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
|
|
|
|
2022-11-22 14:55:34 +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())
|
2022-11-22 14:55:34 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-11-22 11:41:05 +00:00
|
|
|
# initialize the data
|
2022-11-21 14:54:13 +00:00
|
|
|
self.title = self.json_data['title']
|
2022-11-23 10:33:24 +00:00
|
|
|
self.artists = []
|
|
|
|
for a in self.json_data['artists']:
|
|
|
|
new_artist = Artist(a)
|
|
|
|
exists = False
|
|
|
|
for existing_artist in self.artists:
|
|
|
|
if new_artist == existing_artist:
|
|
|
|
exists = True
|
|
|
|
break
|
|
|
|
if not exists:
|
|
|
|
self.artists.append(new_artist)
|
2022-11-22 11:41:05 +00:00
|
|
|
self.isrc = self.json_data['isrc']
|
2022-11-21 14:54:13 +00:00
|
|
|
|
2022-11-22 11:41:05 +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
|
|
|
|
2022-11-22 11:41:05 +00:00
|
|
|
# initialize the target
|
|
|
|
self.target = Target()
|
|
|
|
self.target.file = self.json_data['file']
|
|
|
|
self.target.path = self.json_data['path']
|
|
|
|
|
2022-11-22 14:55:34 +00:00
|
|
|
# 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()
|
2022-11-22 14:55:34 +00:00
|
|
|
# EasyID3.valid_keys.keys()
|
|
|
|
|
2022-11-22 12:19:54 +00:00
|
|
|
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()
|
|
|
|
|
2022-11-22 11:41:05 +00:00
|
|
|
def has_isrc(self) -> bool:
|
|
|
|
return self.isrc is not None
|
2022-11-21 14:40:41 +00:00
|
|
|
|
2022-11-22 11:41:05 +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):
|
2022-11-22 11:46:45 +00:00
|
|
|
if item == "file":
|
|
|
|
self.target.file = value
|
|
|
|
return
|
|
|
|
if item == "path":
|
|
|
|
self.target.path = value
|
|
|
|
return
|
2022-11-22 11:48:01 +00:00
|
|
|
|
2022-11-21 14:40:41 +00:00
|
|
|
self.json_data[item] = value
|