From 68bf4ccba50c2680e920bb112d3f0a35b42e8edf Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 11 Aug 2020 18:31:20 +0200 Subject: [PATCH] Finished Kanji search --- cogs/kanji.py | 8 +++++--- utils/jisho.py | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cogs/kanji.py b/cogs/kanji.py index 9f67ba1..801e3d2 100644 --- a/cogs/kanji.py +++ b/cogs/kanji.py @@ -38,11 +38,13 @@ class Kanji(commands.Cog): ) if node.kun: - embed.add_field(name="Kun", value=", ".join(node.kun), inline=False) + embed.add_field(name="Kun", value="、 ".join(node.kun), inline=False) if node.on: - embed.add_field(name="On", value=", ".join(node.on), inline=False) + embed.add_field(name="On", value="、 ".join(node.on), inline=False) - embed.set_footer(text=f"{self.activeObject.page + 1}/{self.activeObject.total_pages}") + embed.add_field(name=f"Radical: {node.radical[0]}", value=node.radical[1], inline=False) + + embed.set_footer(text=f"Jōyō kanji (Grade {node.grade}) | JLPT level {node.jlpt}\t\t{self.activeObject.page + 1}/{self.activeObject.total_pages}") return embed diff --git a/utils/jisho.py b/utils/jisho.py index 800cff5..ac2dd88 100644 --- a/utils/jisho.py +++ b/utils/jisho.py @@ -1,6 +1,7 @@ import requests import urllib.parse import json +import re from bs4 import BeautifulSoup TEMPLATE_URL = "https://jisho.org/api/v1/search/words?keyword={0}" @@ -90,7 +91,7 @@ class JishoKanjiNode(): self.meaning = "" self.kun = [] self.on = [] - self.radical = "" + self.radical = [] self.grade = "" self.jlpt = "" @@ -144,6 +145,14 @@ class JishoKanji(): self.nodes[-1].on.append(reading.string) # Radical - radical_block = readings_block.findChild("div", {"class": "radicals"}, recursive=True) - print(radical_block) + radical_block = info.findChild("div", {"class": "radicals"}, recursive=True).findChild("span") + self.nodes[-1].radical.append(re.sub(r'[ \n"]', "", radical_block.contents[2].string)) + self.nodes[-1].radical.append(re.sub(r'[ \n"]', "", radical_block.contents[1].string)) + + # JLPT/Grade info + grade_block = info.findChild("div", {"class": "grade"}, recursive=True) + if grade_block != None: + self.nodes[-1].grade = grade_block.findChild("strong").string[-1] + + self.nodes[-1].jlpt = info.findChild("div", {"class": "jlpt"}, recursive=True).findChild("strong").string