support sending gifs via rpc

This commit is contained in:
2024-02-29 21:30:05 +03:00
parent 49a1b62f21
commit 5d69af14d5
4 changed files with 1426 additions and 1188 deletions

View File

@@ -1,11 +1,13 @@
import base64
import json
import logging
import os
import tempfile
import traceback
from io import BytesIO
from uuid import uuid4
import ffmpeg
import imagehash
import requests
from PIL import Image
@@ -17,7 +19,7 @@ from jsonrpc import Dispatcher as RPCDispatcher
from djconfig import config
from bots.models import TelegramBotModuleConfig, BotUser
from bots.tasks import upload_image_rpc
from bots.tasks import upload_image_rpc, upload_animation_rpc
class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
@@ -70,6 +72,31 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
# QueuedItem.objects.create(config=self, type='photo', args=json.dumps([]), image_hash=image_hash, processed=True)
return True
def rpc_post_gif(self, url, note=''):
config._reload_maybe()
try:
fpath_input = os.path.join(tempfile.gettempdir(), '{}.gif'.format(uuid4()))
fpath_output = os.path.join(tempfile.gettempdir(), '{}.mp4'.format(uuid4()))
resp = requests.get(url)
resp.raise_for_status()
with open(fpath_input, 'wb') as f:
f.write(resp.content)
except:
raise RuntimeError('Could not load image')
video_input = ffmpeg \
.input(fpath_input)
cmd = video_input \
.output(fpath_output,
vf='pad=width=ceil(iw/2)*2:height=ceil(ih/2)*2:x=0:y=0:color=Black',
vcodec='libx264',
crf='26')
logging.info('ffmpeg ' + ' '.join(cmd.get_args()))
cmd.run()
qi = QueuedItem.objects.create(config=self, type='animation', args=json.dumps([]), message_id=None)
upload_animation_rpc.delay(qi.pk, fpath_output, note or None)
return True
def periodic_task(self, bot: Bot):
i = self.queued_items.filter(processed=False).order_by('?').first() # type: QueuedItem
if i: