support sending gifs via rpc

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

View File

@ -44,6 +44,7 @@ vkwave = "*"
imagehash = "*"
tqdm = "*"
tiktokapi = "*"
ffmpeg-python = "*"
[dev-packages]

2551
Pipfile.lock generated

File diff suppressed because it is too large Load Diff

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:

View File

@ -3,6 +3,7 @@ import logging
import os
import traceback
from telegram import Bot
from telegram.error import RetryAfter
from config.celery import app
@ -39,3 +40,35 @@ def upload_image_rpc(self, queued_image_pk, fpath, caption):
except Exception as e:
traceback.print_exc()
raise self.retry(exc=e, countdown=5)
@app.task(bind=True)
def upload_animation_rpc(self, queued_image_pk, fpath, caption):
from .modules.channel_helper import QueuedItem
try:
logging.warning(f'Processing upload task for queued animation item #{queued_image_pk}')
qi = QueuedItem.objects.get(pk=queued_image_pk)
bot = qi.config.bot.get_bot()
if qi.config.queued:
m = bot.send_animation(
config.tmp_uploads_chat_id,
open(fpath, 'rb'),
caption=caption,
)
qi.message_id = m.message_id
qi.args = json.dumps([m.animation.file_id])
qi.save()
else:
bot.send_animation(
qi.config.chat_id,
open(fpath, 'rb'),
caption=caption,
)
qi.processed = True
qi.save()
os.unlink(fpath)
except RetryAfter as e:
raise self.retry(exc=e, countdown=int(e.retry_after))
except Exception as e:
traceback.print_exc()
raise self.retry(exc=e, countdown=5)