Skip to content

Instantly share code, notes, and snippets.

@EpixMan
Last active August 4, 2022 22:20
Show Gist options
  • Save EpixMan/f62085324cece742fdb0461e7e21ef59 to your computer and use it in GitHub Desktop.
Save EpixMan/f62085324cece742fdb0461e7e21ef59 to your computer and use it in GitHub Desktop.
This python code can link Jotoba to Anki using Anki connect addon to make creating cards much easier
'''
notes:
1- Make a deck and change it name in the code, make sure anki is running and anki connect add on is already installed
2- You might need to install requests in python using "pip install requests" without quotes
4- Make sure to create a note type, make fields same as fields variable, change modelname to be your note type
3- You just past the kanji in the python code and it will automatically import the
A- the Kanji
B-the Onyomi/Kunyomi
C- the meaning of the kanji word
D- Also the parts of the kanji if the request was only for one letter
Aother important note: if you get {'result': [None], 'error': None}
then it means that you have a problem either:
1- you already have the card
2- The fields aren't correct (case and position sensitive)
3- The deck name in code doesnt exist (create one as in note no.1)
'''
import requests
import json
deck = "GENKI"
modelName = "JotobaTest"
while True:
user_input = input()
headers = {
'Accept': 'application/json',
}
json_data = {
"query": f"{user_input}",
"language": "English",
"no_english": False
}
response = requests.post('https://jotoba.de/api/search/words', headers=headers, json=json_data)
reqres = response.json()
if len(user_input) == 1:
for x in reqres["kanji"]:
if x["literal"] == user_input:
if 'onyomi' in x:
onyomi = str(x['onyomi']).replace('[', '').replace(']', '').replace('\'', '').replace('\"', '')
else:
onyomi = ""
if 'kunyomi' in x:
kunyomi = str(x['kunyomi']).replace('[', '').replace(']', '').replace('\'', '').replace('\"', '')
else:
kunyomi = ""
thekanji = ' ' + user_input + ' '
kana = onyomi+kunyomi
meaning = str(x['meanings']).replace('[', '').replace(']', '').replace('\'', '').replace('\"','')
parts = str(x['parts']).replace('[', '').replace(']', '').replace('\'', '').replace('\"', '').replace(',', '')
else:
for x in reqres["words"]:
if x["reading"]["kanji"] == user_input:
kana = x["reading"]["kana"]
meaning = str(x['senses'][0]['glosses']).replace('[', '').replace(']', '').replace('\'', '').replace('\"', '')
parts = ""
thekanji = user_input
fields = {
"Kanji": thekanji,
"Meaning": meaning,
"Kana": kana,
"Mnemonic": "",
"Radicals": parts
}
print(fields)
export = {
"action": "addNotes",
"version": 6,
"params": {
"notes": [
{
"deckName": deck,
"modelName": modelName,
"fields": fields,
"tags": [
]
}
]
}
}
ff = requests.post("http://127.0.0.1:8765/", f"{json.dumps(export)}")
gg = ff.json()
print(gg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment