Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import wolframalpha
from os.path import dirname, join
from requests import HTTPError
import ssl

from adapt.intent import IntentBuilder
from mycroft.api import Api
Expand Down Expand Up @@ -75,8 +76,8 @@ def __init__(self):
def get_data(self, response):
return response

def query(self, input):
data = self.request({"query": {"input": input}})
def query(self, input, params=()):
data = self.request({"query": {"input": input, "params": params}})
return wolframalpha.Result(StringIO(data.content))


Expand All @@ -92,19 +93,17 @@ def __init__(self):
self.last_answer = None

def __init_client(self):
# TODO: Storing skill-specific settings in mycroft.conf is deprecated.
# Should be stored in the skill's local settings.json instead.
appID = self.config.get('api_key')
if not appID:
# Attempt to get an AppID skill settings instead (normally this
# doesn't exist, but privacy-conscious might want to do this)
appID = self.settings.get('appID', None)

if appID and not self.config.get('proxy'):
# Attempt to get an AppID skill settings instead (normally this
# doesn't exist, but privacy-conscious might want to do this)
appID = self.settings.get('api_key', None)

if appID and self.settings.get('proxy') == "false":
# user has a private AppID
self.log.debug("Creating a private client")
self.client = wolframalpha.Client(appID)
else:
# use the default API for Wolfram queries
self.log.debug("Using the default API")
self.client = WAApi()

def initialize(self):
Expand Down Expand Up @@ -148,8 +147,9 @@ def handle_fallback(self, message):
# Try to store pieces of utterance (None if not parsed_question)
utt_word = parsed_question.get('QuestionWord')
utt_verb = parsed_question.get('QuestionVerb')
utt_query = parsed_question.get('Query')
query = "%s %s %s" % (utt_word, utt_verb, utt_query)
utt_query = parsed_question.get('Query')

query = "%s %s %s" % (utt_word, utt_verb, utt_query)
phrase = "know %s %s %s" % (utt_word, utt_query, utt_verb)
self.log.debug("Querying WolframAlpha: " + query)
else:
Expand All @@ -159,13 +159,47 @@ def handle_fallback(self, message):
return False

try:
# Make several assumptions based on the user settings
params = ()

# ask user if he/she wants location forwarded => settings
if self.settings.get("forward_location") == "true":

latlong = self.config_core.get('location')['coordinate']
params += (
('latlong', str(latlong['latitude'])+ "," + str(latlong['longitude'])),
)

# Based on the setting for the date format, assume certain things
if self.config_core.get('date_format') == 'MDY':
params += (('assumption', 'DateOrder_**Month.Day.Year--'),)
else:
params += (('assumption', 'DateOrder_**Day.Month.Year--'),)

# line to get the current settings for preferred units
# can be metric or imperial
# => ask wolfram alpha to convert the output
# to the preferred unit system
# based on self.config_core.get('system_unit')
if self.config_core.get('system_unit') == 'imperial':
params += (
('units', 'nonmetric'),
)
else:
params += (
('units', 'metric'),
)

self.enclosure.mouth_think()
res = self.client.query(query)
# Params thus get ignored if using the official API (by now)
# but will be considered when having delivering an appId
res = self.client.query(query, params)
result = self.get_result(res)
except HTTPError as e:
if e.response.status_code == 401:
self.emitter.emit(Message("mycroft.not.paired"))
return True
# except SSLError as e: why is there a read error happening
except Exception as e:
self.log.exception(e)
return False
Expand Down
31 changes: 31 additions & 0 deletions settingsmeta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"identifier": "WolframAlphaSkill",
"name": "WolframAlpha",
"skillMetadata": {
"sections": [
{
"name": "Login",
"fields": [
{
"name": "api_key",
"type": "text",
"label": "own appID",
"value": ""
},
{
"name": "proxy",
"type": "checkbox",
"label": "Use mycroft proxy instead of own appID",
"value": "true"
},
{
"name": "forward_location",
"type": "checkbox",
"label": "forward location to WolframAlpha",
"value": "false"
}
]
}
]
}
}