txlyre 2 жил өмнө
parent
commit
9ebf7713aa
2 өөрчлөгдсөн 33 нэмэгдсэн , 20 устгасан
  1. 22 20
      commands.py
  2. 11 0
      utils.py

+ 22 - 20
commands.py

@@ -8,6 +8,7 @@ from ujson import dumps
 from tortoise.exceptions import IntegrityError
 from telethon.utils import get_display_name, get_peer_id
 from aiofiles.os import remove
+from aiofiles.os.path import isfile
 from emoji import is_emoji
 
 from actions import (
@@ -24,6 +25,7 @@ from actions import (
 from utils import (
   WORDS_TABLE,
   make_temporary_filename,
+  make_cache_filename,
   parse_kind,
   get_user_name,
   get_word_for,
@@ -154,29 +156,27 @@ async def addgif_handler(bot, event, command):
 
 # Very, very, VERY evil code...
 async def make_message_shot(bot, message):
-  proc = await create_subprocess_shell(
-    './makeshot/makeshot',
-    stdin=PIPE
-  )
-
-  output_path = make_temporary_filename('png')
-  avatar_path = make_temporary_filename('png')
-
   if message.sender is None:
-    await bot.download_profile_photo(message.peer_id.channel_id, file=avatar_path)
+    sender_id = message.peer_id.channel_id
 
-    full_name = await bot.get_entity(message.peer_id.channel_id)
+    full_name = await bot.get_entity(sender_id)
     full_name = full_name.title
   else:
-    await bot.download_profile_photo(message.sender, file=avatar_path)
+    sender_id = message.sender
+ 
+    full_name = get_display_name(sender_id)
 
-    full_name = get_display_name(message.sender)
+  output_path = make_temporary_filename('png')
+  avatar_path = await make_cache_filename(sender_id, 'png')
 
-  # TO-DO: make it better.
-  mproc = await create_subprocess_shell(
-    f'mogrify -format png {avatar_path}'
-  )
-  await mproc.communicate()
+  if not await isfile(avatar_path):
+    await bot.download_profile_photo(sender_id, file=avatar_path)
+
+    # TO-DO: make it better.
+    mproc = await create_subprocess_shell(
+      f'mogrify -format png {avatar_path}'
+    )
+    await mproc.communicate()
 
   data = bytes()
 
@@ -195,9 +195,11 @@ async def make_message_shot(bot, message):
   data += pack('I', len(text))
   data += text
 
-  await proc.communicate(input=data) 
-
-  await remove(avatar_path)
+  proc = await create_subprocess_shell(
+    './makeshot/makeshot',
+    stdin=PIPE
+  )
+  await proc.communicate(input=data)
 
   pproc = await create_subprocess_shell(
     f'pngcrush -reduce -brute -ow {output_path} {output_path}'

+ 11 - 0
utils.py

@@ -3,6 +3,9 @@ from enum import IntEnum
 from datetime import datetime
 from collections import namedtuple
 
+from aiofiles.os import mkdir
+from aiofiles.os.path import isdir
+
 from telethon.utils import get_display_name
 from telethon.tl.types import (
   MessageEntityBold,
@@ -95,6 +98,14 @@ def make_temporary_filename(ext):
   uid = uuid4().hex
 
   return f'tmp_{uid}.{ext}'
+  
+CACHE_DIR = './cache'
+
+async def make_cache_filename(id, ext):
+  if not await isdir(CACHE_DIR):
+    await mkdir(CACHE_DIR)
+
+  return f'{CACHE_DIR}/{id}.{ext}'
 
 def parse_kind(kind):
   kind = int(kind)