34 lines
962 B
Python
34 lines
962 B
Python
"""Utility functions for the server manager bot."""
|
|
|
|
import logging
|
|
|
|
import humanize
|
|
from telebot.formatting import escape_markdown
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
__all__ = ["format_seconds", "human_speed", "escape_markdown"]
|
|
|
|
|
|
def format_seconds(seconds: int) -> str:
|
|
"""Format seconds into human-readable duration."""
|
|
if seconds < 60:
|
|
return f"{seconds}s"
|
|
elif seconds < 3600:
|
|
return f"{seconds // 60}m {seconds % 60}s"
|
|
elif seconds < 86400:
|
|
hours = seconds // 3600
|
|
minutes = (seconds % 3600) // 60
|
|
return f"{hours}h {minutes}m"
|
|
else:
|
|
days = seconds // 86400
|
|
hours = (seconds % 86400) // 3600
|
|
return f"{days}d {hours}h"
|
|
|
|
|
|
def human_speed(speed_bytes: int) -> str:
|
|
"""Format bytes per second into human-readable speed."""
|
|
if speed_bytes < 1024:
|
|
return f"{speed_bytes} B/s"
|
|
return humanize.naturalsize(speed_bytes, binary=True, format="%.1f") + "/s"
|