2022-08-06 06:36:44 +00:00
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
from aiohttp import ClientConnectorError
|
2022-10-30 13:35:53 +00:00
|
|
|
|
from genshin import GenshinException, AlreadyClaimed, InvalidCookies
|
2022-10-12 03:45:06 +00:00
|
|
|
|
from httpx import TimeoutException
|
2022-08-06 07:37:43 +00:00
|
|
|
|
from telegram.constants import ParseMode
|
2022-08-06 06:36:44 +00:00
|
|
|
|
from telegram.error import BadRequest, Forbidden
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from telegram.ext import CallbackContext
|
2022-08-06 06:36:44 +00:00
|
|
|
|
|
2022-10-30 13:35:53 +00:00
|
|
|
|
from core.base.redisdb import RedisDB
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.cookies import CookiesService
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.plugin import Plugin, job
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.sign.models import SignStatusEnum
|
|
|
|
|
from core.sign.services import SignServices
|
|
|
|
|
from core.user import UserService
|
2022-10-30 13:35:53 +00:00
|
|
|
|
from plugins.genshin.sign import SignSystem, NeedChallenge
|
2022-10-12 13:22:41 +00:00
|
|
|
|
from plugins.system.errorhandler import notice_chat_id
|
|
|
|
|
from plugins.system.sign_status import SignStatus
|
2022-10-31 07:43:51 +00:00
|
|
|
|
from utils.helpers import get_genshin_client
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
2022-08-06 06:36:44 +00:00
|
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
class SignJob(Plugin):
|
2022-10-10 05:34:06 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
sign_service: SignServices = None,
|
|
|
|
|
user_service: UserService = None,
|
|
|
|
|
cookies_service: CookiesService = None,
|
2022-10-30 13:35:53 +00:00
|
|
|
|
redis: RedisDB = None,
|
2022-10-10 05:34:06 +00:00
|
|
|
|
):
|
2022-08-06 06:36:44 +00:00
|
|
|
|
self.sign_service = sign_service
|
|
|
|
|
self.cookies_service = cookies_service
|
|
|
|
|
self.user_service = user_service
|
2022-10-30 13:35:53 +00:00
|
|
|
|
self.sign_system = SignSystem(redis)
|
2022-10-10 05:34:06 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@job.run_daily(time=datetime.time(hour=0, minute=1, second=0), name="SignJob")
|
2022-08-06 06:36:44 +00:00
|
|
|
|
async def sign(self, context: CallbackContext):
|
2022-10-12 13:22:41 +00:00
|
|
|
|
logger.info("正在执行自动签到" if context.job.name == "SignJob" else "正在执行自动重签")
|
2022-08-06 06:36:44 +00:00
|
|
|
|
sign_list = await self.sign_service.get_all()
|
|
|
|
|
for sign_db in sign_list:
|
|
|
|
|
user_id = sign_db.user_id
|
2022-10-28 16:37:27 +00:00
|
|
|
|
if sign_db.status in [
|
|
|
|
|
SignStatusEnum.INVALID_COOKIES,
|
|
|
|
|
SignStatusEnum.FORBIDDEN,
|
|
|
|
|
]:
|
2022-10-12 13:22:41 +00:00
|
|
|
|
continue
|
|
|
|
|
if context.job.name == "SignJob":
|
|
|
|
|
if sign_db.status not in [SignStatusEnum.STATUS_SUCCESS, SignStatusEnum.ALREADY_CLAIMED]:
|
|
|
|
|
continue
|
2022-12-25 13:26:08 +00:00
|
|
|
|
elif context.job.name == "SignAgainJob" and sign_db.status in [
|
|
|
|
|
SignStatusEnum.STATUS_SUCCESS,
|
|
|
|
|
SignStatusEnum.ALREADY_CLAIMED,
|
|
|
|
|
]:
|
|
|
|
|
continue
|
2022-08-06 06:36:44 +00:00
|
|
|
|
try:
|
2022-10-31 07:43:51 +00:00
|
|
|
|
client = await get_genshin_client(user_id)
|
2022-10-30 13:35:53 +00:00
|
|
|
|
text = await self.sign_system.start_sign(
|
2022-10-31 07:43:51 +00:00
|
|
|
|
client, is_sleep=True, is_raise=True, title="自动签到" if context.job.name == "SignJob" else "自动重新签到"
|
2022-10-30 13:35:53 +00:00
|
|
|
|
)
|
2022-10-14 16:23:10 +00:00
|
|
|
|
sign_db.status = SignStatusEnum.STATUS_SUCCESS
|
2022-08-06 06:36:44 +00:00
|
|
|
|
except InvalidCookies:
|
|
|
|
|
text = "自动签到执行失败,Cookie无效"
|
|
|
|
|
sign_db.status = SignStatusEnum.INVALID_COOKIES
|
|
|
|
|
except AlreadyClaimed:
|
|
|
|
|
text = "今天旅行者已经签到过了~"
|
|
|
|
|
sign_db.status = SignStatusEnum.ALREADY_CLAIMED
|
|
|
|
|
except GenshinException as exc:
|
|
|
|
|
text = f"自动签到执行失败,API返回信息为 {str(exc)}"
|
|
|
|
|
sign_db.status = SignStatusEnum.GENSHIN_EXCEPTION
|
2022-10-12 03:45:06 +00:00
|
|
|
|
except TimeoutException:
|
2022-08-06 06:36:44 +00:00
|
|
|
|
text = "签到失败了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ "
|
|
|
|
|
sign_db.status = SignStatusEnum.TIMEOUT_ERROR
|
2022-10-12 03:45:06 +00:00
|
|
|
|
except ClientConnectorError as exc:
|
2023-02-05 16:12:08 +00:00
|
|
|
|
logger.warning("aiohttp 请求错误 %s", str(exc))
|
2022-10-12 03:45:06 +00:00
|
|
|
|
text = "签到失败了呜呜呜 ~ 链接服务器发生错误 服务器熟啦 ~ "
|
|
|
|
|
sign_db.status = SignStatusEnum.TIMEOUT_ERROR
|
2022-09-02 16:17:00 +00:00
|
|
|
|
except NeedChallenge:
|
2023-02-05 16:12:08 +00:00
|
|
|
|
text = "签到失败,触发验证码风控"
|
2023-02-09 09:46:38 +00:00
|
|
|
|
sign_db.status = SignStatusEnum.NEED_CHALLENGE
|
2022-10-11 05:52:42 +00:00
|
|
|
|
except Exception as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error(f"执行自动签到时发生错误 用户UID[{user_id}]")
|
|
|
|
|
logger.exception(exc)
|
2022-09-02 16:17:00 +00:00
|
|
|
|
text = "签到失败了呜呜呜 ~ 执行自动签到时发生错误"
|
2022-08-06 07:37:43 +00:00
|
|
|
|
if sign_db.chat_id < 0:
|
2022-10-10 05:34:06 +00:00
|
|
|
|
text = f'<a href="tg://user?id={sign_db.user_id}">NOTICE {sign_db.user_id}</a>\n\n{text}'
|
2022-08-06 06:36:44 +00:00
|
|
|
|
try:
|
2022-08-06 07:37:43 +00:00
|
|
|
|
await context.bot.send_message(sign_db.chat_id, text, parse_mode=ParseMode.HTML)
|
2022-08-06 06:36:44 +00:00
|
|
|
|
except BadRequest as exc:
|
2023-02-05 16:12:08 +00:00
|
|
|
|
logger.error("执行自动签到时发生错误 message[%s] user_id[%s]", exc.message, user_id)
|
2022-08-06 06:36:44 +00:00
|
|
|
|
sign_db.status = SignStatusEnum.BAD_REQUEST
|
|
|
|
|
except Forbidden as exc:
|
2023-02-05 16:12:08 +00:00
|
|
|
|
logger.error("执行自动签到时发生错误 message[%s] user_id[%s]", exc.message, user_id)
|
2022-08-06 06:36:44 +00:00
|
|
|
|
sign_db.status = SignStatusEnum.FORBIDDEN
|
2022-10-11 05:52:42 +00:00
|
|
|
|
except Exception as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error(f"执行自动签到时发生错误 用户UID[{user_id}]")
|
|
|
|
|
logger.exception(exc)
|
2022-08-06 06:36:44 +00:00
|
|
|
|
continue
|
|
|
|
|
sign_db.time_updated = datetime.datetime.now()
|
|
|
|
|
await self.sign_service.update(sign_db)
|
2022-10-12 13:22:41 +00:00
|
|
|
|
logger.info("执行自动签到完成" if context.job.name == "SignJob" else "执行自动重签完成")
|
2022-10-12 03:45:06 +00:00
|
|
|
|
if context.job.name == "SignJob":
|
2022-10-12 16:41:40 +00:00
|
|
|
|
context.job_queue.run_once(self.sign, when=60, name="SignAgainJob")
|
2022-10-12 13:22:41 +00:00
|
|
|
|
elif context.job.name == "SignAgainJob":
|
|
|
|
|
text = await SignStatus.get_sign_status(self.sign_service)
|
|
|
|
|
await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML)
|