forked from kate/uniborg
Stop downloading media snip and respect their origin
This commit is contained in:
parent
ec50e689b4
commit
c1a025f4e2
|
@ -8,40 +8,47 @@ from telethon import events, utils
|
||||||
from telethon.tl import types, functions
|
from telethon.tl import types, functions
|
||||||
|
|
||||||
|
|
||||||
# {name: (text, media)}
|
TYPE_TEXT = 0
|
||||||
|
TYPE_PHOTO = 1
|
||||||
|
TYPE_DOCUMENT = 2
|
||||||
|
|
||||||
|
|
||||||
|
# {name: {'text': text, 'id': id, 'hash': access_hash, 'type': type}}
|
||||||
snips = storage.snips or {}
|
snips = storage.snips or {}
|
||||||
|
|
||||||
|
|
||||||
def remove_snip(name):
|
|
||||||
if name in snips:
|
|
||||||
text, media = snips[name]
|
|
||||||
if media:
|
|
||||||
try:
|
|
||||||
os.remove(text)
|
|
||||||
except Exception as e:
|
|
||||||
print('failed to remove', snip, 'due to', e, file=sys.stderr)
|
|
||||||
del snips[name]
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'.snip (\w+)'))
|
@borg.on(events.NewMessage(pattern=r'.snip (\w+)'))
|
||||||
async def on_snip(event):
|
async def on_snip(event):
|
||||||
msg = await event.reply_message
|
msg = await event.reply_message
|
||||||
name = event.pattern_match.group(1)
|
name = event.pattern_match.group(1)
|
||||||
if msg:
|
if msg:
|
||||||
remove_snip(name)
|
snips.pop(name, None)
|
||||||
|
snip = {'type': TYPE_TEXT, 'text': msg.message or ''}
|
||||||
if msg.media:
|
if msg.media:
|
||||||
file = await borg.download_media(
|
media = None
|
||||||
msg.media, os.path.join(storage._root, str(uuid.uuid4())))
|
if isinstance(msg.media, types.MessageMediaPhoto):
|
||||||
snips[name] = (file, True)
|
media = utils.get_input_photo(msg.media.photo)
|
||||||
else:
|
snip['type'] = TYPE_PHOTO
|
||||||
snips[name] = (msg.message, False)
|
elif isinstance(msg.media, types.MessageMediaDocument):
|
||||||
|
media = utils.get_input_document(msg.media.document)
|
||||||
|
snip['type'] = TYPE_DOCUMENT
|
||||||
|
if media:
|
||||||
|
snip['id'] = media.id
|
||||||
|
snip['hash'] = media.access_hash
|
||||||
|
|
||||||
|
snips[name] = snip
|
||||||
storage.snips = snips
|
storage.snips = snips
|
||||||
elif name in snips:
|
elif name in snips:
|
||||||
text, media = snips[name]
|
snip = snips[name]
|
||||||
if media:
|
if snip['type'] == TYPE_PHOTO:
|
||||||
await borg.send_file(await event.input_chat, text)
|
media = types.InputPhoto(snip['id'], snip['hash'])
|
||||||
|
elif snip['type'] == TYPE_DOCUMENT:
|
||||||
|
media = types.InputDocument(snip['id'], snip['hash'])
|
||||||
else:
|
else:
|
||||||
await borg.send_message(await event.input_chat, text)
|
media = None
|
||||||
|
|
||||||
|
await borg.send_message(
|
||||||
|
await event.input_chat, snip['text'], file=media)
|
||||||
|
|
||||||
await event.delete()
|
await event.delete()
|
||||||
|
|
||||||
|
@ -54,5 +61,5 @@ async def on_snip_list(event):
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'.snipd (\w+)'))
|
@borg.on(events.NewMessage(pattern=r'.snipd (\w+)'))
|
||||||
async def on_snip_delete(event):
|
async def on_snip_delete(event):
|
||||||
remove_snip(event.pattern_match.group(1))
|
snips.pop(event.pattern_match.group(1), None)
|
||||||
await event.delete()
|
await event.delete()
|
||||||
|
|
Loading…
Reference in New Issue