obsolete/exp_anki.py
changeset 555 4a3188fc8951
child 667 5f69f0776c37
equal deleted inserted replaced
554:59714b9033bc 555:4a3188fc8951
       
     1 FONAME = "test.apkg"
       
     2 
       
     3 # Looks like anki libs change working directory to media directory of current deck
       
     4 # Therefore absolute path should be stored before creating temporary deck
       
     5 FONAME = os.path.abspath(FONAME)
       
     6 FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))
       
     7 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))
       
     8 
       
     9 import anki
       
    10 from anki.exporting import AnkiPackageExporter
       
    11 
       
    12 collection = anki.Collection(os.path.join(TMPDIR, 'collection.anki2'))
       
    13 
       
    14 deck_id = collection.decks.id(FBASENAME + "_deck")
       
    15 deck = collection.decks.get(deck_id)
       
    16 # deck = collection.decks.confForDid(deck_id)
       
    17 # collection.decks.update(deck)
       
    18 # print(dir(deck))
       
    19 # print(type(deck))
       
    20 # print(deck)
       
    21 
       
    22 model = collection.models.new(FBASENAME + "_model")
       
    23 model['tags'].append(FBASENAME + "_tag")
       
    24 model['did'] = deck_id
       
    25 model['css'] = """
       
    26 .card {
       
    27   font-family: arial;
       
    28   font-size: 20px;
       
    29   text-align: center;
       
    30   color: black;
       
    31   background-color: white;
       
    32 }
       
    33 .from {
       
    34   font-style: italic;
       
    35 }
       
    36 """
       
    37 
       
    38 collection.models.addField(model, collection.models.newField('en'))
       
    39 collection.models.addField(model, collection.models.newField('tr'))
       
    40 
       
    41 tmpl = collection.models.newTemplate('en -> tr')
       
    42 tmpl['qfmt'] = '<div class="from">{{en}}</div>'
       
    43 tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n{{tr}}'
       
    44 collection.models.addTemplate(model, tmpl)
       
    45 tmpl = collection.models.newTemplate('tr -> en')
       
    46 tmpl['qfmt'] = '{{tr}}'
       
    47 tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n<div class="from">{{en}}</div>'
       
    48 collection.models.addTemplate(model, tmpl)
       
    49 
       
    50 
       
    51 print(dir(model))
       
    52 print(type(model))
       
    53 print(model)
       
    54 # Equivalent of:
       
    55 # collection.models.add(model)
       
    56 # without setting auto-generated ID:
       
    57 model['id'] = 12345678  # essential for upgrade detection
       
    58 collection.models.update(model)
       
    59 collection.models.setCurrent(model)
       
    60 collection.models.save(model)
       
    61 
       
    62 # collection.decks.select(deck_id)
       
    63 
       
    64 note = anki.notes.Note(collection, model)
       
    65 print(dir(note))
       
    66 print(type(note))
       
    67 print(note._fmap)
       
    68 print(note)
       
    69 
       
    70 
       
    71 note['en'] = "hello"
       
    72 note['tr'] = u"[heləʊ]\nint. привет"
       
    73 note.guid = "xxx1"
       
    74 collection.addNote(note)
       
    75 print(dir(note))
       
    76 
       
    77 note = collection.newNote()
       
    78 note['en'] = "bye"
       
    79 note['tr'] = u"[baɪ]\nint. пока"
       
    80 note.guid = "xxx2"
       
    81 collection.addNote(note)
       
    82 
       
    83 # model.add(deck)
       
    84 # model.save()
       
    85 
       
    86 export = AnkiPackageExporter(collection)
       
    87 export.exportInto(FONAME)
       
    88 
       
    89 cleanup()