Added Kun/On readings to kanji search
This commit is contained in:
parent
d3cbf5b5ab
commit
b333bbad41
|
@ -2,9 +2,68 @@ import discord
|
|||
from discord.ext import commands
|
||||
from utils import jisho
|
||||
|
||||
class JishoKanjiObject():
|
||||
def __init__(self, query):
|
||||
self.response = jisho.JishoKanji(query)
|
||||
self.total_pages = self.response.entries
|
||||
self.page = 0
|
||||
|
||||
def prev(self):
|
||||
self.page -= 1
|
||||
if self.page < 0:
|
||||
self.page = self.total_pages - 1
|
||||
|
||||
def next(self):
|
||||
self.page += 1
|
||||
if self.page >= self.total_pages:
|
||||
self.page = 0
|
||||
|
||||
|
||||
class Kanji(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.activeObject = None
|
||||
self.latestMessage = 0
|
||||
|
||||
async def createEmbed(self):
|
||||
response = self.activeObject.response
|
||||
node = response.nodes[self.activeObject.page]
|
||||
|
||||
embed = discord.Embed(
|
||||
title = node.kanji,
|
||||
url = node.url,
|
||||
colour = 0x56d926
|
||||
)
|
||||
|
||||
if node.kun:
|
||||
embed.add_field(name="Kun", value=", ".join(node.kun))
|
||||
if node.on:
|
||||
embed.add_field(name="On", value=", ".join(node.on))
|
||||
|
||||
embed.set_footer(text=f"{self.activeObject.page + 1}/{self.activeObject.total_pages}")
|
||||
|
||||
return embed
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_reaction_add(self, reaction, user):
|
||||
message = reaction.message
|
||||
if message.id != self.latestMessage:
|
||||
return
|
||||
|
||||
if user == self.bot.user:
|
||||
return
|
||||
|
||||
if reaction.me:
|
||||
if reaction.emoji == "⬅️":
|
||||
self.activeObject.prev()
|
||||
await reaction.remove(user)
|
||||
|
||||
if reaction.emoji == "➡️":
|
||||
self.activeObject.next()
|
||||
await reaction.remove(user)
|
||||
|
||||
embed = await self.createEmbed()
|
||||
await message.edit(embed=embed)
|
||||
|
||||
@commands.command(name="kanji", description="Performs a Kanji search", usage="<kanji>", aliases=["k"])
|
||||
@commands.cooldown(1, 5)
|
||||
|
@ -12,8 +71,14 @@ class Kanji(commands.Cog):
|
|||
if kanji is None:
|
||||
return
|
||||
|
||||
response = jisho.JishoKanji(kanji)
|
||||
await ctx.send(response.entries)
|
||||
self.activeObject = JishoKanjiObject(kanji)
|
||||
embed = await self.createEmbed()
|
||||
message = await ctx.send(embed=embed)
|
||||
self.latestMessage = message.id
|
||||
|
||||
if self.activeObject.total_pages > 1:
|
||||
await message.add_reaction("⬅️")
|
||||
await message.add_reaction("➡️")
|
||||
|
||||
def setup(bot):
|
||||
bot.add_cog(Kanji(bot))
|
|
@ -67,15 +67,15 @@ class Search(commands.Cog):
|
|||
if reaction.me:
|
||||
if reaction.emoji == "⬅️":
|
||||
self.activeObject.prev()
|
||||
await reaction.remove(user)
|
||||
|
||||
if reaction.emoji == "➡️":
|
||||
self.activeObject.next()
|
||||
await reaction.remove(user)
|
||||
|
||||
embed = await self.createEmbed()
|
||||
await message.edit(embed=embed)
|
||||
|
||||
await reaction.remove(user)
|
||||
|
||||
|
||||
@commands.command(name="search", description="Searches Jisho", usage="<query>", aliases=["s"])
|
||||
@commands.cooldown(1, 5)
|
||||
|
@ -87,8 +87,10 @@ class Search(commands.Cog):
|
|||
embed = await self.createEmbed()
|
||||
message = await ctx.send(embed=embed)
|
||||
self.latestMessage = message.id
|
||||
await message.add_reaction("⬅️")
|
||||
await message.add_reaction("➡️")
|
||||
|
||||
if self.activeObject.total_pages > 1:
|
||||
await message.add_reaction("⬅️")
|
||||
await message.add_reaction("➡️")
|
||||
|
||||
@search.error
|
||||
async def search_error(self, ctx, error):
|
||||
|
|
|
@ -86,8 +86,9 @@ class JishoKanjiNode():
|
|||
def __init__(self):
|
||||
# Information about the Kanji
|
||||
self.kanji = ""
|
||||
self.on = []
|
||||
self.url = "https://jisho.org/search/"
|
||||
self.kun = []
|
||||
self.on = []
|
||||
|
||||
class JishoKanji():
|
||||
def __init__(self, query):
|
||||
|
@ -100,8 +101,8 @@ class JishoKanji():
|
|||
self.query()
|
||||
|
||||
def query(self):
|
||||
url = TEMPLATE_KANJI_URL.format(urllib.parse.quote_plus(self.query_string + "#kanji"))
|
||||
r = requests.get(url, headers=HEADER)
|
||||
self.url = TEMPLATE_KANJI_URL.format(urllib.parse.quote_plus(self.query_string + "#kanji"))
|
||||
r = requests.get(self.url, headers=HEADER)
|
||||
|
||||
if r.status_code != 200:
|
||||
print(f"ERROR: Failed to access Jisho API... {r.status_code}")
|
||||
|
@ -112,4 +113,24 @@ class JishoKanji():
|
|||
info_blocks = body.find_all("div", {"class": "kanji details"})
|
||||
|
||||
for info in info_blocks:
|
||||
block = BeautifulSoup(str(info), features="html.parser")
|
||||
self.entries += 1
|
||||
self.nodes.append(JishoKanjiNode())
|
||||
|
||||
self.nodes[-1].kanji = info.findChild("h1").string
|
||||
self.nodes[-1].url += urllib.parse.quote_plus(self.nodes[-1].kanji + "#kanji")
|
||||
|
||||
readings_block = info.findChild("div", {"class": "kanji-details__main-readings"}, recursive=True)
|
||||
|
||||
# Kun Yomi
|
||||
kun_block = readings_block.findChild("dl", {"class": "dictionary_entry kun_yomi"}, recursive=True)
|
||||
if kun_block != None:
|
||||
readings = kun_block.findChildren("a", recursive=True)
|
||||
for reading in readings:
|
||||
self.nodes[-1].kun.append(reading.string)
|
||||
|
||||
# On Yomi
|
||||
on_block = readings_block.findChild("dl", {"class": "dictionary_entry on_yomi"}, recursive=True)
|
||||
if on_block != None:
|
||||
readings = on_block.findChildren("a", recursive=True)
|
||||
for reading in readings:
|
||||
self.nodes[-1].on.append(reading.string)
|
||||
|
|
Loading…
Reference in a new issue