Skip to content

Commit

Permalink
Calculate progress only when printing progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Apr 28, 2024
1 parent 9549679 commit aac450a
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions twitchdl/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def __init__(self, vod_count: int):
self.remaining_time: Optional[int] = None
self.samples: Deque[Sample] = deque(maxlen=1000)
self.speed: Optional[float] = None
self.start_time: float = time.time()
self.tasks: Dict[TaskId, Task] = {}
self.vod_count = vod_count
self.vod_downloaded_count: int = 0
Expand All @@ -51,8 +50,6 @@ def start(self, task_id: int, size: int):
raise ValueError(f"Task {task_id}: cannot start, already started")

self.tasks[task_id] = Task(task_id, size)
self._calculate_total()
self._calculate_progress()
self.print()

def advance(self, task_id: int, size: int):
Expand All @@ -63,7 +60,6 @@ def advance(self, task_id: int, size: int):
self.progress_bytes += size
self.tasks[task_id].advance(size)
self.samples.append(Sample(self.downloaded, time.time()))
self._calculate_progress()
self.print()

def already_downloaded(self, task_id: int, size: int):
Expand All @@ -73,8 +69,6 @@ def already_downloaded(self, task_id: int, size: int):
self.tasks[task_id] = Task(task_id, size)
self.progress_bytes += size
self.vod_downloaded_count += 1
self._calculate_total()
self._calculate_progress()
self.print()

def abort(self, task_id: int):
Expand All @@ -83,9 +77,6 @@ def abort(self, task_id: int):

del self.tasks[task_id]
self.progress_bytes = sum(t.downloaded for t in self.tasks.values())

self._calculate_total()
self._calculate_progress()
self.print()

def end(self, task_id: int):
Expand All @@ -101,12 +92,10 @@ def end(self, task_id: int):
self.vod_downloaded_count += 1
self.print()

def _calculate_total(self):
def _recalculate(self):
self.estimated_total = (
int(mean(t.size for t in self.tasks.values()) * self.vod_count) if self.tasks else None
)

def _calculate_progress(self):
self.speed = self._calculate_speed()
self.progress_perc = (
int(100 * self.progress_bytes / self.estimated_total) if self.estimated_total else 0
Expand Down Expand Up @@ -136,6 +125,8 @@ def print(self):
if self.last_printed and now - self.last_printed < 0.1:
return

self._recalculate()

click.echo(f"\rDownloaded {self.vod_downloaded_count}/{self.vod_count} VODs", nl=False)
click.secho(f" {self.progress_perc}%", fg="blue", nl=False)

Expand Down

0 comments on commit aac450a

Please sign in to comment.