moved support class in a seperate module
This commit is contained in:
parent
ba2e3463d3
commit
e8d17d5f37
@ -1,18 +1,18 @@
|
|||||||
import random
|
|
||||||
from typing import Optional, Union, Type, Dict, List, Set, Tuple
|
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
import requests
|
|
||||||
import logging
|
import logging
|
||||||
from dataclasses import dataclass
|
import random
|
||||||
from copy import copy
|
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 (
|
from ..objects import (
|
||||||
Song,
|
Song,
|
||||||
Source,
|
Source,
|
||||||
Album,
|
Album,
|
||||||
Artist,
|
Artist,
|
||||||
Lyrics,
|
|
||||||
Target,
|
Target,
|
||||||
DatabaseObject,
|
DatabaseObject,
|
||||||
Options,
|
Options,
|
||||||
@ -22,87 +22,7 @@ from ..objects import (
|
|||||||
AlbumType
|
AlbumType
|
||||||
)
|
)
|
||||||
from ..tagging import write_metadata_to_target
|
from ..tagging import write_metadata_to_target
|
||||||
from ..utils.shared import DOWNLOAD_PATH, DOWNLOAD_FILE, DEFAULT_VALUES
|
from ..utils import shared
|
||||||
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."
|
|
||||||
|
|
||||||
|
|
||||||
class Page:
|
class Page:
|
||||||
@ -115,7 +35,7 @@ class Page:
|
|||||||
TIMEOUT = 5
|
TIMEOUT = 5
|
||||||
POST_TIMEOUT = TIMEOUT
|
POST_TIMEOUT = TIMEOUT
|
||||||
TRIES = 5
|
TRIES = 5
|
||||||
LOGGER = LOGGER
|
LOGGER = logging.getLogger("this shouldn't be used")
|
||||||
|
|
||||||
SOURCE_TYPE: SourcePages
|
SOURCE_TYPE: SourcePages
|
||||||
|
|
||||||
|
0
src/music_kraken/pages/support_classes/__init__.py
Normal file
0
src/music_kraken/pages/support_classes/__init__.py
Normal file
65
src/music_kraken/pages/support_classes/default_target.py
Normal file
65
src/music_kraken/pages/support_classes/default_target.py
Normal file
@ -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
|
25
src/music_kraken/pages/support_classes/download_result.py
Normal file
25
src/music_kraken/pages/support_classes/download_result.py
Normal file
@ -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."
|
Loading…
Reference in New Issue
Block a user