fix: video sticker

This commit is contained in:
xtaodada 2024-03-07 18:52:58 +08:00
parent 58379c3493
commit d4109d32ed
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 20 additions and 11 deletions

View File

@ -79,7 +79,7 @@ async def converter(src_file: Union[Path, str]) -> Path:
src_file.unlink(missing_ok=True) src_file.unlink(missing_ok=True)
else: else:
logs.error("转换 %s -> %s 时出错: %s", src_file.name, target_file.name, stderr.decode("utf-8")) logs.error("转换 %s -> %s 时出错: %s", src_file.name, target_file.name, stderr.decode("utf-8"))
raise ValueError raise ValueError("转换 %s -> %s 时出错" % (src_file.name, target_file.name))
return target_file return target_file
@ -162,14 +162,12 @@ async def get_from_sticker_set(short_name: str, uid: int, client: "Client", repl
async def get_from_sticker(client: "Client", message: "Message") -> Path: async def get_from_sticker(client: "Client", message: "Message") -> Path:
sticker_path = temp_path / f"{message.sticker.file_unique_id}.webp" sticker_path = await client.download_media(message)
await client.download_media(message, file_name=sticker_path.as_posix())
return await converter(sticker_path) return await converter(sticker_path)
async def get_from_custom_emoji(client: "Client", sticker: "Sticker") -> Path: async def get_from_custom_emoji(client: "Client", sticker: "Sticker") -> Path:
sticker_path = temp_path / f"{sticker.file_unique_id}.webp" sticker_path = await client.download_media(sticker.file_id)
await client.download_media(sticker.file_id, file_name=sticker_path.as_posix())
return await converter(sticker_path) return await converter(sticker_path)

View File

@ -43,8 +43,11 @@ async def process_sticker_set(client: "Client", message: "Message"):
async def process_single_sticker(client: "Client", message: "Message"): async def process_single_sticker(client: "Client", message: "Message"):
await message.reply_chat_action(ChatAction.TYPING) await message.reply_chat_action(ChatAction.TYPING)
if temp := await cache.get(f"sticker:export:{message.from_user.id}"): if temp := await cache.get(f"sticker:export:{message.from_user.id}"):
try:
await export_add(temp, message.sticker, client) await export_add(temp, message.sticker, client)
await message.reply_text("成功加入导出列表,结束选择请输入 /sticker_export_end", quote=True) await message.reply_text("成功加入导出列表,结束选择请输入 /sticker_export_end", quote=True)
except ValueError as exc:
await message.reply(str(exc), quote=True)
else: else:
reply = await message.reply("正在转换贴纸...请耐心等待", quote=True) reply = await message.reply("正在转换贴纸...请耐心等待", quote=True)
target_file = None target_file = None
@ -52,11 +55,13 @@ async def process_single_sticker(client: "Client", message: "Message"):
target_file = await get_from_sticker(client, message) target_file = await get_from_sticker(client, message)
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT) await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT)
await message.reply_document(target_file.as_posix(), quote=True) await message.reply_document(target_file.as_posix(), quote=True)
with contextlib.suppress(Exception):
await reply.delete()
except ValueError as exc:
await reply.edit(str(exc))
finally: finally:
if target_file: if target_file:
target_file.unlink(missing_ok=True) target_file.unlink(missing_ok=True)
with contextlib.suppress(Exception):
await reply.delete()
raise ContinuePropagation raise ContinuePropagation
@ -70,15 +75,21 @@ async def process_custom_emoji(client: "Client", message: "Message"):
await message.reply("无法获取贴纸", quote=True) await message.reply("无法获取贴纸", quote=True)
raise ContinuePropagation raise ContinuePropagation
reply = await message.reply(f"正在下载 {len(stickers)} 个 emoji ...请耐心等待", quote=True) reply = await message.reply(f"正在下载 {len(stickers)} 个 emoji ...请耐心等待", quote=True)
exc = None
for sticker in stickers: for sticker in stickers:
target_file = None target_file = None
try: try:
target_file = await get_from_custom_emoji(client, sticker) target_file = await get_from_custom_emoji(client, sticker)
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT) await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT)
await message.reply_document(target_file.as_posix(), quote=True) await message.reply_document(target_file.as_posix(), quote=True)
except ValueError as exc_:
exc = exc_
finally: finally:
if target_file: if target_file:
target_file.unlink(missing_ok=True) target_file.unlink(missing_ok=True)
if exc:
await reply.edit(str(exc))
else:
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
await reply.delete() await reply.delete()
raise ContinuePropagation raise ContinuePropagation