2021-10-01 01:18:04 +00:00
|
|
|
|
'''编写机器人时可能会用到的一些工具类方法。'''
|
2021-04-28 12:21:33 +00:00
|
|
|
|
import os
|
2021-07-21 17:58:33 +00:00
|
|
|
|
import traceback
|
2021-07-27 17:42:47 +00:00
|
|
|
|
import uuid
|
2021-08-07 07:56:48 +00:00
|
|
|
|
from os.path import abspath
|
2021-10-01 01:18:04 +00:00
|
|
|
|
from typing import Union
|
2021-04-27 15:01:17 +00:00
|
|
|
|
|
2021-02-06 16:30:13 +00:00
|
|
|
|
import aiohttp
|
2021-07-21 17:58:33 +00:00
|
|
|
|
import filetype as ft
|
2021-09-10 18:05:27 +00:00
|
|
|
|
import ujson as json
|
2021-11-12 14:43:04 +00:00
|
|
|
|
from tenacity import retry, wait_fixed, stop_after_attempt
|
2021-02-06 16:30:13 +00:00
|
|
|
|
|
2022-01-17 15:34:07 +00:00
|
|
|
|
from core.elements import PrivateAssets
|
2021-10-14 15:18:47 +00:00
|
|
|
|
from core.loader import load_modules
|
2021-11-12 14:25:53 +00:00
|
|
|
|
from core.logger import Logger
|
2021-04-28 12:21:33 +00:00
|
|
|
|
|
2021-08-07 07:56:48 +00:00
|
|
|
|
|
2021-10-01 01:18:04 +00:00
|
|
|
|
def init() -> None:
|
2022-01-08 07:31:30 +00:00
|
|
|
|
'''初始化机器人。仅用于bot.py与console.py。'''
|
2021-10-14 15:18:47 +00:00
|
|
|
|
load_modules()
|
2021-08-21 15:58:07 +00:00
|
|
|
|
version = os.path.abspath(PrivateAssets.path + '/version')
|
|
|
|
|
write_version = open(version, 'w')
|
2021-10-16 05:24:53 +00:00
|
|
|
|
try:
|
|
|
|
|
write_version.write(os.popen('git rev-parse HEAD', 'r').read()[0:6])
|
2022-04-01 14:55:21 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
write_version.write(str(e))
|
2021-08-21 15:58:07 +00:00
|
|
|
|
write_version.close()
|
|
|
|
|
tag = os.path.abspath(PrivateAssets.path + '/version_tag')
|
|
|
|
|
write_tag = open(tag, 'w')
|
2021-10-16 05:24:53 +00:00
|
|
|
|
try:
|
|
|
|
|
write_tag.write(os.popen('git tag -l', 'r').read().split('\n')[-2])
|
2022-04-01 14:55:21 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
write_tag.write(str(e))
|
2021-08-21 15:58:07 +00:00
|
|
|
|
write_tag.close()
|
|
|
|
|
|
|
|
|
|
|
2021-11-12 14:55:29 +00:00
|
|
|
|
@retry(stop=stop_after_attempt(3), wait=wait_fixed(3), reraise=True)
|
2021-11-12 14:43:04 +00:00
|
|
|
|
async def get_url(url: str, status_code: int = False, headers: dict = None, fmt=None, log=False):
|
2021-11-12 14:25:53 +00:00
|
|
|
|
"""利用AioHttp获取指定url的内容。
|
|
|
|
|
|
2021-10-01 01:18:04 +00:00
|
|
|
|
:param url: 需要获取的url。
|
2021-11-01 14:04:08 +00:00
|
|
|
|
:param status_code: 指定请求到的状态码,若不符则抛出ValueError。
|
2021-10-01 01:18:04 +00:00
|
|
|
|
:param headers: 请求时使用的http头。
|
2021-11-12 14:43:04 +00:00
|
|
|
|
:param fmt: 指定返回的格式。
|
|
|
|
|
:param log: 是否输出日志。
|
2021-11-01 14:04:08 +00:00
|
|
|
|
:returns: 指定url的内容(字符串)。
|
2021-11-12 14:25:53 +00:00
|
|
|
|
"""
|
2021-11-12 14:43:04 +00:00
|
|
|
|
async with aiohttp.ClientSession(headers=headers) as session:
|
2021-02-11 12:41:07 +00:00
|
|
|
|
async with session.get(url, timeout=aiohttp.ClientTimeout(total=20), headers=headers) as req:
|
2021-11-12 14:43:04 +00:00
|
|
|
|
if log:
|
2021-11-12 14:45:31 +00:00
|
|
|
|
Logger.info(await req.read())
|
2021-10-02 07:05:50 +00:00
|
|
|
|
if status_code and req.status != status_code:
|
2022-01-07 15:16:28 +00:00
|
|
|
|
raise ValueError(f'{str(req.status)}[Ke:Image,path=https://http.cat/{str(req.status)}.jpg]')
|
2021-10-02 07:05:50 +00:00
|
|
|
|
if fmt is not None:
|
|
|
|
|
if hasattr(req, fmt):
|
|
|
|
|
return await getattr(req, fmt)()
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError(f"NoSuchMethod: {fmt}")
|
|
|
|
|
else:
|
|
|
|
|
text = await req.text()
|
|
|
|
|
return text
|
2021-04-27 15:01:17 +00:00
|
|
|
|
|
|
|
|
|
|
2022-01-08 12:27:46 +00:00
|
|
|
|
@retry(stop=stop_after_attempt(3), wait=wait_fixed(3), reraise=True)
|
|
|
|
|
async def post_url(url: str, data: any, headers: dict = None):
|
|
|
|
|
'''发送POST请求。
|
|
|
|
|
:param url: 需要发送的url。
|
|
|
|
|
:param data: 需要发送的数据。
|
|
|
|
|
:param headers: 请求时使用的http头。
|
|
|
|
|
:returns: 发送请求后的响应。'''
|
|
|
|
|
async with aiohttp.ClientSession(headers=headers) as session:
|
|
|
|
|
async with session.post(url, data=data, headers=headers) as req:
|
|
|
|
|
return await req.text()
|
|
|
|
|
|
|
|
|
|
|
2021-11-12 14:55:29 +00:00
|
|
|
|
@retry(stop=stop_after_attempt(3), wait=wait_fixed(3), reraise=True)
|
2021-11-01 15:54:12 +00:00
|
|
|
|
async def download_to_cache(link: str) -> Union[str, bool]:
|
2021-10-01 01:18:04 +00:00
|
|
|
|
'''利用AioHttp下载指定url的内容,并保存到缓存(./cache目录)。
|
2022-01-08 07:31:30 +00:00
|
|
|
|
|
2021-10-01 01:18:04 +00:00
|
|
|
|
:param link: 需要获取的link。
|
|
|
|
|
:returns: 文件的相对路径,若获取失败则返回False。'''
|
2021-07-21 17:58:33 +00:00
|
|
|
|
try:
|
2021-11-12 14:43:04 +00:00
|
|
|
|
async with aiohttp.ClientSession() as session:
|
2021-07-21 17:58:33 +00:00
|
|
|
|
async with session.get(link) as resp:
|
|
|
|
|
res = await resp.read()
|
|
|
|
|
ftt = ft.match(res).extension
|
|
|
|
|
path = abspath(f'./cache/{str(uuid.uuid4())}.{ftt}')
|
|
|
|
|
with open(path, 'wb+') as file:
|
|
|
|
|
file.write(res)
|
|
|
|
|
return path
|
|
|
|
|
except:
|
2022-01-20 13:31:50 +00:00
|
|
|
|
Logger.error(traceback.format_exc())
|
2021-07-21 17:58:33 +00:00
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2021-07-27 17:42:47 +00:00
|
|
|
|
def cache_name():
|
|
|
|
|
return abspath(f'./cache/{str(uuid.uuid4())}')
|
|
|
|
|
|
|
|
|
|
|
2021-10-01 01:18:04 +00:00
|
|
|
|
async def slk_converter(filepath: str) -> str:
|
|
|
|
|
'''将指定文件转为slk格式。
|
2022-01-08 07:31:30 +00:00
|
|
|
|
|
2021-10-01 01:18:04 +00:00
|
|
|
|
:param filepath: 需要获取的link。
|
|
|
|
|
:returns: 文件的相对路径。'''
|
2021-07-21 17:58:33 +00:00
|
|
|
|
filepath2 = filepath + '.silk'
|
|
|
|
|
Logger.info('Start encoding voice...')
|
|
|
|
|
os.system('python slk_coder.py ' + filepath)
|
|
|
|
|
Logger.info('Voice encoded.')
|
|
|
|
|
return filepath2
|
2021-08-19 12:17:48 +00:00
|
|
|
|
|
2021-08-21 15:58:07 +00:00
|
|
|
|
|
2021-12-31 14:44:34 +00:00
|
|
|
|
async def load_prompt(bot) -> None:
|
2021-08-22 14:55:25 +00:00
|
|
|
|
author_cache = os.path.abspath(PrivateAssets.path + '/cache_restart_author')
|
2021-10-14 15:18:47 +00:00
|
|
|
|
loader_cache = os.path.abspath(PrivateAssets.path + '/.cache_loader')
|
2021-08-19 12:17:48 +00:00
|
|
|
|
if os.path.exists(author_cache):
|
|
|
|
|
open_author_cache = open(author_cache, 'r')
|
2021-08-21 15:58:07 +00:00
|
|
|
|
author = json.loads(open_author_cache.read())['ID']
|
2021-08-19 12:17:48 +00:00
|
|
|
|
open_loader_cache = open(loader_cache, 'r')
|
2021-08-21 15:58:07 +00:00
|
|
|
|
m = await bot.fetch_target(author)
|
|
|
|
|
if m:
|
2022-01-26 08:54:57 +00:00
|
|
|
|
await m.sendDirectMessage(open_loader_cache.read())
|
2021-08-21 15:58:07 +00:00
|
|
|
|
open_loader_cache.close()
|
|
|
|
|
open_author_cache.close()
|
|
|
|
|
os.remove(author_cache)
|
2021-11-12 14:25:53 +00:00
|
|
|
|
os.remove(loader_cache)
|