From 88d7ea0ac40eea702de5bbacf847cd60628554b0 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 11 Aug 2020 12:09:59 +0200 Subject: [PATCH] Fixed api interface --- cogs/search.py | 7 ++++--- utils/jisho.py | 24 +++++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/cogs/search.py b/cogs/search.py index 58dc437..08b2558 100644 --- a/cogs/search.py +++ b/cogs/search.py @@ -12,14 +12,15 @@ class Search(commands.Cog): if query == None: return - response = jisho.query(query) - await ctx.send(response["data"][0]["slug"]) + response = jisho.JishoResponse(query) + await ctx.send(response.nodes[0].slug) @search.error async def search_error(self, ctx, error): if isinstance(error, commands.CommandOnCooldown): - pass # Suppress that annoying exception everytime someone is on cooldown + return # Suppress that annoying exception everytime someone is on cooldown + raise error def setup(bot: commands.Bot): diff --git a/utils/jisho.py b/utils/jisho.py index 7d46530..9afc3da 100644 --- a/utils/jisho.py +++ b/utils/jisho.py @@ -16,25 +16,35 @@ class JishoNode(): self.is_common = node["is_common"] self.tags = node["tags"] self.jlpt = node["jlpt"] - self.japanese_word = node["japanese"]["word"] - self.japanese_reading = node["japanese"]["reading"] + self.japanese = [] + for entry in node["japanese"]: + if "word" not in entry: + word = entry["reading"] + reading = "" + else: + word = entry["word"] + reading = entry["reading"] + + self.japanese.append((word, reading)) self.senses = [] - for sense in node["senses"] + + for sense in node["senses"]: self.senses.append(JishoSenses(sense)) class JishoResponse(): def __init__(self, query: str): - self.query = query - self.raw = query() + self.query_string = query + self.raw = self.query() self.nodes = [] - disassemble() + self.disassemble() def query(self): - url = TEMPLATE_URL.format(urllib.parse.quote_plus(self.query)) + url = TEMPLATE_URL.format(urllib.parse.quote_plus(self.query_string)) r = requests.get(url) if r.status_code != 200: print(f"ERROR: Failed to access Jisho API... {r.status_code}") + return None return json.loads(r.text)