forked from MayeulC/hb-downloader
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprogress_tracker.py
More file actions
122 lines (107 loc) · 4.31 KB
/
progress_tracker.py
File metadata and controls
122 lines (107 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logger
__author__ = "Brian Schkerke"
__copyright__ = "Copyright 2016 Brian Schkerke"
__license__ = "MIT"
import time
start_time = None
class ProgressTracker(object):
item_count_current = 0
item_count_total = 0
download_size_current = 0
download_size_total = 0
current_product = ""
current_subproduct = ""
current_download = ""
@staticmethod
def assign_download(hd):
ProgressTracker.current_product = hd.product_name
ProgressTracker.current_subproduct = hd.subproduct_name
ProgressTracker.current_download = hd.machine_name
@staticmethod
def display_summary():
global start_time
if start_time:
elapsed = time.time() - start_time
fasts = ProgressTracker.download_size_current / elapsed
remaining = (ProgressTracker.download_size_total - ProgressTracker.download_size_current) / fasts
else:
start_time = time.time()
remaining = None
fasts = 1024**2
progress_message = "%d/%d DL: %s/%s (%s, %s)" % (
ProgressTracker.item_count_current,
ProgressTracker.item_count_total,
ProgressTracker.format_filesize(
ProgressTracker.download_size_current),
ProgressTracker.format_filesize(
ProgressTracker.download_size_total),
ProgressTracker.format_percentage(
ProgressTracker.download_size_current,
ProgressTracker.download_size_total),
ProgressTracker.format_seconds(remaining, ProgressTracker.download_size_total, fasts))
logger.display_message(False, "Progress", progress_message)
logger.display_message(
True, "Progress", "%s: %s: %s" %
(ProgressTracker.current_product,
ProgressTracker.current_subproduct,
ProgressTracker.current_download))
@staticmethod
def reset():
ProgressTracker.item_count_total = 0
ProgressTracker.item_count_current = 0
ProgressTracker.download_size_current = 0
ProgressTracker.download_size_total = 0
ProgressTracker.current_product = ""
ProgressTracker.current_subproduct = ""
ProgressTracker.current_download = ""
@staticmethod
def format_filesize(filesize):
prefixes = [' bytes', ' KiB', ' MiB', ' GiB', ' TiB']
index_level = 0
while abs(filesize / 1024) > 1 and index_level < len(prefixes) - 1:
index_level += 1
filesize /= 1024
try:
size = "%.2f%s" % (filesize, prefixes[index_level])
except:
size = "unknown"
pass
return size
@staticmethod
def format_seconds(seconds, bytecount, speed):
if not seconds:
return ProgressTracker.format_seconds(bytecount / speed, 1, speed) + f" estimated @ {ProgressTracker.format_filesize(speed)}/s"
seconds = int(seconds)
if seconds < 90:
return f"{seconds}s left @ {ProgressTracker.format_filesize(speed)}/s"
minutes = int(seconds / 60)
seconds %= 60
if minutes < 90:
return f"{minutes}m {seconds}s left @ {ProgressTracker.format_filesize(speed)}/s"
hours = int(minutes / 60)
minutes %= 60
if hours < 30:
return f"{hours}h {minutes}m left @ {ProgressTracker.format_filesize(speed)}/s"
days = int(hours / 24)
hours %= 24
if days < 7:
return f"{days}d {hours}h {minutes}m left @ {ProgressTracker.format_filesize(speed)}/s"
weeks = int(days / 7)
if weeks < 3:
days %= 7
return f"{weeks}w {days}d {hours}h left @ {ProgressTracker.format_filesize(speed)}/s"
months = int(days / 30)
if months < 15:
days %= 30
return f"{months}m {days}d left @ {ProgressTracker.format_filesize(speed)}/s"
years = int(days / 365)
days %= 365
return f"{years}y {days}d left @ {ProgressTracker.format_filesize(speed)}/s"
@staticmethod
def format_percentage(current, total):
if total == 0:
return "0.00%"
else:
return '{percent:.2%}'.format(percent=(1.0 * current)/total)