2020-08-11 09:21:09 +00:00
|
|
|
import requests
|
|
|
|
import urllib.parse
|
|
|
|
import json
|
2020-08-11 14:56:19 +00:00
|
|
|
from bs4 import BeautifulSoup
|
2020-08-11 09:21:09 +00:00
|
|
|
|
|
|
|
TEMPLATE_URL = "https://jisho.org/api/v1/search/words?keyword={0}"
|
2020-08-11 14:56:19 +00:00
|
|
|
TEMPLATE_KANJI_URL = "https://jisho.org/search/{0}"
|
2020-08-11 12:07:12 +00:00
|
|
|
HEADER = {
|
|
|
|
"User-Agent": "Jisho Bot",
|
|
|
|
"From": "https://github.com/Lauchmelder23/JishoBot"
|
|
|
|
}
|
2020-08-11 09:21:09 +00:00
|
|
|
|
2020-08-11 14:56:19 +00:00
|
|
|
class JishoSense():
|
2020-08-11 09:51:36 +00:00
|
|
|
def __init__(self, sense):
|
|
|
|
self.english_definitions = sense["english_definitions"]
|
2020-08-11 11:11:46 +00:00
|
|
|
self.fenglish_definitions = "; ".join(self.english_definitions)
|
2020-08-11 09:51:36 +00:00
|
|
|
self.parts_of_speech = sense["parts_of_speech"]
|
2020-08-11 11:11:46 +00:00
|
|
|
self.fparts_of_speech = ", ".join(sense["parts_of_speech"])
|
|
|
|
if self.fparts_of_speech == "":
|
|
|
|
self.fparts_of_speech = "\u200b" # Zero width space to have empty embed value
|
2020-08-11 09:51:36 +00:00
|
|
|
|
|
|
|
class JishoNode():
|
|
|
|
def __init__(self, node):
|
|
|
|
self.slug = node["slug"]
|
2020-08-11 11:11:46 +00:00
|
|
|
if "is_common" in node:
|
|
|
|
self.is_common = node["is_common"]
|
|
|
|
else:
|
|
|
|
self.is_common = False
|
|
|
|
|
2020-08-11 09:51:36 +00:00
|
|
|
self.tags = node["tags"]
|
|
|
|
self.jlpt = node["jlpt"]
|
2020-08-11 11:11:46 +00:00
|
|
|
|
|
|
|
self.ftags = ""
|
|
|
|
if self.is_common:
|
|
|
|
self.ftags += "Common "
|
|
|
|
for tag in self.tags:
|
|
|
|
self.ftags += f"| {tag} "
|
|
|
|
for jlpt in self.jlpt:
|
|
|
|
self.ftags += f"| {jlpt} "
|
|
|
|
|
2020-08-11 10:09:59 +00:00
|
|
|
self.japanese = []
|
|
|
|
for entry in node["japanese"]:
|
|
|
|
if "word" not in entry:
|
|
|
|
word = entry["reading"]
|
|
|
|
reading = ""
|
2020-08-11 11:11:46 +00:00
|
|
|
elif "reading" not in entry:
|
|
|
|
word = entry["word"]
|
|
|
|
reading = ""
|
2020-08-11 10:09:59 +00:00
|
|
|
else:
|
|
|
|
word = entry["word"]
|
|
|
|
reading = entry["reading"]
|
|
|
|
|
|
|
|
self.japanese.append((word, reading))
|
2020-08-11 09:51:36 +00:00
|
|
|
self.senses = []
|
2020-08-11 10:09:59 +00:00
|
|
|
|
|
|
|
for sense in node["senses"]:
|
2020-08-11 14:56:19 +00:00
|
|
|
self.senses.append(JishoSense(sense))
|
2020-08-11 09:51:36 +00:00
|
|
|
|
|
|
|
class JishoResponse():
|
|
|
|
def __init__(self, query: str):
|
2020-08-11 10:09:59 +00:00
|
|
|
self.query_string = query
|
|
|
|
self.raw = self.query()
|
2020-08-11 09:51:36 +00:00
|
|
|
self.nodes = []
|
2020-08-11 10:09:59 +00:00
|
|
|
self.disassemble()
|
2020-08-11 09:51:36 +00:00
|
|
|
|
|
|
|
def query(self):
|
2020-08-11 10:09:59 +00:00
|
|
|
url = TEMPLATE_URL.format(urllib.parse.quote_plus(self.query_string))
|
2020-08-11 13:52:13 +00:00
|
|
|
r = requests.get(url, headers=HEADER)
|
2020-08-11 09:51:36 +00:00
|
|
|
|
|
|
|
if r.status_code != 200:
|
|
|
|
print(f"ERROR: Failed to access Jisho API... {r.status_code}")
|
2020-08-11 10:09:59 +00:00
|
|
|
return None
|
2020-08-11 09:51:36 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2020-08-11 14:56:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JishoKanjiNode():
|
|
|
|
def __init__(self):
|
|
|
|
# Information about the Kanji
|
|
|
|
self.kanji = ""
|
2020-08-11 15:34:22 +00:00
|
|
|
self.url = "https://jisho.org/search/"
|
2020-08-11 14:56:19 +00:00
|
|
|
self.kun = []
|
2020-08-11 15:34:22 +00:00
|
|
|
self.on = []
|
2020-08-11 14:56:19 +00:00
|
|
|
|
|
|
|
class JishoKanji():
|
|
|
|
def __init__(self, query):
|
|
|
|
self.query_string = query
|
|
|
|
|
|
|
|
# List of JishoKanjiNodes
|
|
|
|
self.nodes = []
|
|
|
|
self.entries = 0
|
|
|
|
|
|
|
|
self.query()
|
|
|
|
|
|
|
|
def query(self):
|
2020-08-11 15:34:22 +00:00
|
|
|
self.url = TEMPLATE_KANJI_URL.format(urllib.parse.quote_plus(self.query_string + "#kanji"))
|
|
|
|
r = requests.get(self.url, headers=HEADER)
|
2020-08-11 14:56:19 +00:00
|
|
|
|
|
|
|
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:
|
2020-08-11 15:34:22 +00:00
|
|
|
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)
|