main
 1#!/usr/bin/env python
 2# -*- coding: utf-8 -*-
 3import asyncio
 4import contextlib
 5import random
 6import re
 7
 8from glom import glom
 9from loguru import logger
10from pyrogram.client import Client
11from pyrogram.types import Message
12
13from config import TZ
14from custom.config import ACCOUNT_NAME, GROUP_JSQ, USER_BENNY
15from messages.utils import delete_message
16from utils import nowdt, strings_list
17
18EMBY_SKIP_REG_BOT = "lilyembybot"
19
20
21async def emby_register(client: Client, message: Message) -> None:
22    if not glom(message, "from_user.is_bot", default=False):
23        return
24    bot_name = glom(message, "from_user.username", default="")
25    # start register, send /start to bot
26    markup_text = glom(message, "reply_markup.inline_keyboard.0.0.text", default="")
27    markup_url = glom(message, "reply_markup.inline_keyboard.0.0.url", default="")
28    if ("自由注册" in message.content or "定时注册" in message.content) and markup_text == "👆🏻 点击注册" and markup_url:
29        reg_bot = markup_url.split("/")[-1]
30        if not reg_bot.endswith("bot"):
31            return
32        allow_reg_count = 0
33        if count_matched := re.search(r"剩余可注册 \| (\d+)", str(message.content)):
34            allow_reg_count = int(count_matched.group(1))
35            if allow_reg_count <= 0:
36                return
37            logger.warning(f"【Emby】{reg_bot} 已开启注册!")
38            if bot_name not in strings_list(EMBY_SKIP_REG_BOT) and allow_reg_count >= 5 and nowdt(tz=TZ).hour >= 8:  # 至少 5 个名额
39                await client.send_message(reg_bot, "/start")
40            if ACCOUNT_NAME == "xiaohao":
41                fwd_message = await message.forward(GROUP_JSQ)
42                # get continue duration
43                duration_seconds = 1200  # default 20 min
44                if duration_matched := re.search(r"⏳ 可持续时间 \| (\d+) min", message.content):
45                    duration_seconds = int(duration_matched.group(1)) * 60
46                duration_seconds = min(duration_seconds, allow_reg_count * 5)  # 预估每个注册名额最多够 5s
47                duration_seconds = min(duration_seconds, 1800)  # 最多 30 min
48                duration_seconds = max(duration_seconds, 300)  # 最少 5 min
49                await asyncio.sleep(duration_seconds)
50                await delete_message(fwd_message)  # type: ignore
51
52    # join group and channel
53    if ACCOUNT_NAME == "xiaohao" and str(message.content).startswith("💢 拜托啦"):
54        urls = glom(message, "reply_markup.inline_keyboard.**.url", default=[])
55        urls = {x for x in urls if str(x).startswith(("t.me/", "https://t.me/", "http://t.me/"))}
56        for url in urls:
57            with contextlib.suppress(Exception):
58                await client.join_chat(url.replace("http://", "https://"))
59
60    # init account
61    if "重新召唤面板" in message.content:
62        await client.send_message(message.chat.id, "/start")
63
64    # click `create` button
65    callback_data = glom(message, "reply_markup.inline_keyboard.**.callback_data", default=[])
66    if "create" in callback_data and bot_name not in {"sntp_lite_emby_bot", "shrekpublic_bot"}:
67        logger.warning(f"【Emby】{glom(message, 'from_user.username', default='')} 正在创建账户!")
68        resp = await client.request_callback_answer(message.chat.id, message.id, callback_data="create")
69        logger.warning(f"【Emby】{glom(resp, 'message', default='')}")
70
71    # set account credentials (only for bot)
72    if glom(message, "chat.type.name", default="") != "BOT":
73        return
74    if "您已进入注册状态" in str(message.content):
75        username = fakeuser()
76        safecode = random.randint(1000, 9999)
77        notice = f"【Emby】@{bot_name} 正在设置账户: {username} [{safecode}]"
78        logger.warning(notice)
79        await client.send_message(message.from_user.id, f"{username} {safecode}")
80        await client.send_message(USER_BENNY, notice)
81
82    if "创建用户成功" in str(message.content):
83        await message.forward(USER_BENNY)
84
85
86def fakeuser() -> str:
87    """生成一个随机的用户名, 格式为1个随机的声母 + 2个字母 + 4位年份数字."""
88    shengmu = "bpmfdtnlgkhjqxrzcsyw"
89    letters = "abcdefghijklmnopqrstuvwxyz"
90    return random.choice(shengmu) + "".join(random.choices(letters, k=2)) + str(random.randint(1980, 1999))