mirror.Manyana/setUp.py

341 lines
13 KiB
Python
Raw Normal View History

2024-02-27 21:33:36 +08:00
import logging
2023-07-22 08:39:49 +08:00
import os
2024-02-27 21:33:36 +08:00
import subprocess
import sys
2024-02-27 21:33:36 +08:00
import colorlog
2023-08-02 20:31:56 +08:00
2024-02-27 21:33:36 +08:00
import shutil
def newLogger():
# 创建一个logger对象
logger = logging.getLogger("villia")
# 设置日志级别为DEBUG这样可以输出所有级别的日志
logger.setLevel(logging.DEBUG)
# 创建一个StreamHandler对象用于输出日志到控制台
console_handler = logging.StreamHandler()
# 设置控制台输出的日志格式和颜色
logger.propagate = False
console_format = '%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(message)s'
console_colors = {
'DEBUG': 'white',
'INFO': 'cyan',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'bold_red',
}
console_formatter = colorlog.ColoredFormatter(console_format, log_colors=console_colors)
console_handler.setFormatter(console_formatter)
# 将控制台处理器添加到logger对象中
logger.addHandler(console_handler)
return logger
2023-07-22 08:39:49 +08:00
2024-02-27 21:33:36 +08:00
logger = newLogger()
2023-07-22 19:38:52 +08:00
def main():
2024-03-30 19:25:56 +08:00
print("请输入要执行的指令:\n1 绑定到远程仓库(如果通过源码包安装请执行)\n2 更新bot代码\n3 清理无用数据(如缓存图片)\n4 导出群信息制作一个chatLearning可用的配置文件\n5 全面去除二刺猿相关内容")
2023-08-02 20:31:56 +08:00
2023-07-22 19:38:52 +08:00
a=input("输入要执行的数字")
if a=="1":
os.system("git init")
os.system("git remote add origin https://github.com/avilliai/Manyana.git")
2023-07-24 11:46:20 +08:00
print("正在添加文件.....这可能需要较长时间")
2023-07-23 08:45:49 +08:00
os.system("git add .")
2023-07-22 19:38:52 +08:00
print("over")
elif a=="2":
2024-02-27 21:33:36 +08:00
updaat()
2023-07-23 13:19:52 +08:00
elif a=="3":
print("执行清理缓存操作")
ls1 = os.listdir("data/pictures/avatars")
for i in ls1:
try:
os.remove("data/pictures/avatars/" + i)
except:
continue
print("清理头像缓存完成")
2023-07-24 13:00:04 +08:00
ls1 = os.listdir("data/voices")
for i in ls1:
try:
os.remove("data/voices/" + i)
except:
continue
2023-12-06 20:06:42 +08:00
ls1 = os.listdir("data/music/musicCache")
for i in ls1:
try:
os.remove("data/music/musicCache/" + i)
except:
continue
2023-07-24 13:00:04 +08:00
print("清理音频缓存完成")
2023-07-23 13:19:52 +08:00
ls1 = os.listdir("data/pictures/cache")
for i in ls1:
try:
os.remove("data/pictures/cache/" + i)
except:
continue
ls1 = os.listdir("data/pictures/wallpaper")
for i in ls1:
try:
os.remove("data/pictures/wallpaper/" + i)
except:
continue
print("清理本地图库缓存完成,缓存的涩图都没喽")
print("清理缓存完成")
elif a=="4":
import json
print("chatLearning https://mirai.mamoe.net/topic/1018/chatlearning-%E8%AE%A9bot%E5%AD%A6%E4%BC%9A%E4%BD%A0%E7%9A%84%E7%BE%A4%E8%81%8A/8")
if os.path.exists("config.clc"):
2023-09-14 13:07:31 +08:00
with open("config.clc", "r", encoding="utf_8_sig") as ass:
p = json.loads(ass.read())
else:
p = {
"unmergegrouplist": [],
"learninggrouplist": [
628763673,
699455559
],
"replygrouplist": [
628763673,
699455559
],
"interval": 900,
"sendmode": 0,
"mergetime": 900,
"learning": 0,
"merge": 0,
"reply": 0,
"replychance": 50,
"admin": 0,
"Administrator": [
123456
],
"blackfreq": 5,
"voicereply": 0,
"voicereplychance": 20,
"synthesizer": "",
"version": "3.0.3",
"tag": {},
"singlereplychance": {},
"singlevoicereplychance": {},
"typefreq": {},
"tempmessagenum": 32,
"fastdelete": 0,
"replywait": [
0,
0
],
"voicecharerror": "存在违规字符,转换失败",
"voicecderror": "转换冷却中",
"voicelengtherror": "长度超过限制",
"deletesuccess": "已从词库内删除!",
"deletetemperror": "删除失败,该消息已不在缓存内",
"deletefinderror": "删除失败,词库中已无法找到该答案",
"replycd": 3,
"stopsign": 0,
"voicecommand": "快说 ",
"cosmatch": 0,
"cosmatching": 0.5,
"botname": [
""
],
"replylength": 100,
"atreply": 1
}
file1 = open('data/music/groups.txt', 'r')
js1 = file1.read()
js1 = json.loads(js1)
file1.close()
da = js1.keys()
print(list(da), type(list(da)))
p["replygrouplist"] = list(da)
p["learninggrouplist"] = list(da)
2023-09-14 13:07:31 +08:00
ja = json.dumps(p,ensure_ascii=False,indent=1)
with open("config.clc", "w", encoding="utf_8_sig",newline='\n') as fp:
fp.write(ja)
print("完毕请用config.clc覆盖chatLearning文件夹下同名文件")
2023-10-11 20:49:14 +08:00
elif a=="5":
import yaml
logger = newLogger()
logger.info("开始清理nudgeReply.yaml")
with open('config/nudgeReply.yaml', 'r', encoding='utf-8') as f:
resy = yaml.load(f.read(), Loader=yaml.FullLoader)
resy["BeatNudge"]=["干嘛","有事吗"]
resy["BeatNudge1"]=["干嘛","有事吗"]
resy["nudgedReply"]=["干嘛","有事吗"]
with open('config/nudgeReply.yaml', 'w', encoding="utf-8") as file:
yaml.dump(resy, file, allow_unicode=True)
logger.info("清理config/nudgeReply.yaml完成")
logger.info("开始清理:词库")
os.remove("data/autoReply/lexicon/public.xlsx")
2024-02-27 21:33:36 +08:00
2023-10-11 20:49:14 +08:00
shutil.copyfile('data/autoReply/lexicon/init.xlsx',
'data/autoReply/lexicon/public.xlsx')
logger.info("清理词库成功")
logger.warning("请自行删除 miraiBot/plugins/AutoGroup-2.0.3.mirai.jar")
logger.info("请自行修改 chatGLM模式为chatglm_pro")
2023-07-22 19:38:52 +08:00
else:
print("结束")
2024-02-29 22:25:26 +08:00
def updaat(f=False,jump=False,source="3"):
if jump==False:
logger.warning("更新python库按1跳过如果最近没有更新过不建议跳过可能错过某些更新。")
if input("在这里输入:") != "1":
os.system("pip install openai")
2024-02-29 22:25:26 +08:00
os.system("pip install edge-tts")
os.system("pip install psutil")
os.system("pip install -q -U google-generativeai")
os.system("pip install ruamel.yaml")
# os.system("pip install -U zhipuai")
# os.system("pip install pydantic==1.10.11")
# os.system("pip install aspose-words")
2024-02-27 21:33:36 +08:00
2024-02-29 22:25:26 +08:00
# os.system("pip install --upgrade poe-api")
# os.system("pip install --upgrade requests")
# os.system("pip install --upgrade urllib3[socks]")
# os.system("pip install selenium")
if source=="3":
logger.info("拉取bot代码\n--------------------")
2024-03-20 22:39:16 +08:00
logger.warning("建议优先选用与搭建源匹配的更新源,以免由于更新版本不一致导致意外状况")
2024-03-18 09:50:35 +08:00
logger.info("选择更新源()\n1 git源\n2 镜像源(无需代理同步更新。兼容git源搭建的Manyana git源不可用时可选此项)")
2024-02-29 22:26:24 +08:00
source = input("选择更新源(输入数字 )")
2024-02-29 22:25:26 +08:00
else:
source=str(source)
if source == "1":
2024-02-27 21:33:36 +08:00
# os.system("git pull https://github.com/avilliai/Manyana.git")
# 启动进程
p = subprocess.Popen(['git', 'pull', 'https://github.com/avilliai/Manyana.git'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
2024-02-29 22:25:26 +08:00
elif source=="2":
2024-02-28 12:00:19 +08:00
p = subprocess.Popen(['git', 'pull', 'https://gh-proxy.com/https://github.com/avilliai/Manyana'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
else:
logger.error("无效输入,重新执行")
updaat()
# 获取进程的输出和错误信息
stdout, stderr = p.communicate()
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
# 输出内容和错误信息都是字节串,需要解码为字符串
stdout = stdout.decode()
logger.info(stdout)
stderr = stderr.decode()
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
# 标记是否在错误信息中
in_error_info = False
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
# 存放冲突文件名的列表
conflict_files = []
if f==True:
if os.path.exists("./temp"):
if len(os.listdir("./config")) != 14:
logger.error("文件数目异常请参考远程仓库config文件夹https://github.com/avilliai/Manyana/tree/main/config补全缺失文件。")
input("如已补全请按任意键继续:")
2024-02-28 12:00:19 +08:00
for i in os.listdir("./temp"):
logger.info("开始处理"+i)
if os.path.exists("config/"+i):
conflict_file_dealter("./temp/"+i,"config/"+i)
2024-03-24 14:25:54 +08:00
if os.path.exists("./temp/conflict " + i):
os.remove("./temp/conflict " + i)
os.rename("./temp/" + i, "./temp/conflict " + i)
2024-02-28 12:00:19 +08:00
else:
continue
2024-03-17 20:01:24 +08:00
#shutil.rmtree("./temp")
2024-02-28 12:00:19 +08:00
logger.info("处理冲突文件完成")
2024-03-17 20:01:24 +08:00
logger.info("旧的冲突文件被保存到了temp文件夹以防万一你需要它们。")
2024-02-28 12:00:19 +08:00
logger.info("你可以关闭此窗口了")
input()
# 逐行检查错误信息
for line in stderr.split('\n'):
# 标记冲突文件名开始位置
if 'error: Your local changes to the following files would be overwritten by merge:' in line:
in_error_info = True
continue # 结束当前循环,进入下一个循环
# 标记冲突文件名结束位置
if 'Please commit your changes or stash them before you merge.' in line:
in_error_info = False
2024-02-27 21:52:13 +08:00
2024-02-28 12:00:19 +08:00
# 将冲突文件名添加到列表
if in_error_info:
conflict_files.append(line.strip())
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
# 显示冲突的文件列表
for file in conflict_files:
print('冲突文件:', file)
logger.warning("开始处理冲突文件")
if file.endswith(".py"):
os.remove(file)
logger.warning("移除了" + file)
elif file.endswith(".yaml"):
logger.info("冲突的配置文件" + file)
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
logger.warning("开始处理冲突文件.....读取中")
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
if os.path.exists("./temp"):
2024-03-30 19:25:56 +08:00
try:
shutil.copyfile(file, file.replace("config/", "temp/").replace("data/","temp/"))
os.remove(file)
except:
continue
2024-02-28 12:00:19 +08:00
else:
os.mkdir("./temp")
shutil.copyfile(file, file.replace("config", "temp"))
os.remove(file)
else:
logger.warning("无法处理的 " + file)
logger.warning("请自行决定删除或修改文件名称,在重新拉取后根据旧文件重新填写新文件")
2024-03-23 20:30:02 +08:00
logger.warning("如果存在冲突的.yaml类文件请按任意键程序将自动处理。如果无冲突yaml文件并完成了拉取请关闭此窗口")
2024-04-13 11:20:21 +08:00
logger.info("即将再次执行拉取操作,按任意键继续")
input()
2024-02-29 22:25:26 +08:00
updaat(True,True,str(source))
2024-02-28 12:00:19 +08:00
# 不要忘了等待进程结束
p.wait()
2024-02-27 21:33:36 +08:00
logger.info("结束")
logger.info("如更新成功请自行查看 更新日志.yaml")
# 创建一个YAML对象来加载和存储YAML数据
try:
from ruamel.yaml import YAML
except Exception as e:
logger.error("未安装ruamel.yaml库无法处理冲突文件开始安装缺少的依赖")
os.system("pip install ruamel.yaml")
from ruamel.yaml import YAML
# 创建一个YAML对象来加载和存储YAML数据
yaml = YAML()
def merge_dicts(old, new):
for k, v in old.items():
# 如果值是一个字典并且键在新的yaml文件中那么我们就递归地更新键值对
if isinstance(v, dict) and k in new and isinstance(new[k], dict):
merge_dicts(v, new[k])
# 如果键在新的yaml文件中我们就更新它的值
elif k in new:
logger.info("更新key"+str(k)+" value"+str(v))
new[k] = v
def conflict_file_dealter(file_old='old_aiReply.yaml', file_new='new_aiReply.yaml'):
# 加载旧的YAML文件
with open(file_old, 'r',encoding="utf-8") as file:
old_data = yaml.load(file)
# 加载新的YAML文件
with open(file_new, 'r',encoding="utf-8") as file:
new_data = yaml.load(file)
# 遍历旧的YAML数据并更新新的YAML数据中的相应值
merge_dicts(old_data, new_data)
# 把新的YAML数据保存到新的文件中
2024-02-29 22:14:04 +08:00
with open(file_new, 'w',encoding="utf-8") as file:
2024-02-27 21:33:36 +08:00
yaml.dump(new_data, file)
2023-07-22 19:38:52 +08:00
# 添加远程仓库
# 获取远程更新并合并到本地
if __name__ == '__main__':
2023-07-26 09:15:36 +08:00
main()
2024-03-17 16:44:15 +08:00
input("按任意键退出.")