mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
Add files via upload
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
from nonebot import on_command
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent
|
||||
from .data_source import download_gocq_lasted, upload_gocq_lasted
|
||||
import os
|
||||
from nonebot.adapters.cqhttp.permission import GROUP
|
||||
from services.log import logger
|
||||
from util.utils import scheduler, get_bot, UserExistLimiter
|
||||
from configs.config import UPDATE_GOCQ_GROUP
|
||||
from pathlib import Path
|
||||
|
||||
path = str(Path('/resources/gocqhttp_file/').absolute()) + '/'
|
||||
|
||||
lasted_gocqhttp = on_command("更新gocq", permission=GROUP, priority=5, block=True)
|
||||
|
||||
|
||||
_ulmt = UserExistLimiter()
|
||||
|
||||
|
||||
@lasted_gocqhttp.handle()
|
||||
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
# try:
|
||||
if event.group_id in UPDATE_GOCQ_GROUP:
|
||||
await lasted_gocqhttp.send('检测中...')
|
||||
info = await download_gocq_lasted()
|
||||
if info == 'gocqhttp没有更新!':
|
||||
await lasted_gocqhttp.finish('gocqhttp没有更新!')
|
||||
if _ulmt.check(event.group_id):
|
||||
await lasted_gocqhttp.finish('gocqhttp正在更新,请勿重复使用该命令', at_sender=True)
|
||||
_ulmt.set_True(event.group_id)
|
||||
try:
|
||||
for file in os.listdir(path):
|
||||
await upload_gocq_lasted(file, event.group_id)
|
||||
logger.info(f'更新了cqhttp...{file}')
|
||||
await lasted_gocqhttp.send(f'gocqhttp更新了,已上传成功!\n更新内容:\n{info}')
|
||||
except Exception as e:
|
||||
logger.error(f'更新gocq错误 e:{e}')
|
||||
_ulmt.set_False(event.group_id)
|
||||
|
||||
|
||||
# 更新gocq
|
||||
@scheduler.scheduled_job(
|
||||
'cron',
|
||||
hour=3,
|
||||
minute=1,
|
||||
)
|
||||
async def _():
|
||||
if UPDATE_GOCQ_GROUP:
|
||||
bot = get_bot()
|
||||
try:
|
||||
info = await download_gocq_lasted()
|
||||
if info == 'gocqhttp没有更新!':
|
||||
logger.info('gocqhttp没有更新!')
|
||||
return
|
||||
for group in UPDATE_GOCQ_GROUP:
|
||||
for file in os.listdir(path):
|
||||
await upload_gocq_lasted(file, group)
|
||||
await bot.send_group_msg(group_id=group, message=f"gocqhttp更新了,已上传成功!\n更新内容:\n{info}")
|
||||
except Exception as e:
|
||||
logger.error(f'自动更新gocq出错 e:{e}')
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import aiohttp
|
||||
from util.utils import get_local_proxy, get_bot
|
||||
from util.user_agent import get_user_agent
|
||||
import asyncio
|
||||
import platform
|
||||
import aiofiles
|
||||
from bs4 import BeautifulSoup
|
||||
import os
|
||||
if platform.system() == 'Windows':
|
||||
path = os.getcwd() + '/resources/gocqhttp_file/'
|
||||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||||
else:
|
||||
path = r'/home/hibiki/hibikibot/resources/gocqhttp_file/'
|
||||
|
||||
|
||||
url = 'https://github.com/Mrs4s/go-cqhttp/releases'
|
||||
|
||||
|
||||
async def download_gocq_lasted():
|
||||
async with aiohttp.ClientSession(headers=get_user_agent()) as session:
|
||||
async with session.get(url, proxy=get_local_proxy()) as response:
|
||||
soup = BeautifulSoup(await response.text(), 'lxml')
|
||||
a = soup.find('div', {'class': 'release-header'}).find('a')
|
||||
title = a.text
|
||||
_url = a.get('href')
|
||||
for file in os.listdir(path):
|
||||
if file.endswith('.zip'):
|
||||
if file == title + '-windows-amd64.zip' or file == title + '_windows_amd64.zip':
|
||||
return 'gocqhttp没有更新!'
|
||||
for file in os.listdir(path):
|
||||
os.remove(path + file)
|
||||
async with session.get("https://github.com" + _url, proxy=get_local_proxy()) as res:
|
||||
update_info = ''
|
||||
soup = BeautifulSoup(await res.text(), 'lxml')
|
||||
info_div = soup.find('div', {'class': 'markdown-body'})
|
||||
for p in info_div.find_all('p'):
|
||||
update_info += p.text.replace('<br>', '\n') + '\n'
|
||||
div_all = soup.select('div.d-flex.flex-justify-between.flex-items-center.py-1.py-md-2.Box-body.px-2')
|
||||
for div in div_all:
|
||||
if div.find('a').find('span').text == title + '-windows-amd64.zip' or\
|
||||
div.find('a').find('span').text == title + '-linux-arm64.tar.gz' or\
|
||||
div.find('a').find('span').text == 'go-cqhttp_windows_amd64.zip' or \
|
||||
div.find('a').find('span').text == 'go-cqhttp_linux_arm64.tar.gz':
|
||||
file_url = div.find('a').get('href')
|
||||
if div.find('a').find('span').text.find('windows') == -1:
|
||||
tag = '-linux-arm64.tar.gz'
|
||||
else:
|
||||
tag = '-windows-amd64.zip'
|
||||
async with session.get("https://github.com" + file_url, proxy=get_local_proxy()) as res_file:
|
||||
async with aiofiles.open(path + title + tag, 'wb') as f:
|
||||
await f.write(await res_file.read())
|
||||
return update_info
|
||||
|
||||
|
||||
async def upload_gocq_lasted(name, group_id):
|
||||
bot = get_bot()
|
||||
folder_id = 0
|
||||
for folder in (await bot.get_group_root_files(group_id=group_id))['folders']:
|
||||
if folder['folder_name'] == 'gocq':
|
||||
folder_id = folder['folder_id']
|
||||
if not folder_id:
|
||||
await bot.send_group_msg(group_id=group_id,
|
||||
message=f'请创建gocq文件夹后重试!')
|
||||
for file in os.listdir(path):
|
||||
os.remove(path+file)
|
||||
else:
|
||||
await bot.upload_group_file(
|
||||
group_id=group_id,
|
||||
folder=folder_id,
|
||||
file=path + name,
|
||||
name=name
|
||||
)
|
||||
|
||||
|
||||
# asyncio.get_event_loop().run_until_complete(download_gocq_lasted())
|
||||
|
||||
Reference in New Issue
Block a user