diff --git a/src/music_kraken/pages/abstract.py b/src/music_kraken/pages/abstract.py index e886a7f..dd7306c 100644 --- a/src/music_kraken/pages/abstract.py +++ b/src/music_kraken/pages/abstract.py @@ -1,18 +1,18 @@ -import random -from typing import Optional, Union, Type, Dict, List, Set, Tuple -from bs4 import BeautifulSoup -import requests import logging -from dataclasses import dataclass +import random from copy import copy +from typing import Optional, Union, Type, Dict, Set -from ..utils import shared +import requests +from bs4 import BeautifulSoup + +from .support_classes.default_target import DefaultTarget +from .support_classes.download_result import DownloadResult from ..objects import ( Song, Source, Album, Artist, - Lyrics, Target, DatabaseObject, Options, @@ -22,87 +22,7 @@ from ..objects import ( AlbumType ) from ..tagging import write_metadata_to_target -from ..utils.shared import DOWNLOAD_PATH, DOWNLOAD_FILE, DEFAULT_VALUES -from ..utils.string_processing import fit_to_file_system - -LOGGER = logging.getLogger("this shouldn't be used") - - -@dataclass -class DefaultTarget: - genre: str = DEFAULT_VALUES["genre"] - label: str = DEFAULT_VALUES["label"] - artist: str = DEFAULT_VALUES["artist"] - album: str = DEFAULT_VALUES["album"] - album_type: str = DEFAULT_VALUES["album_type"] - song: str = DEFAULT_VALUES["song"] - - def __setattr__(self, __name: str, __value: str) -> None: - if __name in DEFAULT_VALUES: - if self.__getattribute__(__name) == DEFAULT_VALUES[__name]: - super().__setattr__(__name, fit_to_file_system(__value)) - return - - super().__setattr__(__name, __value) - - @property - def target(self) -> Target: - return Target( - relative_to_music_dir=True, - path=DOWNLOAD_PATH.format(genre=self.genre, label=self.label, artist=self.artist, album=self.album, - song=self.song, album_type=self.album_type), - file=DOWNLOAD_FILE.format(genre=self.genre, label=self.label, artist=self.artist, album=self.album, - song=self.song, album_type=self.album_type) - ) - - def song_object(self, song: Song): - self.song = song.title - - if not song.album_collection.empty: - self.album_object(song.album_collection[0]) - if not song.main_artist_collection.empty: - self.artist_object(song.main_artist_collection[0]) - - def album_object(self, album: Album): - self.album = album.title - self.album_type = album.album_type.value - - if not album.artist_collection.empty: - self.artist_object(album.artist_collection[0]) - if not album.label_collection.empty: - self.label_object(album.label_collection[0]) - - def artist_object(self, artist: Artist): - self.artist = artist.name - - if not artist.label_collection.empty: - self.label_object(artist.label_collection[0]) - - def label_object(self, label: Label): - self.label = label.name - -@dataclass -class DownloadResult: - total: int = 0 - fail: int = 0 - error_message: str = None - - @property - def success(self) -> int: - return self.total - self.fail - - @property - def fatal_error(self) -> bool: - return self.error_message is not None - - def merge(self, other: "DownloadResult"): - self.total += other.total - self.fail += other.fail - - def __repr__(self): - if self.fatal_error: - return self.error_message - return f"{self.fail} from {self.total} downloads failed." +from ..utils import shared class Page: @@ -115,7 +35,7 @@ class Page: TIMEOUT = 5 POST_TIMEOUT = TIMEOUT TRIES = 5 - LOGGER = LOGGER + LOGGER = logging.getLogger("this shouldn't be used") SOURCE_TYPE: SourcePages diff --git a/src/music_kraken/pages/support_classes/__init__.py b/src/music_kraken/pages/support_classes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/music_kraken/pages/support_classes/default_target.py b/src/music_kraken/pages/support_classes/default_target.py new file mode 100644 index 0000000..74cee42 --- /dev/null +++ b/src/music_kraken/pages/support_classes/default_target.py @@ -0,0 +1,65 @@ +from dataclasses import dataclass + +from ...utils.shared import DOWNLOAD_PATH, DOWNLOAD_FILE, DEFAULT_VALUES +from ...utils.string_processing import fit_to_file_system +from ...objects import ( + Song, + Album, + Artist, + Target, + Label +) + + +@dataclass +class DefaultTarget: + genre: str = DEFAULT_VALUES["genre"] + label: str = DEFAULT_VALUES["label"] + artist: str = DEFAULT_VALUES["artist"] + album: str = DEFAULT_VALUES["album"] + album_type: str = DEFAULT_VALUES["album_type"] + song: str = DEFAULT_VALUES["song"] + + def __setattr__(self, __name: str, __value: str) -> None: + if __name in DEFAULT_VALUES: + if self.__getattribute__(__name) == DEFAULT_VALUES[__name]: + super().__setattr__(__name, fit_to_file_system(__value)) + return + + super().__setattr__(__name, __value) + + @property + def target(self) -> Target: + return Target( + relative_to_music_dir=True, + path=DOWNLOAD_PATH.format(genre=self.genre, label=self.label, artist=self.artist, album=self.album, + song=self.song, album_type=self.album_type), + file=DOWNLOAD_FILE.format(genre=self.genre, label=self.label, artist=self.artist, album=self.album, + song=self.song, album_type=self.album_type) + ) + + def song_object(self, song: Song): + self.song = song.title + + if not song.album_collection.empty: + self.album_object(song.album_collection[0]) + if not song.main_artist_collection.empty: + self.artist_object(song.main_artist_collection[0]) + + def album_object(self, album: Album): + self.album = album.title + self.album_type = album.album_type.value + + if not album.artist_collection.empty: + self.artist_object(album.artist_collection[0]) + if not album.label_collection.empty: + self.label_object(album.label_collection[0]) + + def artist_object(self, artist: Artist): + self.artist = artist.name + + if not artist.label_collection.empty: + self.label_object(artist.label_collection[0]) + + def label_object(self, label: Label): + self.label = label.name diff --git a/src/music_kraken/pages/support_classes/download_result.py b/src/music_kraken/pages/support_classes/download_result.py new file mode 100644 index 0000000..a11f114 --- /dev/null +++ b/src/music_kraken/pages/support_classes/download_result.py @@ -0,0 +1,25 @@ +from dataclasses import dataclass + + +@dataclass +class DownloadResult: + total: int = 0 + fail: int = 0 + error_message: str = None + + @property + def success(self) -> int: + return self.total - self.fail + + @property + def fatal_error(self) -> bool: + return self.error_message is not None + + def merge(self, other: "DownloadResult"): + self.total += other.total + self.fail += other.fail + + def __repr__(self): + if self.fatal_error: + return self.error_message + return f"{self.fail} from {self.total} downloads failed."