obsolete/exp_anki.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Thu, 15 Sep 2016 20:13:18 +0300
changeset 558 53fd793e345d
parent 555 4a3188fc8951
child 667 5f69f0776c37
permissions -rw-r--r--
Add shortcut to deploy to HG repos.

FONAME = "test.apkg"

# Looks like anki libs change working directory to media directory of current deck
# Therefore absolute path should be stored before creating temporary deck
FONAME = os.path.abspath(FONAME)
FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))
TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))

import anki
from anki.exporting import AnkiPackageExporter

collection = anki.Collection(os.path.join(TMPDIR, 'collection.anki2'))

deck_id = collection.decks.id(FBASENAME + "_deck")
deck = collection.decks.get(deck_id)
# deck = collection.decks.confForDid(deck_id)
# collection.decks.update(deck)
# print(dir(deck))
# print(type(deck))
# print(deck)

model = collection.models.new(FBASENAME + "_model")
model['tags'].append(FBASENAME + "_tag")
model['did'] = deck_id
model['css'] = """
.card {
  font-family: arial;
  font-size: 20px;
  text-align: center;
  color: black;
  background-color: white;
}
.from {
  font-style: italic;
}
"""

collection.models.addField(model, collection.models.newField('en'))
collection.models.addField(model, collection.models.newField('tr'))

tmpl = collection.models.newTemplate('en -> tr')
tmpl['qfmt'] = '<div class="from">{{en}}</div>'
tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n{{tr}}'
collection.models.addTemplate(model, tmpl)
tmpl = collection.models.newTemplate('tr -> en')
tmpl['qfmt'] = '{{tr}}'
tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n<div class="from">{{en}}</div>'
collection.models.addTemplate(model, tmpl)


print(dir(model))
print(type(model))
print(model)
# Equivalent of:
# collection.models.add(model)
# without setting auto-generated ID:
model['id'] = 12345678  # essential for upgrade detection
collection.models.update(model)
collection.models.setCurrent(model)
collection.models.save(model)

# collection.decks.select(deck_id)

note = anki.notes.Note(collection, model)
print(dir(note))
print(type(note))
print(note._fmap)
print(note)


note['en'] = "hello"
note['tr'] = u"[heləʊ]\nint. привет"
note.guid = "xxx1"
collection.addNote(note)
print(dir(note))

note = collection.newNote()
note['en'] = "bye"
note['tr'] = u"[baɪ]\nint. пока"
note.guid = "xxx2"
collection.addNote(note)

# model.add(deck)
# model.save()

export = AnkiPackageExporter(collection)
export.exportInto(FONAME)

cleanup()