Skip to content

Instantly share code, notes, and snippets.

@kanjieater
Last active August 18, 2019 21:20
Show Gist options
  • Save kanjieater/33044e7dc57c82abefcc932ee66706d3 to your computer and use it in GitHub Desktop.
Save kanjieater/33044e7dc57c82abefcc932ee66706d3 to your computer and use it in GitHub Desktop.
automate mia japanese calls
from aqt import mw
from aqt.qt import QAction, QKeySequence
from anki.hooks import addHook
buttonText = "-------------Bulk-add Routine------------"
ROUTE_1_KEY = 'Ctrl+9'
def setupMenu(browser):
from importlib import reload
from . import autobulk
reload(autobulk)
a = QAction(buttonText, browser)
a.triggered.connect(lambda: autobulk.run_auto_bulk(browser))
browser.form.menuEdit.addSeparator()
browser.form.menuEdit.addAction(a)
a.setShortcut(QKeySequence(ROUTE_1_KEY))
addHook("browser.setupMenus", setupMenu)
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QAction
from anki.hooks import addHook
from aqt import mw
# from aqt.utils import showText
TAGS_TO_DELETE = ['外国語', 'mmi', 'mmk', 'beta', 'beta7']
TAGS_TO_ADD = ['mm', 'beta8']
FIELDS_TO_CLEAR = ['Reading']
MIA_FIELD = 'Reading'
def noop(*args):
pass
def run_routine(browser):
nids = browser.selectedNotes()
cids = browser.selectedCards()
# 0
clean_up(nids, cids, browser)
# 1
run_vocab(nids)
# 2
run_jsh(nids)
# 3
run_japanese(nids)
# # 4
run_phonetics(nids)
# 5
run_pronunciation(nids)
# 6
run_freq(nids)
# 7
run_keywords(nids)
# 8
run_images(nids)
def run_auto_bulk(browser):
mw.checkpoint("AutoBulk")
# monkeypatch before the bactch
backup_checkpoint = mw.checkpoint
mw.checkpoint = noop
run_routine(browser)
# restore sanity (monkeypatch)
mw.checkpoint = backup_checkpoint
def clean_up(nids, cids, browser):
browser.col.sched.unsuspendCards(cids)
# browser.col.setUserFlag(3, cids) #green
browser.model.reset()
browser.mw.requireReset()
for nid in nids:
note = mw.col.getNote(nid)
for tag in TAGS_TO_DELETE:
note.delTag(tag)
for tag in TAGS_TO_ADD:
note.addTag(tag)
note.flush()
def run_vocab(nids):
try:
import autovocab
except:
raise Exception('Failed to import vocab module')
autovocab.autovocab.copy_vocab(nids)
def run_jsh(nids):
try:
# jsh = __import__("jsentencehighlighter")
import jsentencehighlighter as jsh
except:
raise Exception('Failed to import jsentencehighlighter module')
jsh.highlightSentences(nids)
# def run_japanese(nids):
# try:
# import bulk_japanese
# except:
# raise Exception('Failed to import japanese module')
# bulk_japanese.bulkreading.regenerateReadings(nids, False)
def run_japanese(nids):
try:
import MIAJapaneseSupport as japanese
import bulk_japanese
except:
raise Exception('Failed to import japanese module')
bulk_japanese.bulkreading.regenerateReadings(nids, False)
# self.kanaMode, self.dictMode, self.pitchMode, self.audioMode, self.graphMode
japanese.main.MExporter.generate([False, True, False, False, False], MIA_FIELD, nids)
def run_phonetics(nids):
try:
import phonetics
except:
raise Exception('Failed to import phonetics module')
phonetics.phonetics.regeneratePhonetics(nids)
def run_pronunciation(nids):
try:
pronunciation = __import__('932119536')
except:
raise Exception('Failed to import pronunciation module')
pronunciation.nhk_pronunciation.regeneratePronunciations(nids)
def run_freq(nids):
try:
frequency = __import__('1612642956')
except:
raise Exception('Failed to import freq module')
frequency.Bulk_Generate_Vocab_Frequency.bulkGenerateVocabFq(nids)
def run_keywords(nids):
try:
import rtk
except:
raise Exception('Failed to import keywords module')
rtk.rtkkw.regenerateKeywords(nids)
def run_images(nids):
try:
import anki_image
except:
raise Exception('Failed to import images module')
anki_image.anki_image.anki_adder.fetchImage(nids, True)
#add this to the bottom of massExporter.py (make sure it's indented to be a part of the class)
def generate(self, checkboxBools, field, notes):
dest = field
addType = 'OVERWRITE'
progWid, bar = self.getProgressWidget()
bar.setMinimum(0)
bar.setMaximum(len(notes))
val = 0;
for nid in notes:
note = self.mw.col.getNote(nid)
fields = self.mw.col.models.fieldNames(note.model())
if field in fields and dest in fields:
text = note[field]
newText = text
text = text.replace('</div> <div>', '</div><div>')
htmlFinds, text = self.exporter.htmlRemove(text)
text, sounds = self.exporter.removeBrackets(text, True)
text = text.replace(',&', '-co-am-')
text, invalids = self.exporter.replaceInvalidChars(text);
text = self.mw.col.media.strip(text).encode("utf-8", "ignore")
results = self.dictParser.getParsed(text)
results = self.exporter.wordData(results)
text, audioGraphList = self.dictParser.dictBasedParsing(results, newText, False, checkboxBools)
if htmlFinds:
text = self.exporter.replaceHTML(text, htmlFinds)
for match in sounds:
text = text.replace("-_-AUDIO-_-", match, 1)
if text:
text = self.exporter.returnInvalids(text, invalids)
text = text.replace('-co-am-', ',&').strip()
if addType == 'If Empty':
if note[dest] == '':
note[dest] = text
elif addType == 'Add':
if note[dest] == '':
note[dest] = text
else:
note[dest] += '<br>' + text
else:
note[dest] = text
if audioGraphList:
self.exporter.addVariants(audioGraphList, note)
if text or audioGraphList:
note.flush()
val+=1;
bar.setValue(val)
self.mw.app.processEvents()
self.mw.progress.finish()
self.mw.reset()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment