mirror of
https://github.com/paul-nameless/tg
synced 2024-11-22 20:03:17 +00:00
Humanize size and duration fields
This commit is contained in:
parent
57da72e141
commit
3d26774f7d
2 changed files with 30 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
from tg import utils
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -60,14 +61,14 @@ class MsgProxy:
|
|||
@property
|
||||
def size(self):
|
||||
doc = self.get_doc(self.msg)
|
||||
return doc["size"]
|
||||
return utils.humanize_size(doc["size"])
|
||||
|
||||
@property
|
||||
def duration(self):
|
||||
if self.type not in ("audio", "voice"):
|
||||
if self.type not in ("audio", "voice", "video", "recording"):
|
||||
return None
|
||||
doc = self.get_doc(self.msg, deep=1)
|
||||
return doc["duration"]
|
||||
return utils.humanize_duration(doc["duration"])
|
||||
|
||||
@property
|
||||
def file_name(self):
|
||||
|
|
31
tg/utils.py
31
tg/utils.py
|
@ -1,14 +1,35 @@
|
|||
import logging
|
||||
import os
|
||||
from typing import Optional
|
||||
import subprocess
|
||||
from functools import wraps
|
||||
import curses
|
||||
import logging
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
from functools import wraps
|
||||
from typing import Optional
|
||||
|
||||
from tg import config
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def humanize_size(
|
||||
num, suffix="B", suffixes=("", "K", "M", "G", "T", "P", "E", "Z")
|
||||
):
|
||||
magnitude = int(math.floor(math.log(num, 1024)))
|
||||
val = num / math.pow(1024, magnitude)
|
||||
if magnitude > 7:
|
||||
return "{:.1f}{}{}".format(val, "Yi", suffix)
|
||||
return "{:3.1f}{}{}".format(val, suffixes[magnitude], suffix)
|
||||
|
||||
|
||||
def humanize_duration(seconds):
|
||||
dt = datetime.utcfromtimestamp(seconds)
|
||||
fmt = "%-M:%S"
|
||||
if seconds >= 3600:
|
||||
fmt = "%-H:%M:%S"
|
||||
return dt.strftime(fmt)
|
||||
|
||||
|
||||
def num(value: str, default: Optional[int] = None) -> Optional[int]:
|
||||
try:
|
||||
return int(value)
|
||||
|
|
Loading…
Reference in a new issue