misskey2telegram/defs/misskey.py

291 lines
9.2 KiB
Python
Raw Normal View History

2022-12-22 14:18:43 +00:00
from datetime import datetime, timedelta, timezone
from typing import Optional
from mipac import Note
from mipac.models.lite import LiteUser
from mipac.types import IDriveFile
from pyrogram.enums import ParseMode
from pyrogram.errors import MediaEmpty
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InputMediaPhoto, InputMediaVideo, \
InputMediaDocument, InputMediaAudio
from glover import misskey_host
from init import bot, request
from scheduler import add_delete_file_job, delete_file
def get_note_url(note: Note) -> str:
return note.url or f"https://{misskey_host}/notes/{note.id}"
def gen_button(note: Note, author: str):
source = get_note_url(note)
reply_source = get_note_url(note.reply) if note.reply else None
if reply_source:
2022-12-24 08:31:16 +00:00
first_line = [
2022-12-22 14:18:43 +00:00
InlineKeyboardButton(text="Source", url=source),
InlineKeyboardButton(text="RSource", url=reply_source),
2022-12-24 08:31:16 +00:00
InlineKeyboardButton(text="Author", url=author),
]
2022-12-22 14:18:43 +00:00
else:
2022-12-24 08:31:16 +00:00
first_line = [
2022-12-22 14:18:43 +00:00
InlineKeyboardButton(text="Source", url=source),
2022-12-24 08:31:16 +00:00
InlineKeyboardButton(text="Author", url=author),
]
second_line = [
InlineKeyboardButton(text="🔁", callback_data=f"renote:{note.id}"),
InlineKeyboardButton(text="❤️", callback_data=f"react:{note.id}:love"),
2022-12-25 09:40:41 +00:00
InlineKeyboardButton(text="🌐", callback_data=f"translate:{note.id}"),
2022-12-24 08:31:16 +00:00
]
return InlineKeyboardMarkup([first_line, second_line])
2022-12-22 14:18:43 +00:00
def get_user_link(user: LiteUser) -> str:
if user.host:
return f"https://{user.host}/@{user.username}"
return f"https://{misskey_host}/@{user.username}"
def get_post_time(date: datetime) -> str:
try:
date = date + timedelta(hours=8)
return date.strftime("%Y-%m-%d %H:%M:%S")
except Exception:
return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
def get_content(note: Note) -> str:
2022-12-22 15:01:53 +00:00
content = note.content or ""
action = "发表"
origin = ""
show_note = note
if note.renote:
show_note = note.renote
action = "转推"
content = note.renote.content or content
origin = f"\n<a href=\"{get_user_link(note.renote.author)}\">{note.renote.author.nickname}</a> " \
f"发表于 {get_post_time(note.renote.created_at)}"
2022-12-24 08:31:16 +00:00
content = content[:768]
2022-12-22 14:18:43 +00:00
return f"""<b>Misskey Timeline Update</b>
2022-12-22 15:01:53 +00:00
<code>{content}</code>
2022-12-22 14:18:43 +00:00
2022-12-22 15:01:53 +00:00
<a href=\"{get_user_link(note.author)}\">{note.author.nickname}</a> {action}{get_post_time(note.created_at)}{origin}
2023-01-17 08:36:41 +00:00
点赞: {sum(show_note.reactions.values())} | 回复: {show_note.replies_count} | 转发: {show_note.renote_count}"""
2022-12-22 14:18:43 +00:00
2023-01-17 03:26:10 +00:00
async def send_text(cid: int, note: Note, reply_to_message_id: int):
2022-12-22 14:18:43 +00:00
await bot.send_message(
cid,
get_content(note),
2022-12-24 08:31:16 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
reply_markup=gen_button(note, get_user_link(note.author)),
disable_web_page_preview=True,
)
def deprecated_to_text(func):
async def wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except MediaEmpty:
2023-01-17 03:26:10 +00:00
return await send_text(args[0], args[2], args[3])
2022-12-22 14:18:43 +00:00
return wrapper
@deprecated_to_text
2023-01-17 03:26:10 +00:00
async def send_photo(cid: int, url: str, note: Note, reply_to_message_id: int):
2022-12-22 14:18:43 +00:00
if not url:
2023-01-17 03:26:10 +00:00
return await send_text(cid, note, reply_to_message_id)
2022-12-22 14:18:43 +00:00
await bot.send_photo(
cid,
url,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
caption=get_content(note),
reply_markup=gen_button(note, get_user_link(note.author)),
)
@deprecated_to_text
2023-01-17 03:26:10 +00:00
async def send_video(cid: int, url: str, note: Note, reply_to_message_id: int):
2022-12-22 14:18:43 +00:00
if not url:
2023-01-17 03:26:10 +00:00
return await send_text(cid, note, reply_to_message_id)
2022-12-22 14:18:43 +00:00
await bot.send_video(
cid,
url,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
caption=get_content(note),
reply_markup=gen_button(note, get_user_link(note.author)),
)
@deprecated_to_text
2023-01-17 03:26:10 +00:00
async def send_audio(cid: int, url: str, note: Note, reply_to_message_id: int):
2022-12-22 14:18:43 +00:00
if not url:
2023-01-17 03:26:10 +00:00
return await send_text(cid, note, reply_to_message_id)
2022-12-22 14:18:43 +00:00
await bot.send_audio(
cid,
url,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
caption=get_content(note),
reply_markup=gen_button(note, get_user_link(note.author)),
)
async def fetch_document(file: IDriveFile) -> Optional[str]:
file_name = "downloads/" + file.get("name", "file")
file_url = file.get("url", None)
if file.get("size", 0) > 10 * 1024 * 1024:
return file_url
if not file_url:
return file_url
req = await request.get(file_url)
if req.status_code != 200:
return file_url
with open(file_name, "wb") as f:
f.write(req.content)
add_delete_file_job(file_name)
return file_name
@deprecated_to_text
2023-01-17 03:26:10 +00:00
async def send_document(cid: int, file: IDriveFile, note: Note, reply_to_message_id: int):
2022-12-22 14:18:43 +00:00
file = await fetch_document(file)
if not file:
2023-01-17 03:26:10 +00:00
return await send_text(cid, note, reply_to_message_id)
2022-12-22 14:18:43 +00:00
await bot.send_document(
cid,
file,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
caption=get_content(note),
reply_markup=gen_button(note, get_user_link(note.author)),
)
await delete_file(file)
2022-12-24 08:31:16 +00:00
async def get_media_group(files: list[IDriveFile]) -> list:
2022-12-22 14:18:43 +00:00
media_lists = []
2022-12-24 08:31:16 +00:00
for file_ in files:
file_url = file_.get("url", None)
2022-12-22 14:18:43 +00:00
if not file_url:
continue
2022-12-24 08:31:16 +00:00
file_type = file_.get("type", "")
2022-12-22 14:18:43 +00:00
if file_type.startswith("image"):
media_lists.append(
InputMediaPhoto(
file_url,
parse_mode=ParseMode.HTML,
)
)
elif file_type.startswith("video"):
media_lists.append(
InputMediaVideo(
file_url,
parse_mode=ParseMode.HTML,
)
)
elif file_type.startswith("audio"):
media_lists.append(
InputMediaAudio(
file_url,
parse_mode=ParseMode.HTML,
)
)
2022-12-24 08:31:16 +00:00
elif file := await fetch_document(file_):
2022-12-22 14:18:43 +00:00
media_lists.append(
InputMediaDocument(
file,
parse_mode=ParseMode.HTML,
)
)
return media_lists
2023-01-17 03:26:10 +00:00
async def send_group(cid: int, files: list[IDriveFile], note: Note, reply_to_message_id: int):
2022-12-24 08:31:16 +00:00
groups = await get_media_group(files)
2022-12-22 14:18:43 +00:00
if len(groups) == 0:
2023-01-17 03:26:10 +00:00
return await send_text(cid, note, reply_to_message_id)
2022-12-24 08:31:16 +00:00
photo, video, audio, document, msg = [], [], [], [], None
2022-12-22 14:18:43 +00:00
for i in groups:
if isinstance(i, InputMediaPhoto):
photo.append(i)
elif isinstance(i, InputMediaVideo):
video.append(i)
elif isinstance(i, InputMediaAudio):
audio.append(i)
elif isinstance(i, InputMediaDocument):
document.append(i)
if video and (audio or document):
2022-12-24 08:31:16 +00:00
msg = await bot.send_media_group(
2022-12-22 14:18:43 +00:00
cid,
video,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
)
if audio:
2022-12-24 08:31:16 +00:00
msg = await bot.send_media_group(
2022-12-22 14:18:43 +00:00
cid,
audio,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
)
elif document:
2022-12-24 08:31:16 +00:00
msg = await bot.send_media_group(
2022-12-22 14:18:43 +00:00
cid,
document,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
)
elif audio and (photo or document):
2022-12-24 08:31:16 +00:00
msg = await bot.send_media_group(
2022-12-22 14:18:43 +00:00
cid,
audio,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
)
if photo:
2022-12-24 08:31:16 +00:00
msg = await bot.send_media_group(
2022-12-22 14:18:43 +00:00
cid,
photo,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
)
elif document:
2022-12-24 08:31:16 +00:00
msg = await bot.send_media_group(
2022-12-22 14:18:43 +00:00
cid,
document,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
)
else:
2022-12-24 08:31:16 +00:00
msg = await bot.send_media_group(
2022-12-22 14:18:43 +00:00
cid,
groups,
2023-01-17 03:26:10 +00:00
reply_to_message_id=reply_to_message_id,
2022-12-22 14:18:43 +00:00
)
2022-12-24 08:31:16 +00:00
if msg and isinstance(msg, list):
msg = msg[0]
await send_text(cid, note, msg.id if msg else None)
2022-12-22 14:18:43 +00:00
2023-01-17 17:11:26 +00:00
async def send_update(cid: int, note: Note, topic_id: int):
2022-12-22 14:18:43 +00:00
files = list(note.files)
if note.reply:
files.extend(iter(note.reply.files))
2022-12-22 15:01:53 +00:00
if note.renote:
files.extend(iter(note.renote.files))
files = list({f.get("id"): f for f in files}.values())
2022-12-22 14:18:43 +00:00
match len(files):
case 0:
2023-01-17 03:26:10 +00:00
await send_text(cid, note, topic_id)
2022-12-22 14:18:43 +00:00
case 1:
file = files[0]
file_url = file.get("url", None)
file_type = file.get("type", "")
if file_type.startswith("image"):
2023-01-17 03:26:10 +00:00
await send_photo(cid, file_url, note, topic_id)
2022-12-22 14:18:43 +00:00
elif file_type.startswith("video"):
2023-01-17 03:26:10 +00:00
await send_video(cid, file_url, note, topic_id)
2022-12-22 14:18:43 +00:00
elif file_type.startswith("audio"):
2023-01-17 03:26:10 +00:00
await send_audio(cid, file_url, note, topic_id)
2022-12-22 14:18:43 +00:00
else:
2023-01-17 03:26:10 +00:00
await send_document(cid, file, note, topic_id)
2022-12-22 14:18:43 +00:00
case _:
2023-01-17 03:26:10 +00:00
await send_group(cid, files, note, topic_id)