Archived
1
0
Fork 0

Revert "Revert "test audio""

This reverts commit bee81ca0f0.
This commit is contained in:
yzhh 2021-04-05 23:04:42 +08:00
parent 19a07e1170
commit a5ed898ed1
3 changed files with 46 additions and 11 deletions

View file

@ -1,3 +1,8 @@
import traceback
import uuid
from os.path import abspath
import aiohttp
import eventlet
from graia.application import MessageChain, GroupMessage, FriendMessage
from graia.application.friend import Friend
@ -10,6 +15,9 @@ from core.loader import logger_info
from core.broadcast import app, bcc
from database import BotDB
import filetype as ft
from graiax import silkcoder
database = BotDB()
@ -154,6 +162,27 @@ def check_permission(kwargs):
return False
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
async def slk_converter(filepath):
filepath2 = filepath + '.silk'
await silkcoder.encode(filepath, filepath2)
return filepath2
async def Nudge(kwargs):
if Group in kwargs:
await app.sendNudge(kwargs[Member], kwargs[Group])

View file

@ -3,12 +3,12 @@ import re
from graia.application import MessageChain
from graia.application.friend import Friend
from graia.application.group import Group, Member
from graia.application.message.elements.internal import Image, UploadMethods
from graia.application.message.elements.internal import Image, Voice
from graia.application.message.elements.internal import Plain
from modules.wiki.database import WikiDB
import modules.wiki.wikilib
from core.template import sendMessage, check_permission, wait_confirm, revokeMessage, Nudge
from core.template import sendMessage, check_permission, wait_confirm, revokeMessage, Nudge, download_to_cache, slk_converter
from database import BotDB
from modules.wiki.helper import check_wiki_available
from .getinfobox import get_infobox_pic
@ -149,11 +149,7 @@ async def wiki_wrapper(kwargs: dict):
[Plain((prompt + '\n' if prompt else '') + (msg['url'] + '\n' if 'url' in msg else '') + msg['text'])])
if 'net_image' in msg:
try:
if Group in kwargs:
mth = UploadMethods.Group
elif Friend in kwargs:
mth = UploadMethods.Friend
imgchain = MessageChain.create([Image.fromNetworkAddress(msg['net_image'], method=mth)])
imgchain = MessageChain.create([Image.fromNetworkAddress(msg['net_image'])])
msgchain = msgchain.plusWith(imgchain)
except:
pass
@ -306,18 +302,17 @@ async def regex_wiki(kwargs: dict):
await Nudge(kwargs)
waitlist = []
imglist = []
audlist = []
urllist = {}
msglist = MessageChain.create([])
waitmsglist = MessageChain.create([])
if Group in kwargs:
table = 'start_wiki_link_group'
target = kwargs[Group].id
mth = UploadMethods.Group
headtable = 'request_headers_group'
if Friend in kwargs:
table = 'start_wiki_link_self'
target = kwargs[Friend].id
mth = UploadMethods.Friend
headtable = 'request_headers_self'
headers = database.config_headers('get', headtable, target)
for find in find_dict:
@ -390,6 +385,8 @@ async def regex_wiki(kwargs: dict):
msg['url'] + '\n' if 'url' in msg else '') + text)]))
if 'net_image' in msg:
imglist.append(msg['net_image'])
if 'net_audio' in msg:
audlist.append(msg['net_audio'])
if 'apilink' in msg:
get_link = msg['apilink']
if 'url' in msg:
@ -401,8 +398,12 @@ async def regex_wiki(kwargs: dict):
if imglist != []:
imgchain = MessageChain.create([])
for img in imglist:
imgchain = imgchain.plusWith(MessageChain.create([Image.fromNetworkAddress(img, method=mth)]))
imgchain = imgchain.plusWith(MessageChain.create([Image.fromNetworkAddress(img)]))
await sendMessage(kwargs, imgchain)
if audlist != []:
for aud in audlist:
audchain = MessageChain.create([Voice().fromLocalFile(await slk_converter(await download_to_cache(aud)))])
await sendMessage(kwargs, audchain)
if urllist != {}:
print(urllist)
check_options = bot_db.check_enable_modules_self(
@ -413,7 +414,7 @@ async def regex_wiki(kwargs: dict):
get_infobox = await get_infobox_pic(urllist[url], url, headers)
if get_infobox:
infoboxchain = infoboxchain.plusWith(
MessageChain.create([Image.fromLocalFile(get_infobox, method=mth)]))
MessageChain.create([Image.fromLocalFile(get_infobox)]))
if infoboxchain != MessageChain.create([]):
await sendMessage(kwargs, infoboxchain, Quote=False)
if global_status == 'warn':

View file

@ -301,6 +301,11 @@ class wikilib:
getimg = await self.get_image(self.pagename)
if getimg:
msgs['net_image'] = getimg
matchaud = re.match(r'File:.*?\.(?:oga|ogg|flac|mp3|wav)', self.pagename, re.I)
if matchaud:
getaud = await self.get_image(self.pagename)
if getaud:
msgs['net_audio'] = getaud
print(result)
if result != '' and await self.danger_text_check(result):
return {'status': 'done', 'text': 'https://wdf.ink/6OUp'}