forked from uniborg/uniborg
Compare commits
3 Commits
5dd06471db
...
3178eb5fba
Author | SHA1 | Date |
---|---|---|
udf | 3178eb5fba | |
udf | 59245a4e6d | |
udf | 6dc069afd9 |
|
@ -4,13 +4,11 @@
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events
|
||||||
from telethon.utils import add_surrogate
|
from telethon.utils import add_surrogate
|
||||||
from telethon.tl.types import MessageEntityPre
|
from telethon.tl.types import MessageEntityPre, DocumentAttributeFilename
|
||||||
from telethon.tl.tlobject import TLObject
|
from telethon.tl.tlobject import TLObject
|
||||||
|
from telethon.errors import MessageTooLongError
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
STR_LEN_MAX = 256
|
|
||||||
BYTE_LEN_MAX = 64
|
|
||||||
|
|
||||||
|
|
||||||
def parse_pre(text):
|
def parse_pre(text):
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
|
@ -20,7 +18,7 @@ def parse_pre(text):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def yaml_format(obj, indent=0):
|
def yaml_format(obj, indent=0, max_str_len=256, max_byte_len=64):
|
||||||
"""
|
"""
|
||||||
Pretty formats the given object as a YAML string which is returned.
|
Pretty formats the given object as a YAML string which is returned.
|
||||||
(based on TLObject.pretty_format)
|
(based on TLObject.pretty_format)
|
||||||
|
@ -55,8 +53,8 @@ def yaml_format(obj, indent=0):
|
||||||
indent -= 2
|
indent -= 2
|
||||||
elif isinstance(obj, str):
|
elif isinstance(obj, str):
|
||||||
# truncate long strings and display elipsis
|
# truncate long strings and display elipsis
|
||||||
result = repr(obj[:STR_LEN_MAX])
|
result = repr(obj[:max_str_len])
|
||||||
if len(obj) > STR_LEN_MAX:
|
if len(obj) > max_str_len:
|
||||||
result += '…'
|
result += '…'
|
||||||
return result
|
return result
|
||||||
elif isinstance(obj, bytes):
|
elif isinstance(obj, bytes):
|
||||||
|
@ -64,7 +62,7 @@ def yaml_format(obj, indent=0):
|
||||||
if all(0x20 <= c < 0x7f for c in obj):
|
if all(0x20 <= c < 0x7f for c in obj):
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
else:
|
else:
|
||||||
return ('<…>' if len(obj) > BYTE_LEN_MAX else
|
return ('<…>' if len(obj) > max_byte_len else
|
||||||
' '.join(f'{b:02X}' for b in obj))
|
' '.join(f'{b:02X}' for b in obj))
|
||||||
elif isinstance(obj, datetime.datetime):
|
elif isinstance(obj, datetime.datetime):
|
||||||
# ISO-8601 without timezone offset (telethon dates are always UTC)
|
# ISO-8601 without timezone offset (telethon dates are always UTC)
|
||||||
|
@ -74,7 +72,6 @@ def yaml_format(obj, indent=0):
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
indent += 2
|
indent += 2
|
||||||
for x in obj:
|
for x in obj:
|
||||||
|
|
||||||
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
|
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
result.pop()
|
result.pop()
|
||||||
|
@ -91,4 +88,21 @@ async def _(event):
|
||||||
return
|
return
|
||||||
msg = await event.message.get_reply_message()
|
msg = await event.message.get_reply_message()
|
||||||
yaml_text = yaml_format(msg)
|
yaml_text = yaml_format(msg)
|
||||||
|
try:
|
||||||
await event.edit(yaml_text, parse_mode=parse_pre)
|
await event.edit(yaml_text, parse_mode=parse_pre)
|
||||||
|
except MessageTooLongError:
|
||||||
|
await event.delete()
|
||||||
|
yaml_text = yaml_format(
|
||||||
|
msg,
|
||||||
|
max_str_len=9999,
|
||||||
|
max_byte_len=9999
|
||||||
|
)
|
||||||
|
await borg.send_file(
|
||||||
|
await event.get_input_chat(),
|
||||||
|
f'<pre>{yaml_text}</pre>'.encode('utf-8'),
|
||||||
|
reply_to=msg,
|
||||||
|
attributes=[
|
||||||
|
DocumentAttributeFilename('info.html')
|
||||||
|
],
|
||||||
|
allow_cache=False
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue