Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/utils/bot.py

84 lines
2.4 KiB
Python
Raw Normal View History

2021-08-23 12:44:31 +00:00
import json
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-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-02-06 16:30:13 +00:00
2021-08-21 15:58:07 +00:00
from core.elements import FetchTarget
2021-08-23 12:44:31 +00:00
from core.logger import Logger
2021-08-21 15:58:07 +00:00
2021-02-06 16:30:13 +00:00
2021-08-19 12:17:48 +00:00
class PrivateAssets:
2021-08-24 10:22:36 +00:00
path = os.path.abspath('.')
2021-08-19 12:17:48 +00:00
@staticmethod
def set(path):
2021-08-24 10:22:36 +00:00
path = os.path.abspath(path)
2021-08-19 12:17:48 +00:00
if not os.path.exists(path):
os.mkdir(path)
PrivateAssets.path = path
2021-04-28 12:21:33 +00:00
2021-08-07 07:56:48 +00:00
2021-08-21 15:58:07 +00:00
def init():
version = os.path.abspath(PrivateAssets.path + '/version')
write_version = open(version, 'w')
write_version.write(os.popen('git rev-parse HEAD', 'r').read()[0:7])
write_version.close()
tag = os.path.abspath(PrivateAssets.path + '/version_tag')
write_tag = open(tag, 'w')
write_tag.write(os.popen('git tag -l', 'r').read().split('\n')[-2])
write_tag.close()
2021-02-11 12:41:07 +00:00
async def get_url(url: str, headers=None):
2021-02-06 16:30:13 +00:00
async with aiohttp.ClientSession() 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-02-06 16:30:13 +00:00
text = await req.text()
return text
2021-04-27 15:01:17 +00:00
2021-07-21 17:58:33 +00:00
async def download_to_cache(link):
try:
async with aiohttp.ClientSession() as session:
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:
traceback.print_exc()
return False
2021-07-27 17:42:47 +00:00
def cache_name():
return abspath(f'./cache/{str(uuid.uuid4())}')
2021-07-21 17:58:33 +00:00
async def slk_converter(filepath):
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
async def load_prompt(bot: FetchTarget):
2021-08-22 14:55:25 +00:00
author_cache = os.path.abspath(PrivateAssets.path + '/cache_restart_author')
2021-08-19 12:17:48 +00:00
loader_cache = os.path.abspath('.cache_loader')
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:
await m.sendMessage(open_loader_cache.read())
open_loader_cache.close()
open_author_cache.close()
os.remove(author_cache)
os.remove(loader_cache)