[V3 Wolfram] Small updates
This commit is contained in:
@@ -1,16 +1,19 @@
|
|||||||
import os
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import os
|
||||||
from redbot.core import Config, commands, checks
|
from redbot.core import Config, commands, checks
|
||||||
|
from redbot.core.utils.chat_formatting import box
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
|
||||||
BaseCog = getattr(commands, "Cog", object)
|
BaseCog = getattr(commands, "Cog", object)
|
||||||
|
|
||||||
|
|
||||||
class Wolfram(BaseCog):
|
class Wolfram(BaseCog):
|
||||||
"""Ask Wolfram Alpha a question."""
|
"""Ask Wolfram Alpha any question."""
|
||||||
|
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
self.session = aiohttp.ClientSession()
|
||||||
|
|
||||||
default_global = {"WOLFRAM_API_KEY": None}
|
default_global = {"WOLFRAM_API_KEY": None}
|
||||||
|
|
||||||
@@ -18,21 +21,18 @@ class Wolfram(BaseCog):
|
|||||||
self.config.register_guild(**default_global)
|
self.config.register_guild(**default_global)
|
||||||
|
|
||||||
@commands.command(name="wolfram", aliases=["ask"])
|
@commands.command(name="wolfram", aliases=["ask"])
|
||||||
async def _wolfram(self, ctx, *arguments: str):
|
async def _wolfram(self, ctx, *question: str):
|
||||||
"""
|
"""Ask Wolfram Alpha any question."""
|
||||||
Ask Wolfram Alpha any question.
|
|
||||||
"""
|
|
||||||
api_key = await self.config.WOLFRAM_API_KEY()
|
api_key = await self.config.WOLFRAM_API_KEY()
|
||||||
|
|
||||||
if api_key:
|
if api_key:
|
||||||
url = "http://api.wolframalpha.com/v2/query?"
|
url = "http://api.wolframalpha.com/v2/query?"
|
||||||
query = " ".join(arguments)
|
query = " ".join(question)
|
||||||
payload = {"input": query, "appid": api_key}
|
payload = {"input": query, "appid": api_key}
|
||||||
headers = {"user-agent": "Red-cog/2.0.0"}
|
headers = {"user-agent": "Red-cog/2.0.0"}
|
||||||
conn = aiohttp.TCPConnector(verify_ssl=False)
|
async with self.session.get(url, params=payload, headers=headers) as r:
|
||||||
session = aiohttp.ClientSession(connector=conn)
|
|
||||||
async with session.get(url, params=payload, headers=headers) as r:
|
|
||||||
result = await r.text()
|
result = await r.text()
|
||||||
session.close()
|
|
||||||
root = ET.fromstring(result)
|
root = ET.fromstring(result)
|
||||||
a = []
|
a = []
|
||||||
for pt in root.findall(".//plaintext"):
|
for pt in root.findall(".//plaintext"):
|
||||||
@@ -43,17 +43,17 @@ class Wolfram(BaseCog):
|
|||||||
else:
|
else:
|
||||||
message = "\n".join(a[0:3])
|
message = "\n".join(a[0:3])
|
||||||
else:
|
else:
|
||||||
message = (
|
message = "No API key set for Wolfram Alpha. Get one at http://products.wolframalpha.com/api/"
|
||||||
"No API key set for Wolfram Alpha. Get one at http://products.wolframalpha.com/api/"
|
await ctx.send(box(message))
|
||||||
)
|
|
||||||
await ctx.send("```{0}```".format(message))
|
|
||||||
|
|
||||||
@commands.command(name="setwolframapi", aliases=["setwolfram"])
|
|
||||||
@checks.is_owner()
|
@checks.is_owner()
|
||||||
|
@commands.command(name="setwolframapi", aliases=["setwolfram"])
|
||||||
async def _setwolframapi(self, ctx, key: str):
|
async def _setwolframapi(self, ctx, key: str):
|
||||||
"""
|
"""Set the api-key."""
|
||||||
Set the api-key.
|
|
||||||
"""
|
|
||||||
if key:
|
if key:
|
||||||
await self.config.WOLFRAM_API_KEY.set(key)
|
await self.config.WOLFRAM_API_KEY.set(key)
|
||||||
await ctx.send("Key set.")
|
await ctx.send("Key set.")
|
||||||
|
|
||||||
|
def __unload(self):
|
||||||
|
self.bot.loop.create_task(self.session.close())
|
||||||
|
|||||||
Reference in New Issue
Block a user