patch 3c6a7e6e9f4c7cae2781b33d0a23e1ea92c0e060 Author: E. Bosch Date: Wed Feb 9 23:52:06 CET 2022 * telegram: Improve metadata for documents: files and videos diff -rN -u old-irgramd/telegram.py new-irgramd/telegram.py --- old-irgramd/telegram.py 2024-11-22 21:20:06.400367262 +0100 +++ new-irgramd/telegram.py 2024-11-22 21:20:06.400367262 +0100 @@ -10,7 +10,7 @@ from include import CHAN_MAX_LENGHT, NICK_MAX_LENGTH from irc import IRCUser -from utils import sanitize_filename, remove_slash, remove_http_s +from utils import sanitize_filename, remove_slash, remove_http_s, get_human_size, get_human_duration # Configuration @@ -341,12 +341,17 @@ media_type = 'photo' elif message.audio: media_type = 'audio' elif message.voice: media_type = 'rec' - elif message.video: media_type = 'video' + elif message.video: + size = get_human_size(message.media.document.size) + attrib = next(x for x in message.media.document.attributes if isinstance(x, tgty.DocumentAttributeVideo)) + dur = get_human_duration(attrib.duration) + media_type = 'video:{},{}'.format(size, dur) elif message.video_note: media_type = 'videorec' elif message.gif: media_type = 'anim' elif message.sticker: media_type = 'sticker' - elif message.document: media_type = 'file' - + elif message.document: + size = get_human_size(message.media.document.size) + media_type = 'file:{}'.format(size) elif message.contact: media_type = 'contact' caption = '' diff -rN -u old-irgramd/utils.py new-irgramd/utils.py --- old-irgramd/utils.py 2024-11-22 21:20:06.400367262 +0100 +++ new-irgramd/utils.py 2024-11-22 21:20:06.400367262 +0100 @@ -61,3 +61,31 @@ else: surl = url return remove_slash(surl) + +def get_human_size(size): + human_units = ('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') + + def get_human_size_values(size, unit_pos=0): + aux = size / 1024.0 + if aux > 1: return get_human_size_values(aux, unit_pos + 1) + else: return size, human_units[unit_pos] + + if size <= 1237940039285380274899124224: # 1024Y + num, unit = get_human_size_values(size) + else: + num = size / 1208925819614629174706176 # 1Y + unit = 'Y' + + fs = '{:.1f}{}' if num < 10 else '{:.0f}{}' + + return fs.format(num, unit) + +def get_human_duration(duration): + res = '' + x, s = divmod(duration, 60) + h, m = divmod(x, 60) + + if h > 0: res = str(h) + 'h' + if m > 0: res += str(m) + 'm' + if s > 0: res += str(s) + 's' + return res