From e2b42cbabc26d83b52d29e9088d6603608734f17 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 11 Aug 2020 11:51:36 +0200 Subject: [PATCH] Added usable jisho interface --- utils/jisho.py | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/utils/jisho.py b/utils/jisho.py index 25e6571..7d46530 100644 --- a/utils/jisho.py +++ b/utils/jisho.py @@ -4,11 +4,44 @@ import json TEMPLATE_URL = "https://jisho.org/api/v1/search/words?keyword={0}" -def query(query: str): - url = TEMPLATE_URL.format(urllib.parse.quote_plus(query)) - r = requests.get(url) +class JishoSenses(): + def __init__(self, sense): + self.english_definitions = sense["english_definitions"] + self.english_definitions_formatted = "; ".join(self.english_definitions) + self.parts_of_speech = sense["parts_of_speech"] - if r.status_code != 200: - print(f"ERROR: Failed to access Jisho API... {r.status_code}") - - return json.loads(r.text) \ No newline at end of file +class JishoNode(): + def __init__(self, node): + self.slug = node["slug"] + 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.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.nodes = [] + disassemble() + + def query(self): + url = TEMPLATE_URL.format(urllib.parse.quote_plus(self.query)) + r = requests.get(url) + + if r.status_code != 200: + print(f"ERROR: Failed to access Jisho API... {r.status_code}") + + return json.loads(r.text) + + def disassemble(self): + self.status = self.raw["meta"]["status"] + self.entries = len(self.raw["data"]) + + for node in self.raw["data"]: + self.nodes.append(JishoNode(node)) +