From e1e7a575bd59390f53aa2cd427dd6425a22fdcd9 Mon Sep 17 00:00:00 2001 From: nielstron Date: Fri, 5 Jan 2018 18:17:29 +0100 Subject: [PATCH 1/6] Added additional information for wolfram alpha Given the official api gets upgraded or you enter an api_key in the config file wolframalpha now receives plenty of additional data. [Assumptions](https://products.wolframalpha.com/api/documentation/#assumption-DateOrder) based on your settings for date formats [Location](https://products.wolframalpha.com/api/documentation/#specifying-your-location) based on your settings in home.mycroft, given you add the key 'forward_location' : true to the configuration [Automatic unit conversion](https://products.wolframalpha.com/api/documentation/#top) based on your preferences in home.mycroft --- __init__.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/__init__.py b/__init__.py index 4c9dac9..81adb6f 100644 --- a/__init__.py +++ b/__init__.py @@ -75,8 +75,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)) @@ -102,9 +102,11 @@ def __init_client(self): if appID and not self.config.get('proxy'): # 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 +150,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,8 +162,41 @@ def handle_fallback(self, message): return False try: + # Make several assumptions based on the user settings + params = () + + # TODO ask user if he/she wants location forwarded => settings + if self.config.get("forward_location"): + + 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 should be considered when delivering an appId + res = self.client.query(query, params) result = self.get_result(res) except HTTPError as e: if e.response.status_code == 401: From 9391b8a841d93f859fc694c403545c1f40853306 Mon Sep 17 00:00:00 2001 From: nielstron Date: Fri, 5 Jan 2018 18:21:00 +0100 Subject: [PATCH 2/6] Added a manual on how to configure wolfram alpha --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 28a7f22..7e19eca 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,20 @@ For deeper exploration, you can also request the sources behind the answer. An email will be sent to your registered account linking you to all the details within Wolfram Alpha's website. +## Configuration + +Add a block to your `~/.mycroft/mycroft.conf` file like this: + +``` + "WolframAlphaSkill": { + "api_key": "yoursuperscretetAppID", + # to make use of your api key turn proxy off + "proxy": true, + # Turn on to improve answers based on current location + "forward_location": false + } +``` + ## Examples * "How tall is Mount Everest?" * "When was The Rocky Horror Picture Show released?" From 4e8e3519860becda67b886b3ac19aeb9ae9a8af6 Mon Sep 17 00:00:00 2001 From: nielstron Date: Fri, 5 Jan 2018 18:28:43 +0100 Subject: [PATCH 3/6] Added a settingsmeta file for automatic config registration --- settingsmeta.json | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 settingsmeta.json diff --git a/settingsmeta.json b/settingsmeta.json new file mode 100644 index 0000000..d56e9c4 --- /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": "bool", + "label": "Use mycroft proxy instead of own appID", + "value": "true" + }, + { + "name": "forward_location", + "type": "bool", + "label": "forward location to WolframAlpha", + "value": "false" + } + ] + } + ] + } +} \ No newline at end of file From a27800ceb3c2def9273730a168ff56d1e2657d55 Mon Sep 17 00:00:00 2001 From: nielstron Date: Fri, 9 Feb 2018 12:08:33 +0100 Subject: [PATCH 4/6] Making use of "checkbox" fields --- settingsmeta.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/settingsmeta.json b/settingsmeta.json index d56e9c4..c2913da 100644 --- a/settingsmeta.json +++ b/settingsmeta.json @@ -14,13 +14,13 @@ }, { "name": "proxy", - "type": "bool", + "type": "checkbox", "label": "Use mycroft proxy instead of own appID", "value": "true" }, { "name": "forward_location", - "type": "bool", + "type": "checkbox", "label": "forward location to WolframAlpha", "value": "false" } From fdd60adc3b499ee5d6b00360df04c4a587b9d24b Mon Sep 17 00:00:00 2001 From: nielstron Date: Fri, 9 Feb 2018 12:25:44 +0100 Subject: [PATCH 5/6] Make active use of the settingsmeta and ignore configuration --- __init__.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/__init__.py b/__init__.py index 81adb6f..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 @@ -92,15 +93,11 @@ 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) @@ -165,8 +162,8 @@ def handle_fallback(self, message): # Make several assumptions based on the user settings params = () - # TODO ask user if he/she wants location forwarded => settings - if self.config.get("forward_location"): + # ask user if he/she wants location forwarded => settings + if self.settings.get("forward_location") == "true": latlong = self.config_core.get('location')['coordinate'] params += ( @@ -195,13 +192,14 @@ def handle_fallback(self, message): self.enclosure.mouth_think() # Params thus get ignored if using the official API (by now) - # but should be considered when delivering an appId + # 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 From 824fcccf6a008b95e3771bc75e4558455a1d23e7 Mon Sep 17 00:00:00 2001 From: nielstron Date: Fri, 9 Feb 2018 12:27:06 +0100 Subject: [PATCH 6/6] Update README.md --- README.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/README.md b/README.md index 7e19eca..28a7f22 100644 --- a/README.md +++ b/README.md @@ -10,20 +10,6 @@ For deeper exploration, you can also request the sources behind the answer. An email will be sent to your registered account linking you to all the details within Wolfram Alpha's website. -## Configuration - -Add a block to your `~/.mycroft/mycroft.conf` file like this: - -``` - "WolframAlphaSkill": { - "api_key": "yoursuperscretetAppID", - # to make use of your api key turn proxy off - "proxy": true, - # Turn on to improve answers based on current location - "forward_location": false - } -``` - ## Examples * "How tall is Mount Everest?" * "When was The Rocky Horror Picture Show released?"