forked from shirumesu/ui_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
120 lines (101 loc) · 3.57 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
@Author : shiying (github: LYshiying)
@Contact : Twitter: @shiying_ui | QQ: 839778960
@Version : 1.0.5.3
@EditTime : 2021/9/30 15:35pm(Editor: shiying)
@Desc : 修复: selenium跟playwright有一个不存在会导致整个插件无法使用的情况
"""
import os
import sys
import requests
import time
import re
from loguru import logger
from bs4 import BeautifulSoup
import nonebot
import config
from src.Services import init_bot
version = "1.0.5.3"
def get_chrome():
logger.info("正在尝试检查chrome-drive内核……")
if not os.listdir(os.path.join(config.res, "source", "blhxwiki")):
logger.debug(
f"连接url:https://chromedriver.chromium.org/,代理:{str(config.proxies.copy())}"
)
s = requests.get(
"https://chromedriver.chromium.org/",
proxies=config.proxies.copy(),
timeout=10,
)
logger.debug(f"status_code:{str(s.status_code)}")
soup = BeautifulSoup(s.text, "lxml")
html = soup.find_all("a", {"class": "XqQF9c"})
logger.info(
f"检测到稳定版chrome-drive:{html[4].string},请前往下载:{html[4].attrs['href']}"
)
else:
logger.info("已有chrome-drive,无需下载")
time.sleep(3)
def check_update():
logger.info("正在尝试检查更新……")
logger.debug(
f"连接url:https://raw.githubusercontent.com/LYshiying/ui_bot/main/bot.py,代理:{str(config.proxies.copy())}"
)
resp = requests.get(
"https://raw.githubusercontent.com/LYshiying/ui_bot/main/bot.py",
proxies=config.proxies.copy(),
)
logger.debug(f"status_code:{str(resp.status_code)}")
version_git = re.findall("@Version : (.+)", resp.text)[0]
version_desc = re.findall("@Desc : (.+)", resp.text)[0]
new_msg = (
f"发现新版本更新: {version_git}\n"
f"更新描述: {version_desc}\n"
f"请在根目录shift+右键打开终端PowerShell,使用git pull进行更新"
)
msg = (
f"目前uibot版本为: {version},无需更新"
if version_git == version
else f"目前uibot版本为: {version}\n{new_msg}"
)
logger.info(msg)
def switch_modules(modules_list):
for module_name in modules_list:
nonebot.load_plugin(f"src.plugins.{module_name}")
return
def log(debug_mode: bool = False):
logger.remove()
logger.add(
sys.stderr,
format="<g>{time:YYYY-MM-DD HH:mm:ss}</g> | <m>{module}:{function}</m> | <lvl>{level}</lvl> | <lvl>{message}</lvl>",
level="DEBUG" if debug_mode else "INFO",
colorize=True,
)
logger.add(
"./log/uilog.log",
format="<g>{time:YYYY-MM-DD HH:mm:ss}</g> | <m>{module}:{function}</m> | <lvl>{level}</lvl> | <lvl>{message}</lvl>",
rotation="00:00",
retention="5 days",
diagnose=False,
level="DEBUG" if debug_mode else "INFO",
)
logger.debug("日志配置加载完毕")
if __name__ == "__main__":
log(config.DEBUG)
if config.checkupdate:
try:
check_update()
except:
logger.error("检查更新失败,自动跳过")
else:
try:
get_chrome()
except:
logger.error(
"无法检查chrome内核版本,请前往手动下载:https://chromedriver.chromium.org/\n(否则blhxwiki插件无法使用)"
)
os.makedirs(config.res, exist_ok=True)
logger.debug("res文件夹创造完毕/已经存在res文件夹")
nonebot.init(config)
init_bot()
nonebot.run()