Skip to content

Commit

Permalink
Create human_read.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Codeflix-Bots authored May 15, 2024
1 parent 06c623b commit aee0419
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions plugins/helper/human_read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
SIZE_UNITS = ["B", "KB", "MB", "GB", "TB", "PB"]


def get_readable_file_size(size_in_bytes) -> str:
if size_in_bytes is None:
return "0B"
index = 0
while size_in_bytes >= 1024 and index < len(SIZE_UNITS) - 1:
size_in_bytes /= 1024
index += 1
return f"{size_in_bytes:.2f} {SIZE_UNITS[index]}" if index > 0 else f"{size_in_bytes}B"


def get_readable_time(seconds: int) -> str:
result = ""
(days, remainder) = divmod(seconds, 86400)
days = int(days)
if days != 0:
result += f"{days}d "
(hours, remainder) = divmod(remainder, 3600)
hours = int(hours)
if hours != 0:
result += f"{hours}h "
(minutes, seconds) = divmod(remainder, 60)
minutes = int(minutes)
if minutes != 0:
result += f"{minutes}m "
seconds = int(seconds)
result += f"{seconds}s "
return result


def get_readable_bitrate(bitrate_kbps):
return (
f"{str(round(bitrate_kbps / 1000, 2))} Mb/s"
if bitrate_kbps > 10000
else f"{str(round(bitrate_kbps, 2))} kb/s"
)


def get_readable_time2(seconds: int) -> str:
count = 0
ping_time = ""
time_list = []
time_suffix_list = ["s", "m", "h", "d", "w", "m", "y"]
while count < 4:
count += 1
remainder, result = divmod(seconds, 60) if count < 3 else divmod(seconds, 24)
if seconds == 0 and remainder == 0:
break
time_list.append(int(result))
seconds = int(remainder)
for i in range(len(time_list)):
time_list[i] = str(time_list[i]) + time_suffix_list[i]
if len(time_list) == 4:
ping_time += f"{time_list.pop()}, "
time_list.reverse()
ping_time += ":".join(time_list)
return ping_time

0 comments on commit aee0419

Please sign in to comment.