apt 支持批量安装抽奖 (#50)

* 新增 apt 批量安装
This commit is contained in:
c3b2a 2021-02-07 18:55:35 +08:00 committed by GitHub
parent 0687b4c051
commit c4eb7d92d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,11 +13,33 @@ from pagermaid.utils import upload_attachment
from pagermaid.modules import plugin_list as active_plugins, __list_plugins from pagermaid.modules import plugin_list as active_plugins, __list_plugins
def move_plugin(file_path):
plugin_directory = f"{working_dir}/plugins/"
if exists(f"{plugin_directory}{file_path}"):
remove(f"{plugin_directory}{file_path}")
move(file_path, plugin_directory)
elif exists(f"{plugin_directory}{file_path}.disabled"):
remove(f"{plugin_directory}{file_path}.disabled")
move(file_path, f"{plugin_directory}{file_path}.disabled")
else:
move(file_path, plugin_directory)
def update_version(file_path, plugin_content, plugin_name, version):
with open(file_path, 'wb') as f:
f.write(plugin_content)
with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f:
version_json = json.load(f)
version_json[plugin_name] = version
with open(f"{plugin_directory}version.json", 'w') as f:
json.dump(version_json, f)
@listener(is_plugin=False, outgoing=True, command="apt", diagnostics=False, @listener(is_plugin=False, outgoing=True, command="apt", diagnostics=False,
description="用于管理安装到 PagerMaid-Modify 的插件。", description="用于管理安装到 PagerMaid-Modify 的插件。",
parameters="{update|search|show|status|install|remove|enable|disable|upload} <插件名称/文件>") parameters="{update|search|show|status|install|remove|enable|disable|upload} <插件名称/文件>")
async def plugin(context): async def plugin(context):
if len(context.parameter) > 2 or len(context.parameter) == 0: if len(context.parameter) == 0:
await context.edit("出错了呜呜呜 ~ 无效的参数。") await context.edit("出错了呜呜呜 ~ 无效的参数。")
return return
reply = await context.get_reply_message() reply = await context.get_reply_message()
@ -36,85 +58,72 @@ async def plugin(context):
except FileNotFoundError: except FileNotFoundError:
pass pass
return return
if exists(f"{plugin_directory}{file_path}"): move_plugin(file_path)
remove(f"{plugin_directory}{file_path}")
move(file_path, plugin_directory)
elif exists(f"{plugin_directory}{file_path}.disabled"):
remove(f"{plugin_directory}{file_path}.disabled")
move(file_path, f"{plugin_directory}{file_path}.disabled")
else:
move(file_path, plugin_directory)
await context.edit(f"插件 {path.basename(file_path)[:-3]} 已安装PagerMaid-Modify 正在重新启动。") await context.edit(f"插件 {path.basename(file_path)[:-3]} 已安装PagerMaid-Modify 正在重新启动。")
await log(f"成功安装插件 {path.basename(file_path)[:-3]}.") await log(f"成功安装插件 {path.basename(file_path)[:-3]}.")
await context.client.disconnect() await context.client.disconnect()
elif len(context.parameter) == 2: elif len(context.parameter) >= 2:
plugin_name = context.parameter[1] await context.edit("安装插件中 . . .")
plugin_online = \ success_list = []
json.loads(get("https://raw.githubusercontent.com/xtaodada/PagerMaid_Plugins/master/list.json").content)[ failed_list = []
'list'] noneed_list = []
if exists(f"{plugin_directory}version.json"): for x in range(len(context.parameter) - 1):
with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f: plugin_name = context.parameter[1 + x]
version_json = json.load(f) plugin_online = \
try: json.loads(get("https://raw.githubusercontent.com/xtaodada/PagerMaid_Plugins/master/list.json").content)[
plugin_version = version_json[plugin_name] 'list']
except: if exists(f"{plugin_directory}version.json"):
with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f:
version_json = json.load(f)
try:
plugin_version = version_json[plugin_name]
except:
plugin_version = False
else:
temp_dict = {}
with open(f"{plugin_directory}version.json", 'w') as f:
json.dump(temp_dict, f)
plugin_version = False plugin_version = False
else: flag = False
temp_dict = {} for i in plugin_online:
with open(f"{plugin_directory}version.json", 'w') as f: if i['name'] == plugin_name:
json.dump(temp_dict, f) flag = True
plugin_version = False if plugin_version:
for i in plugin_online: if (float(i['version']) - float(plugin_version)) <= 0:
if i['name'] == plugin_name: noneed_list.append(plugin_name)
if plugin_version: break
if (float(i['version']) - float(plugin_version)) <= 0: else:
await context.edit(f"插件 {plugin_name} 为最新版本,无需更新。") file_path = plugin_name + ".py"
return plugin_content = get(
f"https://raw.githubusercontent.com/xtaodada/PagerMaid_Plugins/master/{plugin_name}.py").content
update_version(file_path, plugin_content, plugin_name, i['version'])
move_plugin(file_path)
success_list.append(path.basename(file_path)[:-3])
break
else: else:
file_path = plugin_name + ".py" file_path = plugin_name + ".py"
plugin_content = get( plugin_content = get(
f"https://raw.githubusercontent.com/xtaodada/PagerMaid_Plugins/master/{plugin_name}.py").content f"https://raw.githubusercontent.com/xtaodada/PagerMaid_Plugins/master/{plugin_name}.py").content
with open(file_path, 'wb') as f: update_version(file_path, plugin_content, plugin_name, i['version'])
f.write(plugin_content) move_plugin(file_path)
with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f: success_list.append(path.basename(file_path)[:-3])
version_json = json.load(f) if not flag:
version_json[plugin_name] = i['version'] now_message += f"错误:没有找到插件 {plugin_name}\n"
with open(f"{plugin_directory}version.json", 'w') as f: failed_list.append(plugin_name)
json.dump(version_json, f) message = ""
if exists(f"{plugin_directory}{file_path}"): if len(success_list) > 0:
remove(f"{plugin_directory}{file_path}") message += "安装成功 : %s\n" % ", ".join(success_list)
move(file_path, plugin_directory) if len(failed_list) > 0:
elif exists(f"{plugin_directory}{file_path}.disabled"): message += "安装失败 : %s\n" % ", ".join(failed_list)
remove(f"{plugin_directory}{file_path}.disabled") if len(noneed_list) > 0:
move(file_path, f"{plugin_directory}{file_path}.disabled") message += "无需更新 : %s\n" % ", ".join(noneed_list)
else: await log(message)
move(file_path, plugin_directory) restart = len(success_list) > 0
await context.edit(f"插件 {path.basename(file_path)[:-3]} 已安装PagerMaid-Modify 正在重新启动。") if restart:
await log(f"成功安装插件 {path.basename(file_path)[:-3]}.") message += "PagerMaid-Modify 正在重启。"
await context.client.disconnect() await context.edit(message)
else: if restart:
file_path = plugin_name + ".py" await context.client.disconnect()
plugin_content = get(
f"https://raw.githubusercontent.com/xtaodada/PagerMaid_Plugins/master/{plugin_name}.py").content
with open(file_path, 'wb') as f:
f.write(plugin_content)
with open(f"{plugin_directory}version.json", 'r', encoding="utf-8") as f:
version_json = json.load(f)
version_json[plugin_name] = i['version']
with open(f"{plugin_directory}version.json", 'w') as f:
json.dump(version_json, f)
if exists(f"{plugin_directory}{file_path}"):
remove(f"{plugin_directory}{file_path}")
move(file_path, plugin_directory)
elif exists(f"{plugin_directory}{file_path}.disabled"):
remove(f"{plugin_directory}{file_path}.disabled")
move(file_path, f"{plugin_directory}{file_path}.disabled")
else:
move(file_path, plugin_directory)
await context.edit(f"插件 {path.basename(file_path)[:-3]} 已安装PagerMaid-Modify 正在重新启动。")
await log(f"成功安装插件 {path.basename(file_path)[:-3]}.")
await context.client.disconnect()
await context.edit(f"错误:没有找到插件 {plugin_name}")
else: else:
await context.edit("出错了呜呜呜 ~ 无效的参数。") await context.edit("出错了呜呜呜 ~ 无效的参数。")
elif context.parameter[0] == "remove": elif context.parameter[0] == "remove":
@ -273,15 +282,9 @@ async def plugin(context):
version_json[i] = m['version'] version_json[i] = m['version']
with open(f"{plugin_directory}version.json", 'w') as f: with open(f"{plugin_directory}version.json", 'w') as f:
json.dump(version_json, f) json.dump(version_json, f)
if exists(f"{plugin_directory}{file_path}"): move_plugin(file_path)
remove(f"{plugin_directory}{file_path}")
move(file_path, plugin_directory)
elif exists(f"{plugin_directory}{file_path}.disabled"):
remove(f"{plugin_directory}{file_path}.disabled")
move(file_path, f"{plugin_directory}{file_path}.disabled")
else:
move(file_path, plugin_directory)
await context.edit('正在读取云端插件列表...完成\n正在读取本地插件版本信息...完成\n' + need_update) await context.edit('正在读取云端插件列表...完成\n正在读取本地插件版本信息...完成\n' + need_update)
await context.client.disconnect()
elif context.parameter[0] == "search": elif context.parameter[0] == "search":
if len(context.parameter) == 1: if len(context.parameter) == 1:
await context.edit("没插件名我怎么搜索?") await context.edit("没插件名我怎么搜索?")