query function
This commit is contained in:
@@ -7,8 +7,8 @@ import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from ..connection import Connection
|
||||
from .support_classes.default_target import DefaultTarget
|
||||
from .support_classes.download_result import DownloadResult
|
||||
from ..utils.support_classes.default_target import DefaultTarget
|
||||
from ..utils.support_classes.download_result import DownloadResult
|
||||
from ..objects import (
|
||||
Song,
|
||||
Source,
|
||||
|
@@ -1,70 +0,0 @@
|
||||
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"]
|
||||
audio_format: str = DEFAULT_VALUES["audio_format"]
|
||||
|
||||
def __setattr__(self, __name: str, __value: str) -> None:
|
||||
if __name in DEFAULT_VALUES:
|
||||
if type(__value) != str:
|
||||
return
|
||||
|
||||
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, audio_format=self.audio_format),
|
||||
file=DOWNLOAD_FILE.format(genre=self.genre, label=self.label, artist=self.artist, album=self.album,
|
||||
song=self.song, album_type=self.album_type, audio_format=self.audio_format)
|
||||
)
|
||||
|
||||
def song_object(self, song: Song):
|
||||
self.song = song.title
|
||||
self.genre = song.genre
|
||||
|
||||
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
|
@@ -1,89 +0,0 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Tuple
|
||||
|
||||
from ...utils.shared import SHOW_DOWNLOAD_ERRORS_THRESHOLD, DOWNLOAD_LOGGER as LOGGER
|
||||
from ...objects import Target
|
||||
|
||||
UNIT_PREFIXES: List[str] = ["", "k", "m", "g", "t"]
|
||||
UNIT_DIVISOR = 1024
|
||||
|
||||
|
||||
@dataclass
|
||||
class DownloadResult:
|
||||
total: int = 0
|
||||
fail: int = 0
|
||||
error_message: str = None
|
||||
total_size = 0
|
||||
|
||||
_error_message_list: List[str] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def success(self) -> int:
|
||||
return self.total - self.fail
|
||||
|
||||
@property
|
||||
def success_percentage(self) -> float:
|
||||
if self.total == 0:
|
||||
return 0
|
||||
return self.success / self.total
|
||||
|
||||
@property
|
||||
def failure_percentage(self) -> float:
|
||||
if self.total == 0:
|
||||
return 1
|
||||
return self.fail / self.total
|
||||
|
||||
@property
|
||||
def is_fatal_error(self) -> bool:
|
||||
return self.error_message is not None
|
||||
|
||||
@property
|
||||
def is_mild_failure(self) -> bool:
|
||||
if self.is_fatal_error:
|
||||
return True
|
||||
|
||||
return self.failure_percentage > SHOW_DOWNLOAD_ERRORS_THRESHOLD
|
||||
|
||||
def _size_val_unit_pref_ind(self, val: float, ind: int) -> Tuple[float, int]:
|
||||
if val < UNIT_DIVISOR:
|
||||
return val, ind
|
||||
if ind >= len(UNIT_PREFIXES):
|
||||
return val, ind
|
||||
|
||||
return self._size_val_unit_pref_ind(val=val / UNIT_DIVISOR, ind=ind + 1)
|
||||
|
||||
@property
|
||||
def formated_size(self) -> str:
|
||||
total_size, prefix_index = self._size_val_unit_pref_ind(self.total_size, 0)
|
||||
return f"{total_size:.{2}f} {UNIT_PREFIXES[prefix_index]}B"
|
||||
|
||||
def add_target(self, target: Target):
|
||||
self.total_size += target.size
|
||||
|
||||
def merge(self, other: "DownloadResult"):
|
||||
if other.is_fatal_error:
|
||||
LOGGER.debug(other.error_message)
|
||||
self._error_message_list.append(other.error_message)
|
||||
self.total += 1
|
||||
self.fail += 1
|
||||
else:
|
||||
self.total += other.total
|
||||
self.fail += other.fail
|
||||
self._error_message_list.extend(other._error_message_list)
|
||||
|
||||
self.total_size += other.total_size
|
||||
|
||||
def __str__(self):
|
||||
if self.is_fatal_error:
|
||||
return self.error_message
|
||||
head = f"{self.fail} from {self.total} downloads failed:\n" \
|
||||
f"successrate:\t{int(self.success_percentage * 100)}%\n" \
|
||||
f"failrate:\t{int(self.failure_percentage * 100)}%\n" \
|
||||
f"total size:\t{self.formated_size}"
|
||||
|
||||
if not self.is_mild_failure:
|
||||
return head
|
||||
|
||||
_lines = [head]
|
||||
_lines.extend(self._error_message_list)
|
||||
return "\n".join(_lines)
|
Reference in New Issue
Block a user