Fixed api interface

This commit is contained in:
Robert 2020-08-11 12:09:59 +02:00
parent e2b42cbabc
commit 88d7ea0ac4
2 changed files with 21 additions and 10 deletions

View file

@ -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):

View file

@ -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)