Added html parsing
This commit is contained in:
parent
66e8cc9250
commit
d3cbf5b5ab
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
.vscode
|
.vscode
|
||||||
__pycache__
|
__pycache__
|
||||||
*.json
|
*.json
|
||||||
|
*.html
|
|
@ -1,5 +1,6 @@
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
from utils import jisho
|
||||||
|
|
||||||
class Kanji(commands.Cog):
|
class Kanji(commands.Cog):
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
|
@ -7,8 +8,12 @@ class Kanji(commands.Cog):
|
||||||
|
|
||||||
@commands.command(name="kanji", description="Performs a Kanji search", usage="<kanji>", aliases=["k"])
|
@commands.command(name="kanji", description="Performs a Kanji search", usage="<kanji>", aliases=["k"])
|
||||||
@commands.cooldown(1, 5)
|
@commands.cooldown(1, 5)
|
||||||
async def kanji(self, ctx, *, kanji):
|
async def kanji(self, ctx, *, kanji: str = None):
|
||||||
return kanji
|
if kanji is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
response = jisho.JishoKanji(kanji)
|
||||||
|
await ctx.send(response.entries)
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
bot.add_cog(Kanji(bot))
|
bot.add_cog(Kanji(bot))
|
|
@ -1,14 +1,16 @@
|
||||||
import requests
|
import requests
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import json
|
import json
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
TEMPLATE_URL = "https://jisho.org/api/v1/search/words?keyword={0}"
|
TEMPLATE_URL = "https://jisho.org/api/v1/search/words?keyword={0}"
|
||||||
|
TEMPLATE_KANJI_URL = "https://jisho.org/search/{0}"
|
||||||
HEADER = {
|
HEADER = {
|
||||||
"User-Agent": "Jisho Bot",
|
"User-Agent": "Jisho Bot",
|
||||||
"From": "https://github.com/Lauchmelder23/JishoBot"
|
"From": "https://github.com/Lauchmelder23/JishoBot"
|
||||||
}
|
}
|
||||||
|
|
||||||
class JishoSenses():
|
class JishoSense():
|
||||||
def __init__(self, sense):
|
def __init__(self, sense):
|
||||||
self.english_definitions = sense["english_definitions"]
|
self.english_definitions = sense["english_definitions"]
|
||||||
self.fenglish_definitions = "; ".join(self.english_definitions)
|
self.fenglish_definitions = "; ".join(self.english_definitions)
|
||||||
|
@ -52,7 +54,7 @@ class JishoNode():
|
||||||
self.senses = []
|
self.senses = []
|
||||||
|
|
||||||
for sense in node["senses"]:
|
for sense in node["senses"]:
|
||||||
self.senses.append(JishoSenses(sense))
|
self.senses.append(JishoSense(sense))
|
||||||
|
|
||||||
class JishoResponse():
|
class JishoResponse():
|
||||||
def __init__(self, query: str):
|
def __init__(self, query: str):
|
||||||
|
@ -78,3 +80,36 @@ class JishoResponse():
|
||||||
for node in self.raw["data"]:
|
for node in self.raw["data"]:
|
||||||
self.nodes.append(JishoNode(node))
|
self.nodes.append(JishoNode(node))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class JishoKanjiNode():
|
||||||
|
def __init__(self):
|
||||||
|
# Information about the Kanji
|
||||||
|
self.kanji = ""
|
||||||
|
self.on = []
|
||||||
|
self.kun = []
|
||||||
|
|
||||||
|
class JishoKanji():
|
||||||
|
def __init__(self, query):
|
||||||
|
self.query_string = query
|
||||||
|
|
||||||
|
# List of JishoKanjiNodes
|
||||||
|
self.nodes = []
|
||||||
|
self.entries = 0
|
||||||
|
|
||||||
|
self.query()
|
||||||
|
|
||||||
|
def query(self):
|
||||||
|
url = TEMPLATE_KANJI_URL.format(urllib.parse.quote_plus(self.query_string + "#kanji"))
|
||||||
|
r = requests.get(url, headers=HEADER)
|
||||||
|
|
||||||
|
if r.status_code != 200:
|
||||||
|
print(f"ERROR: Failed to access Jisho API... {r.status_code}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
body = BeautifulSoup(r.text, features="html.parser")
|
||||||
|
|
||||||
|
info_blocks = body.find_all("div", {"class": "kanji details"})
|
||||||
|
|
||||||
|
for info in info_blocks:
|
||||||
|
block = BeautifulSoup(str(info), features="html.parser")
|
||||||
|
|
Loading…
Reference in a new issue