mirror of
https://github.com/Xtao-Labs/misskey2telegram.git
synced 2024-11-29 07:57:03 +00:00
fix: mult file send
This commit is contained in:
parent
d2490de6c1
commit
3c560c934b
@ -1,6 +1,6 @@
|
|||||||
from httpx import URL, InvalidURL
|
from httpx import URL, InvalidURL, AsyncClient
|
||||||
|
|
||||||
from init import request
|
from init import headers
|
||||||
|
|
||||||
|
|
||||||
def get_host(url: str) -> str:
|
def get_host(url: str) -> str:
|
||||||
@ -15,6 +15,7 @@ async def check_host(host: str) -> bool:
|
|||||||
if not host:
|
if not host:
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
|
async with AsyncClient(timeout=60, headers=headers) as request:
|
||||||
req = await request.get(f"https://{host}/.well-known/nodeinfo")
|
req = await request.get(f"https://{host}/.well-known/nodeinfo")
|
||||||
req.raise_for_status()
|
req.raise_for_status()
|
||||||
node_url = req.json()["links"][0]["href"]
|
node_url = req.json()["links"][0]["href"]
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
import contextlib
|
import contextlib
|
||||||
import re
|
import re
|
||||||
|
from asyncio import sleep
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
import aiofiles as aiofiles
|
import aiofiles as aiofiles
|
||||||
|
from httpx import AsyncClient
|
||||||
from mipac import Note, File
|
from mipac import Note, File
|
||||||
from mipac.models.lite import LiteUser
|
from mipac.models.lite import LiteUser
|
||||||
from pyrogram.enums import ParseMode
|
from pyrogram.enums import ParseMode
|
||||||
from pyrogram.errors import MediaEmpty
|
from pyrogram.errors import MediaEmpty, FloodWait
|
||||||
from pyrogram.types import (
|
from pyrogram.types import (
|
||||||
InlineKeyboardMarkup,
|
InlineKeyboardMarkup,
|
||||||
InlineKeyboardButton,
|
InlineKeyboardButton,
|
||||||
@ -19,7 +21,7 @@ from pyrogram.types import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from defs.image import webp_to_png
|
from defs.image import webp_to_png
|
||||||
from init import bot, request
|
from init import bot, logs, headers
|
||||||
from models.services.scheduler import add_delete_file_job, delete_file
|
from models.services.scheduler import add_delete_file_job, delete_file
|
||||||
|
|
||||||
at_parse = re.compile(r"(?<!\S)@(\S+)\s")
|
at_parse = re.compile(r"(?<!\S)@(\S+)\s")
|
||||||
@ -118,6 +120,18 @@ def get_content(host: str, note: Note) -> str:
|
|||||||
点赞: {sum(show_note.reactions.values())} | 回复: {show_note.replies_count} | 转发: {show_note.renote_count}"""
|
点赞: {sum(show_note.reactions.values())} | 回复: {show_note.replies_count} | 转发: {show_note.renote_count}"""
|
||||||
|
|
||||||
|
|
||||||
|
def retry(func):
|
||||||
|
async def wrapper(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
return await func(*args, **kwargs)
|
||||||
|
except FloodWait as e:
|
||||||
|
await sleep(e.value + 1)
|
||||||
|
return await func(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@retry
|
||||||
async def send_text(
|
async def send_text(
|
||||||
host: str, cid: int, note: Note, reply_to_message_id: int, show_second: bool
|
host: str, cid: int, note: Note, reply_to_message_id: int, show_second: bool
|
||||||
) -> Message:
|
) -> Message:
|
||||||
@ -149,6 +163,8 @@ async def fetch_document(file: File) -> Optional[str]:
|
|||||||
return file_url
|
return file_url
|
||||||
if not file_url:
|
if not file_url:
|
||||||
return file_url
|
return file_url
|
||||||
|
logs.info(f"下载远程文件:{file_url}")
|
||||||
|
async with AsyncClient(timeout=60.0, headers=headers) as request:
|
||||||
req = await request.get(file_url)
|
req = await request.get(file_url)
|
||||||
if req.status_code != 200:
|
if req.status_code != 200:
|
||||||
return file_url
|
return file_url
|
||||||
@ -163,6 +179,7 @@ async def fetch_document(file: File) -> Optional[str]:
|
|||||||
return file_name
|
return file_name
|
||||||
|
|
||||||
|
|
||||||
|
@retry
|
||||||
@deprecated_to_text
|
@deprecated_to_text
|
||||||
async def send_photo(
|
async def send_photo(
|
||||||
host: str,
|
host: str,
|
||||||
@ -185,6 +202,7 @@ async def send_photo(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@retry
|
||||||
@deprecated_to_text
|
@deprecated_to_text
|
||||||
async def send_video(
|
async def send_video(
|
||||||
host: str,
|
host: str,
|
||||||
@ -207,6 +225,7 @@ async def send_video(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@retry
|
||||||
@deprecated_to_text
|
@deprecated_to_text
|
||||||
async def send_audio(
|
async def send_audio(
|
||||||
host: str,
|
host: str,
|
||||||
@ -229,6 +248,7 @@ async def send_audio(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@retry
|
||||||
@deprecated_to_text
|
@deprecated_to_text
|
||||||
async def send_document(
|
async def send_document(
|
||||||
host: str,
|
host: str,
|
||||||
@ -257,7 +277,7 @@ async def send_document(
|
|||||||
async def get_media_group(files: list[File]) -> list:
|
async def get_media_group(files: list[File]) -> list:
|
||||||
media_lists = []
|
media_lists = []
|
||||||
for file_ in files:
|
for file_ in files:
|
||||||
file_url = file_.url
|
file_url = await fetch_document(file_)
|
||||||
if not file_url:
|
if not file_url:
|
||||||
continue
|
continue
|
||||||
file_type = file_.type
|
file_type = file_.type
|
||||||
@ -292,6 +312,20 @@ async def get_media_group(files: list[File]) -> list:
|
|||||||
return media_lists
|
return media_lists
|
||||||
|
|
||||||
|
|
||||||
|
@retry
|
||||||
|
async def send_media_group(cid: int, groups: list):
|
||||||
|
return await bot.send_media_group(cid, groups)
|
||||||
|
|
||||||
|
|
||||||
|
async def send_group_msg(cid: int, groups: list):
|
||||||
|
msgs = []
|
||||||
|
for i in range(0, len(groups), 10):
|
||||||
|
msg = await send_media_group(cid, groups[i : i + 10])
|
||||||
|
msgs.extend(msg)
|
||||||
|
return msgs
|
||||||
|
|
||||||
|
|
||||||
|
@deprecated_to_text
|
||||||
async def send_group(
|
async def send_group(
|
||||||
host: str,
|
host: str,
|
||||||
cid: int,
|
cid: int,
|
||||||
@ -303,7 +337,7 @@ async def send_group(
|
|||||||
groups = await get_media_group(files)
|
groups = await get_media_group(files)
|
||||||
if len(groups) == 0:
|
if len(groups) == 0:
|
||||||
return [await send_text(host, cid, note, reply_to_message_id, show_second)]
|
return [await send_text(host, cid, note, reply_to_message_id, show_second)]
|
||||||
photo, video, audio, document, msg = [], [], [], [], None
|
photo, video, audio, document, msg_ids = [], [], [], [], []
|
||||||
for i in groups:
|
for i in groups:
|
||||||
if isinstance(i, InputMediaPhoto):
|
if isinstance(i, InputMediaPhoto):
|
||||||
photo.append(i)
|
photo.append(i)
|
||||||
@ -313,54 +347,8 @@ async def send_group(
|
|||||||
audio.append(i)
|
audio.append(i)
|
||||||
elif isinstance(i, InputMediaDocument):
|
elif isinstance(i, InputMediaDocument):
|
||||||
document.append(i)
|
document.append(i)
|
||||||
if video and (audio or document):
|
for i in (photo, video, audio, document):
|
||||||
msg = await bot.send_media_group(
|
msg_ids.extend(await send_group_msg(cid, i))
|
||||||
cid,
|
|
||||||
video,
|
|
||||||
reply_to_message_id=reply_to_message_id,
|
|
||||||
)
|
|
||||||
if audio:
|
|
||||||
msg = await bot.send_media_group(
|
|
||||||
cid,
|
|
||||||
audio,
|
|
||||||
reply_to_message_id=reply_to_message_id,
|
|
||||||
)
|
|
||||||
elif document:
|
|
||||||
msg = await bot.send_media_group(
|
|
||||||
cid,
|
|
||||||
document,
|
|
||||||
reply_to_message_id=reply_to_message_id,
|
|
||||||
)
|
|
||||||
elif audio and (photo or document):
|
|
||||||
msg = await bot.send_media_group(
|
|
||||||
cid,
|
|
||||||
audio,
|
|
||||||
reply_to_message_id=reply_to_message_id,
|
|
||||||
)
|
|
||||||
if photo:
|
|
||||||
msg = await bot.send_media_group(
|
|
||||||
cid,
|
|
||||||
photo,
|
|
||||||
reply_to_message_id=reply_to_message_id,
|
|
||||||
)
|
|
||||||
elif document:
|
|
||||||
msg = await bot.send_media_group(
|
|
||||||
cid,
|
|
||||||
document,
|
|
||||||
reply_to_message_id=reply_to_message_id,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
msg = await bot.send_media_group(
|
|
||||||
cid,
|
|
||||||
groups,
|
|
||||||
reply_to_message_id=reply_to_message_id,
|
|
||||||
)
|
|
||||||
if msg and isinstance(msg, list):
|
|
||||||
msg_ids = msg
|
|
||||||
elif msg:
|
|
||||||
msg_ids = [msg]
|
|
||||||
else:
|
|
||||||
msg_ids = []
|
|
||||||
tmsg = await send_text(
|
tmsg = await send_text(
|
||||||
host, cid, note, msg_ids[0].id if msg_ids else None, show_second
|
host, cid, note, msg_ids[0].id if msg_ids else None, show_second
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user