diff --git a/config.py b/config.py index a177a1aa..bd4fb574 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,7 @@ -import ujson import os +import ujson + class Config(object): def __init__(self): diff --git a/handler.py b/handler.py index 7ecfdf02..5f495a81 100644 --- a/handler.py +++ b/handler.py @@ -1,17 +1,15 @@ from typing import Optional -from telegram.ext import CommandHandler, MessageHandler, filters, ConversationHandler, \ - CallbackQueryHandler, InlineQueryHandler +from telegram.ext import CommandHandler, MessageHandler, filters, CallbackQueryHandler, InlineQueryHandler from plugins.admin import Admin from plugins.auth import Auth from plugins.base import NewChatMembersHandler from plugins.cookies import Cookies +from plugins.daily_note import DailyNote from plugins.errorhandler import error_handler from plugins.gacha import Gacha from plugins.help import Help -from plugins.uid import Uid -from plugins.daily_note import DailyNote from plugins.inline import Inline from plugins.job_queue import JobQueue from plugins.post import Post @@ -19,6 +17,7 @@ from plugins.quiz import Quiz from plugins.sign import Sign from plugins.start import start, ping, reply_keyboard_remove, unknown_command from plugins.strategy import Strategy +from plugins.uid import Uid from plugins.weapon import Weapon from service import BaseService @@ -33,7 +32,7 @@ def register_handlers(application, service: BaseService = None): application.add_handler(MessageHandler(filters.Regex(regex), handler, block=block)) if query: application.add_handler(CallbackQueryHandler(handler, pattern=query, block=block)) - + if service is None: return @@ -82,4 +81,3 @@ def register_handlers(application, service: BaseService = None): application.job_queue.run_once(job_queue.start_job, when=3, name="start_job") application.add_handler(MessageHandler(filters.COMMAND & filters.ChatType.PRIVATE, unknown_command)) application.add_error_handler(error_handler, block=False) - diff --git a/logger.py b/logger.py index 512b2b00..3a02e520 100644 --- a/logger.py +++ b/logger.py @@ -1,7 +1,8 @@ import logging -from logging.handlers import RotatingFileHandler -import colorlog import os +from logging.handlers import RotatingFileHandler + +import colorlog from config import config diff --git a/main.py b/main.py index bf654fa0..e20c040c 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,15 @@ import asyncio -from typing import Optional from warnings import filterwarnings from telegram.ext import Application from telegram.warnings import PTBUserWarning +from config import config from handler import register_handlers from logger import Log from service import StartService -from service.repository import AsyncRepository -from config import config from service.cache import RedisCache +from service.repository import AsyncRepository # 无视相关警告 # 该警告说明在官方GITHUB的WIKI中Frequently Asked Questions里的What do the per_* settings in ConversationHandler do? diff --git a/model/apihelper/__init__.py b/model/apihelper/__init__.py index 19757312..74905576 100644 --- a/model/apihelper/__init__.py +++ b/model/apihelper/__init__.py @@ -1,3 +1,3 @@ -from .hyperion import * +from .gacha import * from .hoyolab import * -from .gacha import * \ No newline at end of file +from .hyperion import * diff --git a/model/apihelper/hyperion.py b/model/apihelper/hyperion.py index d6278229..42d14e9a 100644 --- a/model/apihelper/hyperion.py +++ b/model/apihelper/hyperion.py @@ -1,8 +1,10 @@ import asyncio import re from typing import List + import httpx from httpx import AsyncClient + from .base import HyperionResponse, ArtworkImage, BaseResponseData from .helpers import get_ds, get_device_id diff --git a/model/helpers.py b/model/helpers.py index 1771490a..64957858 100644 --- a/model/helpers.py +++ b/model/helpers.py @@ -1,13 +1,14 @@ import hashlib import os from typing import List + +import aiofiles import httpx from httpx import UnsupportedProtocol from telegram import Bot from logger import Log from service.cache import RedisCache -import aiofiles USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \ "Chrome/90.0.4430.72 Safari/537.36" diff --git a/plugins/auth.py b/plugins/auth.py index 8b90613b..eddd1a9a 100644 --- a/plugins/auth.py +++ b/plugins/auth.py @@ -1,16 +1,15 @@ -import time import datetime import random +import time from typing import Tuple +from numpy.random import Generator, MT19937 from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ChatPermissions from telegram.constants import ParseMode from telegram.error import BadRequest from telegram.ext import CallbackContext from telegram.helpers import escape_markdown -from numpy.random import Generator, MT19937 - from logger import Log from model.helpers import get_admin_list from service import BaseService diff --git a/plugins/cookies.py b/plugins/cookies.py index b1c7009f..33038581 100644 --- a/plugins/cookies.py +++ b/plugins/cookies.py @@ -1,8 +1,8 @@ from http.cookies import SimpleCookie -import ujson -import genshin -from genshin import InvalidCookies, GenshinException, DataNotPublic +import genshin +import ujson +from genshin import InvalidCookies, GenshinException, DataNotPublic from telegram import Update, ReplyKeyboardRemove, ReplyKeyboardMarkup from telegram.ext import CallbackContext, CommandHandler, MessageHandler, filters, ConversationHandler from telegram.helpers import escape_markdown @@ -33,13 +33,13 @@ class Cookies(BasePlugins): cookies = Cookies(service) cookies_handler = ConversationHandler( entry_points=[CommandHandler('adduser', cookies.command_start, filters.ChatType.PRIVATE, block=True), - MessageHandler(filters.Regex(r"^绑定账号(.*)") & filters.ChatType.PRIVATE, - cookies.command_start, block=True)], + MessageHandler(filters.Regex(r"^绑定账号(.*)") & filters.ChatType.PRIVATE, + cookies.command_start, block=True)], states={ cookies.CHECK_SERVER: [MessageHandler(filters.TEXT & ~filters.COMMAND, - cookies.check_server, block=True)], + cookies.check_server, block=True)], cookies.CHECK_COOKIES: [MessageHandler(filters.TEXT & ~filters.COMMAND, - cookies.check_cookies, block=True)], + cookies.check_cookies, block=True)], cookies.COMMAND_RESULT: [MessageHandler(filters.TEXT & ~filters.COMMAND, cookies.command_result, block=True)], }, diff --git a/plugins/daily_note.py b/plugins/daily_note.py index 452f90a3..44cfbb30 100644 --- a/plugins/daily_note.py +++ b/plugins/daily_note.py @@ -1,5 +1,5 @@ -import os import datetime +import os import genshin from genshin import GenshinException, DataNotPublic @@ -32,7 +32,7 @@ class DailyNote(BasePlugins): daily_note = DailyNote(service) daily_note_handler = ConversationHandler( entry_points=[CommandHandler('dailynote', daily_note.command_start, block=True), - MessageHandler(filters.Regex(r"^当前状态(.*)"), daily_note.command_start, block=True)], + MessageHandler(filters.Regex(r"^当前状态(.*)"), daily_note.command_start, block=True)], states={ daily_note.COMMAND_RESULT: [CallbackQueryHandler(daily_note.command_result, block=True)] }, diff --git a/plugins/errorhandler.py b/plugins/errorhandler.py index fde107ad..a848ddc8 100644 --- a/plugins/errorhandler.py +++ b/plugins/errorhandler.py @@ -6,14 +6,13 @@ import ujson from aiohttp import ClientConnectorError from genshin import InvalidCookies, GenshinException, TooManyRequests from httpx import ConnectTimeout - from telegram import Update, ReplyKeyboardRemove from telegram.constants import ParseMode from telegram.error import BadRequest, TimedOut from telegram.ext import CallbackContext, ConversationHandler -from logger import Log from config import config +from logger import Log try: notice_chat_id = config.TELEGRAM["notice"]["ERROR"]["chat_id"] @@ -36,7 +35,6 @@ async def send_user_notification(update: Update, _: CallbackContext, text: str): def conversation_error_handler(func: Callable) -> Callable: - async def decorator(*args, **kwargs): update: Optional[Update] = None context: Optional[CallbackContext] = None diff --git a/plugins/gacha.py b/plugins/gacha.py index a2404b1a..9c813fa3 100644 --- a/plugins/gacha.py +++ b/plugins/gacha.py @@ -1,14 +1,14 @@ import os +from pyppeteer import launch from telegram import Update from telegram.constants import ChatAction from telegram.ext import CallbackContext, ConversationHandler, filters from logger import Log +from metadata.metadata import metadat from plugins.base import BasePlugins, RestrictsCalls from service import BaseService -from pyppeteer import launch -from metadata.metadata import metadat from service.wish import WishCountInfo, get_one diff --git a/plugins/inline.py b/plugins/inline.py index 9dc622c7..8197e806 100644 --- a/plugins/inline.py +++ b/plugins/inline.py @@ -1,6 +1,7 @@ from typing import cast from urllib.parse import urlparse, urlencode, ParseResult from uuid import uuid4 + from telegram import InlineQueryResultArticle, InputTextMessageContent, Update, InlineQuery, InlineQueryResultPhoto from telegram.constants import ParseMode from telegram.error import BadRequest @@ -8,9 +9,9 @@ from telegram.ext import CallbackContext from telegram.helpers import escape_markdown from logger import Log +from metadata.metadata import metadat from service import BaseService from service.base import QuestionData -from metadata.metadata import metadat class Inline: diff --git a/plugins/post.py b/plugins/post.py index e5d79642..1ecc70ea 100644 --- a/plugins/post.py +++ b/plugins/post.py @@ -1,6 +1,6 @@ from typing import Optional, List -from bs4 import BeautifulSoup +from bs4 import BeautifulSoup from telegram import Update, ReplyKeyboardMarkup, ReplyKeyboardRemove, InputMediaPhoto from telegram.constants import ParseMode from telegram.error import BadRequest diff --git a/plugins/quiz.py b/plugins/quiz.py index 94a28d9e..d64fef7e 100644 --- a/plugins/quiz.py +++ b/plugins/quiz.py @@ -37,7 +37,7 @@ class Quiz(BasePlugins): self.generator = Generator(MT19937(int(self.send_time))) self.service = service self.time_out = 120 - + @staticmethod def create_conversation_handler(service: BaseService): quiz = Quiz(service) @@ -47,13 +47,13 @@ class Quiz(BasePlugins): quiz.CHECK_COMMAND: [MessageHandler(filters.TEXT & ~filters.COMMAND, quiz.check_command, block=True)], quiz.CHECK_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND, - quiz.check_question, block=True)], + quiz.check_question, block=True)], quiz.GET_NEW_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND, - quiz.get_new_question, block=True)], + quiz.get_new_question, block=True)], quiz.GET_NEW_CORRECT_ANSWER: [MessageHandler(filters.TEXT & ~filters.COMMAND, - quiz.get_new_correct_answer, block=True)], + quiz.get_new_correct_answer, block=True)], quiz.GET_NEW_WRONG_ANSWER: [MessageHandler(filters.TEXT & ~filters.COMMAND, - quiz.get_new_wrong_answer, block=True), + quiz.get_new_wrong_answer, block=True), CommandHandler("finish", quiz.finish_edit)], quiz.SAVE_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND, quiz.save_question, block=True)], diff --git a/plugins/sign.py b/plugins/sign.py index 5982cf90..b0430fdc 100644 --- a/plugins/sign.py +++ b/plugins/sign.py @@ -1,5 +1,5 @@ -import time import datetime +import time import genshin from genshin import Game, GenshinException, AlreadyClaimed @@ -33,7 +33,7 @@ class Sign(BasePlugins): sign = Sign(service) sign_handler = ConversationHandler( entry_points=[CommandHandler('sign', sign.command_start, block=True), - MessageHandler(filters.Regex(r"^每日签到(.*)"), sign.command_start, block=True)], + MessageHandler(filters.Regex(r"^每日签到(.*)"), sign.command_start, block=True)], states={ sign.COMMAND_RESULT: [CallbackQueryHandler(sign.command_result, block=True)] }, diff --git a/plugins/strategy.py b/plugins/strategy.py index f3ac30c0..74615c9f 100644 --- a/plugins/strategy.py +++ b/plugins/strategy.py @@ -5,11 +5,11 @@ from telegram.constants import ChatAction, ParseMode from telegram.ext import CallbackContext, filters from logger import Log +from metadata.shortname import roleToName from model.helpers import url_to_file from plugins.base import BasePlugins from plugins.errorhandler import conversation_error_handler from service import BaseService -from metadata.shortname import roleToName class Strategy(BasePlugins): diff --git a/plugins/uid.py b/plugins/uid.py index 758cc29a..a985dc51 100644 --- a/plugins/uid.py +++ b/plugins/uid.py @@ -33,7 +33,7 @@ class Uid(BasePlugins): uid = Uid(service) uid_handler = ConversationHandler( entry_points=[CommandHandler('uid', uid.command_start, block=True), - MessageHandler(filters.Regex(r"^玩家查询(.*)"), uid.command_start, block=True)], + MessageHandler(filters.Regex(r"^玩家查询(.*)"), uid.command_start, block=True)], states={ uid.COMMAND_RESULT: [CallbackQueryHandler(uid.command_result, block=True)] }, diff --git a/plugins/weapon.py b/plugins/weapon.py index 188ab539..746c872d 100644 --- a/plugins/weapon.py +++ b/plugins/weapon.py @@ -5,12 +5,12 @@ from telegram.constants import ChatAction from telegram.ext import CallbackContext, filters from logger import Log +from metadata.metadata import metadat +from metadata.shortname import weaponToName from model.helpers import url_to_file from plugins.base import BasePlugins from plugins.errorhandler import conversation_error_handler from service import BaseService -from metadata.shortname import weaponToName -from metadata.metadata import metadat class Weapon(BasePlugins): diff --git a/service/__init__.py b/service/__init__.py index 549476a2..e70755f2 100644 --- a/service/__init__.py +++ b/service/__init__.py @@ -1,9 +1,9 @@ from service.admin import AdminService +from service.cache import RedisCache from service.gacha import GachaService from service.game import GetGameInfo from service.quiz import QuizService from service.repository import AsyncRepository -from service.cache import RedisCache from service.template import TemplateService from service.user import UserInfoFormDB diff --git a/service/admin.py b/service/admin.py index 19f2e47e..b6f7eaa6 100644 --- a/service/admin.py +++ b/service/admin.py @@ -4,8 +4,8 @@ from pymysql import IntegrityError from config import config from logger import Log -from service.repository import AsyncRepository from service.cache import RedisCache +from service.repository import AsyncRepository class AdminService: diff --git a/service/cache.py b/service/cache.py index 05d4fad4..995902c2 100644 --- a/service/cache.py +++ b/service/cache.py @@ -10,7 +10,7 @@ from service.base import QuestionData, AnswerData class RedisCache: - def __init__(self, host="127.0.0.1", port=6379, db=0,loop=None): + def __init__(self, host="127.0.0.1", port=6379, db=0, loop=None): self._loop = asyncio.get_event_loop() # Redis 官方文档显示 默认创建POOL连接池 Log.debug(f'获取Redis配置 [host]: {host}') diff --git a/service/gacha.py b/service/gacha.py index 1ffaaea5..246281e0 100644 --- a/service/gacha.py +++ b/service/gacha.py @@ -1,6 +1,6 @@ from model.apihelper import GachaInfo -from service.repository import AsyncRepository from service.cache import RedisCache +from service.repository import AsyncRepository class GachaService: diff --git a/service/game.py b/service/game.py index 93273bd9..56bd4e4b 100644 --- a/service/game.py +++ b/service/game.py @@ -1,6 +1,6 @@ from model.apihelper import Hyperion -from service.repository import AsyncRepository from service.cache import RedisCache +from service.repository import AsyncRepository class GetGameInfo: diff --git a/service/quiz.py b/service/quiz.py index 89ed1924..4b28aa53 100644 --- a/service/quiz.py +++ b/service/quiz.py @@ -2,9 +2,9 @@ from typing import List import ujson -from service.repository import AsyncRepository -from service.cache import RedisCache from service.base import QuestionData +from service.cache import RedisCache +from service.repository import AsyncRepository class QuizService: @@ -44,4 +44,4 @@ class QuizService: async def get_answer(self, answer_id: int): data = await self.cache.get_one_answer(answer_id) - return ujson.loads(data) \ No newline at end of file + return ujson.loads(data) diff --git a/service/template.py b/service/template.py index 651c07d8..74b0652e 100644 --- a/service/template.py +++ b/service/template.py @@ -2,6 +2,7 @@ import asyncio import os import time from typing import Optional + from jinja2 import PackageLoader, Environment, Template from playwright.async_api import async_playwright, Browser, ViewportSize, Playwright @@ -10,7 +11,7 @@ from logger import Log class TemplateService: - def __init__(self, template_package_name: str = "resources", cache_dir_name: str = "cache",loop=None): + def __init__(self, template_package_name: str = "resources", cache_dir_name: str = "cache", loop=None): self._template_package_name = template_package_name self._browser: Optional[Browser] = None self._playwright: Optional[Playwright] = None