xyphron / DragMusic /utils /formatters.py
taslim19
feat: Add advanced antiflood plugin and fix admin commands
98e44f0
import json
import subprocess
import re
from datetime import datetime, timedelta
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.strip()
def convert_bytes(size: float) -> str:
"""humanize size"""
if not size:
return ""
power = 1024
t_n = 0
power_dict = {0: " ", 1: "Ki", 2: "Mi", 3: "Gi", 4: "Ti"}
while size > power:
size /= power
t_n += 1
return "{:.2f} {}B".format(size, power_dict[t_n])
async def int_to_alpha(user_id: int) -> str:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
text = ""
user_id = str(user_id)
for i in user_id:
text += alphabet[int(i)]
return text
async def alpha_to_int(user_id_alphabet: str) -> int:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
user_id = ""
for i in user_id_alphabet:
index = alphabet.index(i)
user_id += str(index)
user_id = int(user_id)
return user_id
def time_to_seconds(time):
stringt = str(time)
return sum(int(x) * 60**i for i, x in enumerate(reversed(stringt.split(":"))))
def seconds_to_min(seconds):
if seconds is not None:
seconds = int(seconds)
d, h, m, s = (
seconds // (3600 * 24),
seconds // 3600 % 24,
seconds % 3600 // 60,
seconds % 3600 % 60,
)
if d > 0:
return "{:02d}:{:02d}:{:02d}:{:02d}".format(d, h, m, s)
elif h > 0:
return "{:02d}:{:02d}:{:02d}".format(h, m, s)
elif m > 0:
return "{:02d}:{:02d}".format(m, s)
elif s > 0:
return "00:{:02d}".format(s)
return "-"
def speed_converter(seconds, speed):
if str(speed) == str("0.5"):
seconds = seconds * 2
if str(speed) == str("0.75"):
seconds = seconds + ((50 * seconds) // 100)
if str(speed) == str("1.5"):
seconds = seconds - ((25 * seconds) // 100)
if str(speed) == str("2.0"):
seconds = seconds - ((50 * seconds) // 100)
collect = seconds
if seconds is not None:
seconds = int(seconds)
d, h, m, s = (
seconds // (3600 * 24),
seconds // 3600 % 24,
seconds % 3600 // 60,
seconds % 3600 % 60,
)
if d > 0:
convert = "{:02d}:{:02d}:{:02d}:{:02d}".format(d, h, m, s)
return convert, collect
elif h > 0:
convert = "{:02d}:{:02d}:{:02d}".format(h, m, s)
return convert, collect
elif m > 0:
convert = "{:02d}:{:02d}".format(m, s)
return convert, collect
elif s > 0:
convert = "00:{:02d}".format(s)
return convert, collect
return "-"
def check_duration(file_path):
command = [
"ffprobe",
"-loglevel",
"quiet",
"-print_format",
"json",
"-show_format",
"-show_streams",
file_path,
]
pipe = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = pipe.communicate()
_json = json.loads(out)
if "format" in _json:
if "duration" in _json["format"]:
return float(_json["format"]["duration"])
if "streams" in _json:
for s in _json["streams"]:
if "duration" in s:
return float(s["duration"])
return "Unknown"
formats = [
"webm",
"mkv",
"flv",
"vob",
"ogv",
"ogg",
"rrc",
"gifv",
"mng",
"mov",
"avi",
"qt",
"wmv",
"yuv",
"rm",
"asf",
"amv",
"mp4",
"m4p",
"m4v",
"mpg",
"mp2",
"mpeg",
"mpe",
"mpv",
"m4v",
"svi",
"3gp",
"3g2",
"mxf",
"roq",
"nsv",
"flv",
"f4v",
"f4p",
"f4a",
"f4b",
]
def parse_time(time_str: str) -> timedelta:
matches = re.match(r"(\d+)([d|h|m|s])", time_str)
if matches:
quantity, unit = matches.groups()
quantity = int(quantity)
if unit == 'd':
return timedelta(days=quantity)
elif unit == 'h':
return timedelta(hours=quantity)
elif unit == 'm':
return timedelta(minutes=quantity)
elif unit == 's':
return timedelta(seconds=quantity)
return timedelta(minutes=0) # Default or error case