Skip to content

Add download speed #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Scripts/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ def get_size(self, size, suffix=None, use_1024=False, round_to=2, strip_zeroes=F
b = b.rstrip("0") if strip_zeroes else b.ljust(round_to,"0") if round_to > 0 else ""
return "{:,}{} {}".format(int(a),"" if not b else "."+b,biggest)

def _progress_hook(self, response, bytes_so_far, total_size):
def _progress_hook(self, response, bytes_so_far, total_size, time_start):
if total_size > 0:
percent = float(bytes_so_far) / total_size
percent = round(percent*100, 2)
download_speed = bytes_so_far // (time.time() - time_start) / 1000000 / 8
t_s = self.get_size(total_size)
try: b_s = self.get_size(bytes_so_far, t_s.split(" ")[1])
except: b_s = self.get_size(bytes_so_far)
sys.stdout.write("Downloaded {} of {} ({:.2f}%)\r".format(b_s, t_s, percent))
sys.stdout.write("Downloaded {} of {} ({:.2f}%) \t {:.2f} MB/s\r".format(b_s, t_s, percent, download_speed))
else:
b_s = self.get_size(bytes_so_far)
sys.stdout.write("Downloaded {}\r".format(b_s))
Expand All @@ -105,7 +106,7 @@ def get_bytes(self, url, progress = True, headers = None, expand_gzip = True):
while True:
chunk = response.read(self.chunk)
bytes_so_far += len(chunk)
if progress: self._progress_hook(response, bytes_so_far, total_size)
if progress: self._progress_hook(response, bytes_so_far, total_size, time_start)
if not chunk: break
chunk_so_far += chunk
if expand_gzip and response.headers.get("Content-Encoding","unknown").lower() == "gzip":
Expand All @@ -124,7 +125,7 @@ def stream_to_file(self, url, file_path, progress = True, headers = None):
while True:
chunk = response.read(self.chunk)
bytes_so_far += len(chunk)
if progress: self._progress_hook(response, bytes_so_far, total_size)
if progress: self._progress_hook(response, bytes_so_far, total_size, time_start)
if not chunk: break
f.write(chunk)
return file_path if os.path.exists(file_path) else None