2023-04-28 10:01:27 +00:00
|
|
|
import wolframalpha
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from .logging import Logger
|
|
|
|
|
|
|
|
from typing import Dict, List, Tuple, Generator, Optional
|
|
|
|
|
|
|
|
class WolframAlpha:
|
|
|
|
api_key: str
|
|
|
|
logger: Logger
|
|
|
|
client: wolframalpha.Client
|
|
|
|
|
2023-05-01 06:11:43 +00:00
|
|
|
api_code: str = "wolfram"
|
|
|
|
calculation_api: str = "alpha"
|
|
|
|
|
|
|
|
operator: str = "Wolfram ([https://www.wolfram.com](https://www.wolfram.com))"
|
|
|
|
|
2023-04-28 10:01:27 +00:00
|
|
|
def __init__(self, api_key: str, logger: Optional[Logger] = None):
|
|
|
|
self.api_key: str = api_key
|
|
|
|
self.logger: Logger = logger or Logger()
|
|
|
|
self.client = wolframalpha.Client(self.api_key)
|
|
|
|
|
2023-05-01 06:11:43 +00:00
|
|
|
def generate_calculation_response(self, query: str, text: Optional[bool] = False, results_only: Optional[bool] = False, user: Optional[str] = None) -> Generator[str | bytes, None, None]:
|
2023-04-28 10:01:27 +00:00
|
|
|
self.logger.log(f"Querying WolframAlpha for {query}")
|
|
|
|
|
|
|
|
response: wolframalpha.Result = self.client.query(query)
|
2023-05-01 06:11:43 +00:00
|
|
|
|
|
|
|
if not response.success:
|
|
|
|
yield "Could not process your query."
|
|
|
|
|
|
|
|
if response.didyoumeans:
|
|
|
|
yield "Did you mean: " + response.didyoumeans["didyoumean"][0]["#text"]
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
if response.error:
|
|
|
|
self.logger.log("Error in query to WolframAlpha: " + response.error, "error")
|
|
|
|
|
|
|
|
for pod in response.pods if not results_only else (response.results if len(list(response.results)) else response.pods):
|
2023-04-28 10:01:27 +00:00
|
|
|
self.logger.log(pod.keys(), "debug")
|
|
|
|
if pod.title:
|
|
|
|
yield "*" + pod.title + "*"
|
|
|
|
for subpod in pod.subpods:
|
|
|
|
self.logger.log(subpod.keys(), "debug")
|
|
|
|
if subpod.title:
|
|
|
|
yield "*" + subpod.title + "*"
|
|
|
|
if subpod.img and not text:
|
|
|
|
image = requests.get(subpod.img.src).content
|
|
|
|
yield image
|
|
|
|
elif subpod.plaintext:
|
|
|
|
yield "```\n" + subpod.plaintext + "\n```"
|