2022-08-13 07:38:54 +00:00
|
|
|
import logging
|
|
|
|
import time
|
2022-08-17 06:35:57 +00:00
|
|
|
from collections import deque
|
2024-04-28 07:57:33 +00:00
|
|
|
from dataclasses import dataclass
|
2022-08-13 07:38:54 +00:00
|
|
|
from statistics import mean
|
2024-04-04 06:20:10 +00:00
|
|
|
from typing import Deque, Dict, NamedTuple, Optional
|
|
|
|
|
|
|
|
import click
|
2022-08-13 07:38:54 +00:00
|
|
|
|
2024-05-31 10:57:51 +00:00
|
|
|
from twitchdl.output import blue, clear_line
|
2022-08-14 09:33:38 +00:00
|
|
|
from twitchdl.utils import format_size, format_time
|
2022-08-13 07:38:54 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
TaskId = int
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Task:
|
|
|
|
id: TaskId
|
|
|
|
size: int
|
|
|
|
downloaded: int = 0
|
|
|
|
|
2024-03-26 09:23:50 +00:00
|
|
|
def advance(self, size: int):
|
2022-08-13 07:38:54 +00:00
|
|
|
self.downloaded += size
|
|
|
|
|
|
|
|
|
2022-08-17 06:35:57 +00:00
|
|
|
class Sample(NamedTuple):
|
|
|
|
downloaded: int
|
|
|
|
timestamp: float
|
|
|
|
|
|
|
|
|
2022-08-13 07:38:54 +00:00
|
|
|
class Progress:
|
2024-08-28 12:38:17 +00:00
|
|
|
def __init__(self, file_count: Optional[int] = None):
|
2024-04-28 07:57:33 +00:00
|
|
|
self.downloaded: int = 0
|
|
|
|
self.estimated_total: Optional[int] = None
|
|
|
|
self.last_printed: Optional[float] = None
|
|
|
|
self.progress_bytes: int = 0
|
|
|
|
self.progress_perc: int = 0
|
|
|
|
self.remaining_time: Optional[int] = None
|
|
|
|
self.samples: Deque[Sample] = deque(maxlen=1000)
|
|
|
|
self.speed: Optional[float] = None
|
|
|
|
self.tasks: Dict[TaskId, Task] = {}
|
2024-08-28 12:38:17 +00:00
|
|
|
self.file_count = file_count
|
|
|
|
self.downloaded_count: int = 0
|
2022-08-13 07:38:54 +00:00
|
|
|
|
|
|
|
def start(self, task_id: int, size: int):
|
|
|
|
if task_id in self.tasks:
|
|
|
|
raise ValueError(f"Task {task_id}: cannot start, already started")
|
|
|
|
|
|
|
|
self.tasks[task_id] = Task(task_id, size)
|
|
|
|
self.print()
|
|
|
|
|
2022-08-17 06:35:57 +00:00
|
|
|
def advance(self, task_id: int, size: int):
|
2022-08-13 07:38:54 +00:00
|
|
|
if task_id not in self.tasks:
|
|
|
|
raise ValueError(f"Task {task_id}: cannot advance, not started")
|
|
|
|
|
2022-08-17 06:35:57 +00:00
|
|
|
self.downloaded += size
|
|
|
|
self.progress_bytes += size
|
|
|
|
self.tasks[task_id].advance(size)
|
|
|
|
self.samples.append(Sample(self.downloaded, time.time()))
|
2022-08-13 07:38:54 +00:00
|
|
|
self.print()
|
|
|
|
|
2022-08-14 08:02:41 +00:00
|
|
|
def already_downloaded(self, task_id: int, size: int):
|
|
|
|
if task_id in self.tasks:
|
|
|
|
raise ValueError(f"Task {task_id}: cannot mark as downloaded, already started")
|
|
|
|
|
|
|
|
self.tasks[task_id] = Task(task_id, size)
|
|
|
|
self.progress_bytes += size
|
2024-08-28 12:38:17 +00:00
|
|
|
self.downloaded_count += 1
|
2022-08-14 08:02:41 +00:00
|
|
|
self.print()
|
|
|
|
|
2022-08-13 07:38:54 +00:00
|
|
|
def abort(self, task_id: int):
|
|
|
|
if task_id not in self.tasks:
|
|
|
|
raise ValueError(f"Task {task_id}: cannot abort, not started")
|
|
|
|
|
|
|
|
del self.tasks[task_id]
|
2022-08-14 08:02:41 +00:00
|
|
|
self.progress_bytes = sum(t.downloaded for t in self.tasks.values())
|
2022-08-13 07:38:54 +00:00
|
|
|
self.print()
|
|
|
|
|
|
|
|
def end(self, task_id: int):
|
|
|
|
if task_id not in self.tasks:
|
|
|
|
raise ValueError(f"Task {task_id}: cannot end, not started")
|
|
|
|
|
|
|
|
task = self.tasks[task_id]
|
|
|
|
if task.size != task.downloaded:
|
2024-04-04 06:20:10 +00:00
|
|
|
logger.warn(
|
|
|
|
f"Taks {task_id} ended with {task.downloaded}b downloaded, expected {task.size}b."
|
|
|
|
)
|
2022-08-13 07:38:54 +00:00
|
|
|
|
2024-08-28 12:38:17 +00:00
|
|
|
self.downloaded_count += 1
|
2022-08-13 07:38:54 +00:00
|
|
|
self.print()
|
|
|
|
|
2024-04-28 08:13:47 +00:00
|
|
|
def _recalculate(self):
|
2024-08-28 12:38:17 +00:00
|
|
|
if self.tasks and self.file_count:
|
|
|
|
self.estimated_total = int(mean(t.size for t in self.tasks.values()) * self.file_count)
|
|
|
|
else:
|
|
|
|
self.estimated_total = None
|
|
|
|
|
2022-08-17 06:35:57 +00:00
|
|
|
self.speed = self._calculate_speed()
|
2024-04-04 06:20:10 +00:00
|
|
|
self.progress_perc = (
|
|
|
|
int(100 * self.progress_bytes / self.estimated_total) if self.estimated_total else 0
|
|
|
|
)
|
|
|
|
self.remaining_time = (
|
|
|
|
int((self.estimated_total - self.progress_bytes) / self.speed)
|
|
|
|
if self.estimated_total and self.speed
|
|
|
|
else None
|
|
|
|
)
|
2022-08-13 07:38:54 +00:00
|
|
|
|
2022-08-17 06:35:57 +00:00
|
|
|
def _calculate_speed(self):
|
|
|
|
if len(self.samples) < 2:
|
|
|
|
return None
|
|
|
|
|
|
|
|
first_sample = self.samples[0]
|
|
|
|
last_sample = self.samples[-1]
|
|
|
|
|
|
|
|
size = last_sample.downloaded - first_sample.downloaded
|
|
|
|
duration = last_sample.timestamp - first_sample.timestamp
|
|
|
|
|
2024-04-10 06:47:11 +00:00
|
|
|
return size / duration if duration > 0 else None
|
2022-08-17 06:35:57 +00:00
|
|
|
|
2022-08-13 07:38:54 +00:00
|
|
|
def print(self):
|
2022-08-14 09:04:53 +00:00
|
|
|
now = time.time()
|
|
|
|
|
|
|
|
# Don't print more often than 10 times per second
|
2024-04-28 07:57:33 +00:00
|
|
|
if self.last_printed and now - self.last_printed < 0.1:
|
2022-08-14 09:04:53 +00:00
|
|
|
return
|
|
|
|
|
2024-04-28 08:13:47 +00:00
|
|
|
self._recalculate()
|
|
|
|
|
2024-05-31 10:57:51 +00:00
|
|
|
clear_line()
|
2024-08-28 12:38:17 +00:00
|
|
|
total_label = f"/{self.file_count}" if self.file_count else ""
|
2024-08-30 05:59:51 +00:00
|
|
|
click.echo(f"Downloaded {self.downloaded_count}{total_label} VODs", nl=False)
|
2024-04-12 07:43:04 +00:00
|
|
|
click.secho(f" {self.progress_perc}%", fg="blue", nl=False)
|
|
|
|
|
|
|
|
if self.estimated_total is not None:
|
|
|
|
total = f"~{format_size(self.estimated_total)}"
|
|
|
|
click.echo(f" of {blue(total)}", nl=False)
|
|
|
|
|
|
|
|
if self.speed is not None:
|
|
|
|
speed = f"{format_size(self.speed)}/s"
|
|
|
|
click.echo(f" at {blue(speed)}", nl=False)
|
|
|
|
|
|
|
|
if self.remaining_time is not None:
|
|
|
|
click.echo(f" ETA {blue(format_time(self.remaining_time))}", nl=False)
|
|
|
|
|
2022-08-14 09:04:53 +00:00
|
|
|
self.last_printed = now
|