misskey2telegram/defs/search_user.py
2023-01-27 20:36:41 +08:00

66 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Optional
from datetime import datetime, timedelta
from mipac import UserDetailed
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from misskey_init import MisskeyBot
template = """<b>Misskey User Info</b>
Name: %s
Username: <a href="%s">@%s</a>
Bio: <code>%s</code>
Joined: <code>%s</code>
Updated: <code>%s</code>
📤 %s 粉丝 %s 关注 %s"""
def gen_text(user: UserDetailed):
def parse_time(time: str) -> str:
if not time:
return "Unknown"
time = datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%fZ") + timedelta(hours=8)
return time.strftime("%Y-%m-%d %H:%M:%S")
create_at = parse_time(user.created_at)
update_at = parse_time(user.updated_at)
return template % (
user.nickname,
user.api.action.get_profile_link(),
user.username,
user.description or "",
create_at,
update_at,
user.notes_count,
user.followers_count,
user.following_count,
)
def gen_button(user: UserDetailed):
first_line = [
InlineKeyboardButton(text="Link", url=user.api.action.get_profile_link()),
]
second_line = [
InlineKeyboardButton(
text="" if user.is_followed else "",
callback_data=f"follow:{user.id}",
),
]
return InlineKeyboardMarkup([first_line, second_line])
async def search_user(misskey_bot: MisskeyBot, username: str, host: str = None) -> Optional[UserDetailed]:
"""
搜索用户
"""
if host:
users = await misskey_bot.core.api.tg_user.action.search_by_username_and_host(username, host, limit=1)
else:
users = []
async for user in misskey_bot.core.api.tg_user.action.search(username, limit=1):
users.append(user)
return users[0] if users else None