diff --git a/skills/games/akinator/nlu/en.json b/skills/games/akinator/nlu/en.json index a8a249b9..71e67e05 100644 --- a/skills/games/akinator/nlu/en.json +++ b/skills/games/akinator/nlu/en.json @@ -121,6 +121,11 @@ } }, "answers": { + "network_error": [ + "I cannot reach Akinator, you may verify your network.", + "Akinator isn't reachable at the moment. Please check your network.", + "It looks like your network cannot reach Akinator." + ], "calling_akinator": [ "I'm calling my friend Akinator...", "Alright, let me call Akinator, wait a sec...", diff --git a/skills/games/akinator/src/actions/guess.py b/skills/games/akinator/src/actions/guess.py index 5f83355e..ef865c90 100644 --- a/skills/games/akinator/src/actions/guess.py +++ b/skills/games/akinator/src/actions/guess.py @@ -4,9 +4,6 @@ import utils from ..lib import akinator, db -# TODO: catch network error -# TODO: timeout on question/answer - def guess(params): """Guess according to the given thematic""" diff --git a/skills/games/akinator/src/actions/setup.py b/skills/games/akinator/src/actions/setup.py index f1a5e3d7..ef9e39df 100644 --- a/skills/games/akinator/src/actions/setup.py +++ b/skills/games/akinator/src/actions/setup.py @@ -15,21 +15,24 @@ def setup(params): if thematic != 'characters': theme_lang = lang + '_' + thematic - aki = akinator.Akinator() + try: + aki = akinator.Akinator() - q = aki.start_game(theme_lang) + q = aki.start_game(theme_lang) - db.upsert_session({ - 'response': aki.response, - 'session': aki.session, - 'progression': aki.progression, - 'signature': aki.signature, - 'uri': aki.uri, - 'timestamp': aki.timestamp, - 'server': aki.server, - 'child_mode': aki.child_mode, - 'frontaddr': aki.frontaddr, - 'question_filter': aki.question_filter - }) + db.upsert_session({ + 'response': aki.response, + 'session': aki.session, + 'progression': aki.progression, + 'signature': aki.signature, + 'uri': aki.uri, + 'timestamp': aki.timestamp, + 'server': aki.server, + 'child_mode': aki.child_mode, + 'frontaddr': aki.frontaddr, + 'question_filter': aki.question_filter + }) - return utils.output('end', q, { 'showNextActionSuggestions': True }) + return utils.output('end', q, { 'showNextActionSuggestions': True }) + except: + return utils.output('end', 'network_error') diff --git a/skills/games/akinator/src/lib/akinator.py b/skills/games/akinator/src/lib/akinator.py index 4b285acf..9dd33459 100644 --- a/skills/games/akinator/src/lib/akinator.py +++ b/skills/games/akinator/src/lib/akinator.py @@ -32,6 +32,8 @@ try: except ImportError: pass +request_timeout = 5 # secs + #* URLs for the API requests NEW_SESSION_URL = "https://{}/new_session?callback=jQuery331023608747682107778_{}&urlApiWs={}&partner=1&childMod={}&player=website-desktop&uid_ext_session={}&frontaddr={}&constraint=ETAT<>'AV'&soft_constraint={}&question_filter={}" ANSWER_URL = "https://{}/answer_api?callback=jQuery331023608747682107778_{}&urlApiWs={}&childMod={}&session={}&signature={}&step={}&answer={}&frontaddr={}&question_filter={}" @@ -96,7 +98,7 @@ class Akinator(): """Get uid and frontaddr from akinator.com/game""" info_regex = re.compile("var uid_ext_session = '(.*)'\\;\\n.*var frontaddr = '(.*)'\\;") - r = requests.get("https://en.akinator.com/game") + r = requests.get("https://en.akinator.com/game", timeout=request_timeout) match = info_regex.search(r.text) self.uid, self.frontaddr = match.groups()[0], match.groups()[1] @@ -109,7 +111,7 @@ class Akinator(): bad_list = ["https://srv12.akinator.com:9398/ws"] while True: - r = requests.get("https://" + uri) + r = requests.get("https://" + uri, timeout=request_timeout) match = server_regex.search(r.text) parsed = json.loads(match.group().split("'arrUrlThemesToPlay', ")[-1]) @@ -166,7 +168,7 @@ class Akinator(): self._get_session_info() - r = requests.get(NEW_SESSION_URL.format(self.uri, self.timestamp, self.server, str(self.child_mode).lower(), self.uid, self.frontaddr, soft_constraint, self.question_filter), headers=HEADERS) + r = requests.get(NEW_SESSION_URL.format(self.uri, self.timestamp, self.server, str(self.child_mode).lower(), self.uid, self.frontaddr, soft_constraint, self.question_filter), headers=HEADERS, timeout=request_timeout) self.response = r.text resp = self._parse_response(r.text) @@ -188,7 +190,7 @@ class Akinator(): """ ans = ans_to_id(ans) - r = requests.get(ANSWER_URL.format(self.uri, self.timestamp, self.server, str(self.child_mode).lower(), self.session, self.signature, self.step, ans, self.frontaddr, self.question_filter), headers=HEADERS) + r = requests.get(ANSWER_URL.format(self.uri, self.timestamp, self.server, str(self.child_mode).lower(), self.session, self.signature, self.step, ans, self.frontaddr, self.question_filter), headers=HEADERS, timeout=request_timeout) self.response = r.text resp = self._parse_response(r.text) @@ -206,7 +208,7 @@ class Akinator(): if self.step == 0: raise CantGoBackAnyFurther("You were on the first question and couldn't go back any further") - r = requests.get(BACK_URL.format(self.server, self.timestamp, str(self.child_mode).lower(), self.session, self.signature, self.step, self.question_filter), headers=HEADERS) + r = requests.get(BACK_URL.format(self.server, self.timestamp, str(self.child_mode).lower(), self.session, self.signature, self.step, self.question_filter), headers=HEADERS, timeout=request_timeout) self.response = r.text resp = self._parse_response(r.text) @@ -225,7 +227,7 @@ class Akinator(): It's recommended that you call this function when Aki's progression is above 85%, which is when he will have most likely narrowed it down to just one choice. You can get his current progression via "Akinator.progression" """ - r = requests.get(WIN_URL.format(self.server, self.timestamp, str(self.child_mode).lower(), self.session, self.signature, self.step), headers=HEADERS) + r = requests.get(WIN_URL.format(self.server, self.timestamp, str(self.child_mode).lower(), self.session, self.signature, self.step), headers=HEADERS, timeout=request_timeout) self.response = r.text resp = self._parse_response(r.text)