misskey2telegram/defs/search_user.py

66 lines
1.8 KiB
Python
Raw Normal View History

2022-12-25 09:40:41 +00:00
from typing import Optional
from datetime import datetime, timedelta
from mipac import UserDetailed
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
2023-01-27 12:36:41 +00:00
from misskey_init import MisskeyBot
2022-12-25 09:40:41 +00:00
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,
2022-12-26 09:48:13 +00:00
user.api.action.get_profile_link(),
2022-12-25 09:40:41 +00:00
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 = [
2022-12-26 09:48:13 +00:00
InlineKeyboardButton(text="Link", url=user.api.action.get_profile_link()),
2022-12-25 09:40:41 +00:00
]
second_line = [
InlineKeyboardButton(
text="" if user.is_followed else "",
callback_data=f"follow:{user.id}",
),
]
return InlineKeyboardMarkup([first_line, second_line])
2023-01-27 12:36:41 +00:00
async def search_user(misskey_bot: MisskeyBot, username: str, host: str = None) -> Optional[UserDetailed]:
2022-12-25 09:40:41 +00:00
"""
搜索用户
"""
if host:
2023-01-27 12:36:41 +00:00
users = await misskey_bot.core.api.tg_user.action.search_by_username_and_host(username, host, limit=1)
2022-12-25 09:40:41 +00:00
else:
users = []
2023-01-27 12:36:41 +00:00
async for user in misskey_bot.core.api.tg_user.action.search(username, limit=1):
2022-12-25 09:40:41 +00:00
users.append(user)
return users[0] if users else None