summaryrefslogtreecommitdiffstats
path: root/attain_vocab_to_anki_py/wadoku_client.py
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2021-02-02 13:43:25 +0200
committerJan Tuomi <jan.tuomi@valuemotive.com>2021-02-02 13:43:25 +0200
commit1a1dc2a9dc2380fd04fa2f2da18ff9e6ac87647d (patch)
tree847eab756e01286e0f3f36b2a076902d770120df /attain_vocab_to_anki_py/wadoku_client.py
parent75c731c78880c5a31a50e166d33dcae03b76349a (diff)
Add Wadoku client and improve stuff
Diffstat (limited to 'attain_vocab_to_anki_py/wadoku_client.py')
-rw-r--r--attain_vocab_to_anki_py/wadoku_client.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/attain_vocab_to_anki_py/wadoku_client.py b/attain_vocab_to_anki_py/wadoku_client.py
new file mode 100644
index 0000000..550f801
--- /dev/null
+++ b/attain_vocab_to_anki_py/wadoku_client.py
@@ -0,0 +1,42 @@
+from bs4 import BeautifulSoup
+import requests
+import urllib.parse
+
+WADOKU_BASE_URL = "https://www.wadoku.de/search"
+
+def inject_hiragana_with_accents(entries: list) -> list:
+ entries_with_readings = []
+
+ for index, entry in enumerate(entries):
+ kanji = entry.kanji.replace("~", "")
+
+ if len(entry.hiragana) > 0:
+ print(f"Hiragana exists for #{index}: {kanji}. Skipping...")
+ entries_with_readings.append(entry)
+ continue
+
+ # Remove nasty characters from kanji since Wadoku
+ # fails to find anything otherwise
+ print(f"Fetching hiragana data for #{index}: {kanji}")
+
+ resource_part = urllib.parse.quote_plus(kanji)
+ url = f"{WADOKU_BASE_URL}/{resource_part}"
+ resp = requests.get(url)
+ resp.raise_for_status()
+
+ soup = BeautifulSoup(resp.text, "html.parser")
+ container = soup.select_one("section#content")
+
+ readings = container.select("span.reading")
+ first_reading = str(readings[0])
+
+ # Remove extra characters added by Wadoku
+ # https://www.wadoku.de/wiki/display/WAD/Hinweise+zur+Notation
+ result = first_reading \
+ .replace("~", "") \
+ .replace("|", "") \
+ .replace("・", "")
+ entry.hiragana = result
+ entries_with_readings.append(entry)
+
+ return entries_with_readings