fixed download size feedback

This commit is contained in:
Hellow2 2023-04-05 12:25:57 +02:00
parent 90b0d0ae20
commit 5b947c2d66
2 changed files with 29 additions and 15 deletions

View File

@ -513,17 +513,15 @@ class Page:
target: Target
if any(target.exists for target in song.target_collection) and not override_existing:
r = DownloadResult(total=1, fail=0)
existing_target: Target
for existing_target in song.target_collection:
if existing_target.exists:
r.merge(cls._post_process_targets(song=song, temp_target=existing_target))
break
for target in song.target_collection:
if target is existing_target:
continue
existing_target.copy_content(target)
return DownloadResult(total=1, fail=0)
return r
sources = song.source_collection.get_sources_from_page(cls.SOURCE_TYPE)
if len(sources) == 0:
@ -537,20 +535,23 @@ class Page:
r = cls._download_song_to_targets(source=sources[0], target=temp_target, desc=song.title)
if not r.is_fatal_error:
cls._post_process_targets(song, temp_target)
for target in song.target_collection:
r.add_target(target)
r.merge(cls._post_process_targets(song, temp_target))
return r
@classmethod
def _post_process_targets(cls, song: Song, temp_target: Target):
def _post_process_targets(cls, song: Song, temp_target: Target) -> DownloadResult:
write_metadata_to_target(song.metadata, temp_target)
r = DownloadResult()
target: Target
for target in song.target_collection:
temp_target.copy_content(target)
if temp_target is not target:
temp_target.copy_content(target)
r.add_target(target)
return r
@classmethod
def _fetch_song_from_source(cls, source: Source, stop_at_level: int = 1) -> Song:

View File

@ -1,10 +1,14 @@
from dataclasses import dataclass, field
from typing import List
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
@ -41,9 +45,18 @@ class DownloadResult:
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:
return f"{self.total_size}B"
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