from fugashi_parser import *
import os
from pathlib import Path
import shutil
import hashlib
destination_directory_name = '/run/media/deck/YF8SD/Sync/Notebooks/Obsidian/Japanese/Yu-Gi-Oh/Cards/'
destination_path = Path(destination_directory_name)
#print(annotate_phrase("私は学校へ行きます"))
## Make sure directories exist
if not destination_path.exists():
destination_path.mkdir(parents=True, exist_ok=True)
if not destination_path.joinpath('_attachments').exists():
destination_path.joinpath('_attachments').mkdir(parents=True, exist_ok=True)
if not destination_path.joinpath('vocab').exists():
destination_path.joinpath('vocab').mkdir(parents=True, exist_ok=True)
with open("dump-withFuri.json", "r", encoding="utf-16") as f:
dump = json.load(f)
vocab = {} #load_vocab('foo.json')
for cd in dump:
## The japanese card name
print(dump[cd]['name_J'])
## copy the image if it isn't already there
if Path('YGO_2020/2020.full.illust_j.jpg.zib/' + cd + '.jpg').exists():
##TODO: Ew
if Path(destination_path.joinpath('_attachments/' + cd + '.jpg')).exists() == False:
shutil.copy2(Path('YGO_2020/2020.full.illust_j.jpg.zib/' + cd + '.jpg'), destination_path.joinpath('_attachments'))
with open(destination_path.as_posix() + '/' + cd + '.md', 'w') as card_note:
## add card names as properties
card_note.write("---\n")
card_note.write(f"aliases: [{dump[cd]['name_J']},{dump[cd]['name_E']}]\n")
card_note.write("---\n")
## Name at the top
card_note.write(f"![[{cd}.jpg]]") ## card image
card_note.write("\n#### Name\n")
card_note.write("")
card_note.write(dump[cd]['name_J'])
card_note.write("
")
card_note.write(dump[cd]['name_E'])
card_note.write(" ")
## the description
card_note.write("\n#### Details\n")
card_note.write("")
card_note.write(dump[cd]['desc_J'])
card_note.write("
")
card_note.write(dump[cd]['desc_E'])
card_note.write(" ")
# desc_Furi
card_note.write("\n#### Reading\n")
vocab = {} ## flush the vocab variable each time
result = analyze(dump[cd]['name_J'],vocab)
print(f"{result['ruby_html']}\n> [!summary]- Vocab\n>{result['notes_link']}\n",file=card_note)
## Generate the vocab item
for word in vocab:
if Path(destination_path.joinpath('vocab/' + word + '.md')).exists() == False:
definitions = anki_fields_term(word)
if definitions.get("fields"):
with open(destination_path.as_posix() + '/vocab/' + word + '.md', 'w') as vocab_note:
for entry in definitions.get("fields"):
print(entry['furigana'], file=vocab_note)
if entry['expression'] != entry['reading']:
print(entry['reading'], file=vocab_note)
print("> ",re.sub(r'<.*?>', '', entry['glossary-plain-no-dictionary']), "\n", file=vocab_note)
print(f"Frequency: {entry['frequency-average-rank']}", file=vocab_note)
vocab_note.write(entry['glossary-first'])
print("\n", file=vocab_note)
# card_note.write("##### Glossary\n")
# for vocab in dump[cd]["glossary_Name_J"]:
# print(f"{vocab} : {dump[cd]["glossary_Name_J"][vocab]}", file=card_note)
vocab = {} ## flush the vocab variable each time
result = analyze(dump[cd]['desc_J'],vocab)
#card_note.write("\n")
print(f"{result['ruby_html']}\n> [!summary]- Vocab\n>{result['notes_link']}\n",file=card_note)
for word in vocab:
if Path(destination_path.joinpath('vocab/' + word + '.md')).exists() == False:
definitions = anki_fields_term(word)
if definitions.get("fields"):
with open(destination_path.as_posix() + '/vocab/' + word + '.md', 'w') as vocab_note:
for entry in definitions.get("fields"):
print(entry['furigana'], file=vocab_note)
if entry['expression'] != entry['reading']:
print(entry['reading'], file=vocab_note)
print("> ",re.sub(r'<.*?>', '', entry['glossary-plain-no-dictionary']), "\n", file=vocab_note)
print(f"Frequency: {entry['frequency-average-rank']}", file=vocab_note)
vocab_note.write(entry['glossary-first'])
print("\n", file=vocab_note)
# if(dump[cd].get("desc_Furi")):
# card_note.write(f"{dump[cd]['desc_Furi']}\n")
# else:
# card_note.write(dump[cd]['desc_J'])
# card_note.write("\n")
# card_note.write("##### Glossary\n")
# for vocab in dump[cd]["glossary_Desc_J"]:
# vocab_hash = hashlib.shake_256((vocab + 'Jitendex.org [2026-04-04]').encode()).hexdigest(4)
# ## Only search and create a vocab file if it doesn't exist
# if Path(destination_path.joinpath('vocab/' + vocab_hash + '.md')).exists() == False:
# definitions = anki_fields_term(vocab)
# if definitions.get("fields"):
# with open(destination_path.as_posix() + '/vocab/' + vocab_hash + '.md', 'w') as vocab_note:
# for entry in definitions.get("fields"):
# vocab_note.write("\n")
# print(entry['expression'], file=vocab_note)
# #print(entry['furigana-plain'], file=vocab_note)
# if entry['expression'] != entry['reading']:
# print(entry['reading'], file=vocab_note)
# print(entry['furigana'], file=vocab_note)
# #print(f"Part of Speech: {entry['part-of-speech']}", file=vocab_note)
# print(f"Frequency: {entry['frequency-average-rank']}", file=vocab_note)
# #print(entry['glossary-plain-no-dictionary'], file=vocab_note)
# vocab_note.write(entry['glossary-first'])
# print("##### Other Definitions", file=vocab_note)
# print(entry['glossary-plain-no-dictionary'], file=vocab_note)
# print(f"[[{vocab_hash}|{vocab}]] : {dump[cd]["glossary_Desc_J"][vocab]}", file=card_note)
#print('>' + dump[cd]["glossary_Desc_J"][vocab], file=card_note)
card_note.write("\n")
# foo = anki_fields_term(surface)
# for entry in foo.get("fields"):
# entry['expression']
# entry['glossary-first-brief']
# entry['glossary-plain-no-dictionary']
# entry['frequency-average-rank']