-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwolfram.py
45 lines (31 loc) · 1.32 KB
/
wolfram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# coding=utf8
from __future__ import unicode_literals
from sopel.config.types import StaticSection, ValidatedAttribute
from sopel.module import commands
from sopel import web
import wolframalpha
import json
output_ids = ['DecimalApproximation', 'Result', 'ExactResult']
class WolframSection(StaticSection):
app_id = ValidatedAttribute('app_id', default=None)
def configure(config):
config.define_section('wolfram', WolframSection, validate=False)
config.wolfram.configure_setting('app_id', 'Application ID')
def setup(bot):
bot.config.define_section('wolfram', WolframSection)
@commands('wa', 'wolfram')
def wa_query(bot, trigger):
if not trigger.group(2):
return bot.say('[Wolfram] You must provide a query')
client = wolframalpha.Client(bot.config.wolfram.app_id)
try:
result = client.query(trigger.group(2))
except Exception as e:
return bot.say('[Wolfram] An error occurred ({})'.format(e.message))
for pod in result.pods:
if pod.id not in output_ids:
continue
return bot.say('{}: {}'.format(pod.title, pod.text))
if len(result.pods) > 0:
return bot.say('[Wolfram] No text-representable result found, see http://wolframalpha.com/input/?i={}'.format(web.quote(trigger.group(2))))
return bot.say('[Wolfram] No results found.')