diff --git a/__init__.py b/__init__.py index 4c9dac9..0d7186f 100644 --- a/__init__.py +++ b/__init__.py @@ -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 @@ -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)) @@ -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): @@ -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: @@ -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 diff --git a/settingsmeta.json b/settingsmeta.json new file mode 100644 index 0000000..c2913da --- /dev/null +++ b/settingsmeta.json @@ -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" + } + ] + } + ] + } +} \ No newline at end of file