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/modules/wolframalpha/__init__.py

40 lines
1.2 KiB
Python
Raw Normal View History

2023-04-30 11:58:58 +00:00
import asyncio
import wolframalpha
from config import Config
2023-04-30 14:22:49 +00:00
from core.builtins import Bot, Image
2023-04-30 11:58:58 +00:00
from core.component import module
2023-05-13 04:54:36 +00:00
from core.dirty_check import check_bool
2023-04-30 11:58:58 +00:00
client = wolframalpha.Client(Config('wolfram_alpha_appid'))
w = module(
'wolframalpha',
2023-06-01 17:08:10 +00:00
alias='wolfram',
2023-04-30 11:58:58 +00:00
developers=['Dianliang233'],
desc='{wolframalpha.help.desc}',
2023-05-13 02:42:45 +00:00
support_languages=['en_us'])
2023-04-30 11:58:58 +00:00
2023-05-13 02:42:45 +00:00
@w.handle('<query> {{wolframalpha.help}}')
2023-04-30 11:58:58 +00:00
async def _(msg: Bot.MessageSession):
query = msg.parsed_msg['<query>']
res = await asyncio.get_event_loop().run_in_executor(None, client.query, query)
2023-04-30 14:11:09 +00:00
details = res.details
answer = []
2023-04-30 14:22:49 +00:00
images = []
2023-04-30 14:11:09 +00:00
for title, detail in details.items():
2023-04-30 14:22:49 +00:00
if title == 'Plot':
continue
2023-04-30 14:11:09 +00:00
answer.append(f'{title}: {detail}')
2023-04-30 14:22:49 +00:00
# Parse out all images that don't have a plaintext counterpart
for pod in res.pods:
if pod.text is None and 'img' in pod.subpod:
images.append(pod.subpod['img']['@src'])
bot_images = [Image(image) for image in images]
2023-05-14 01:18:23 +00:00
if await check_bool(' '.join(answer)):
await msg.finish('https://wdf.ink/6OUp')
else:
await msg.finish(['\n'.join(answer), *bot_images])