PK!\ anki_export/__init__.pyfrom .app import ApkgReader PK!?manki_export/app.pyimport sqlite3 from zipfile import ZipFile import os from tempfile import mkdtemp import shutil from collections import OrderedDict import json class ApkgReader: def __init__(self, apkg_path: str): self.temp_dir = mkdtemp() with ZipFile(apkg_path) as zf: zf.extractall(self.temp_dir) self.conn = sqlite3.connect(os.path.join(self.temp_dir, 'collection.anki2')) self.conn.row_factory = sqlite3.Row self.models = None self.decks = None self.init() def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.close() def __next__(self): return self def __iter__(self): return self.notes def close(self): self.conn.close() shutil.rmtree(self.temp_dir) def init(self): cursor = self.conn.execute('SELECT * FROM col') col = next(cursor) self.models = json.loads(col['models']) self.decks = json.loads(col['decks']) def find_model_by_id(self, mid): return self.models.get(str(mid), None) def find_deck_by_id(self, did): return self.decks.get(str(did), None) def find_note_by_id(self, nid): cursor = self.conn.execute('SELECT * FROM notes WHERE id=?', (nid,)) return self._format_note(next(cursor)) @property def notes(self): for note in self.conn.execute('SELECT * FROM notes'): yield self._format_note(note) def _format_note(self, row): note = OrderedDict(row) header = self._model_to_header(self.find_model_by_id(note['mid'])) note.update({ 'data': { 'record': OrderedDict(zip(header, note['flds'].split('\x1f'))) } }) return note @staticmethod def _model_to_header(model): return [x['name'] for x in sorted(model['flds'], key=lambda x: x['ord'])] def find_card_by_id(self, cid): cursor = self.conn.execute('SELECT * FROM cards WHERE id=?', (cid,)) return self._format_card(next(cursor)) @property def cards(self): for card in self.conn.execute('SELECT * FROM cards'): yield self._format_card(card) def _format_card(self, row): card = OrderedDict(row) card.update({ 'data': { 'note': self.find_note_by_id(card['nid']), 'deck': self.find_deck_by_id(card['did']) } }) return card def cards_by_ord(self, ord=0): for formatted_card in self.cards: if formatted_card['ord'] == ord: yield formatted_card def export(self, has_header=True, has_deck=True, ord=0): result = OrderedDict() if has_header: for model in self.models.values(): header = list() header.extend(self._model_to_header(model)) header.append('tags') if has_deck: header.append('deck') result[model['name']] = [header] for formatted_card in self.cards_by_ord(ord=ord): record = list() record.extend(formatted_card['data']['note']['flds'].split('\x1f')) record.append(formatted_card['data']['note']['tags']) if has_deck: record.append(self.find_deck_by_id(formatted_card['did'])['name']) result[self.find_model_by_id(formatted_card['data']['note']['mid'])['name']].append(record) return result if __name__ == '__main__': import pyexcel_xlsxwx from time import time start = time() with ApkgReader('/Users/patarapolw/PycharmProjects/AnkiTools/tests/input/test.apkg') as apkg: pyexcel_xlsxwx.save_data('test.xlsx', apkg.export(), config={'format': None}) print(time() - start) PK! ::#anki_export-0.1.1.dist-info/LICENSEMIT License Copyright (c) 2018 Pacharapol Withayasakpunt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HMWX!anki_export-0.1.1.dist-info/WHEEL A н#Z@Z|Jl~6蓅 Λ MU4[PYBpYD*Mͯ#ڪ/̚?ݭ'%nPK!HB$anki_export-0.1.1.dist-info/METADATATao6_q@ 8"ym0lFAbC;$tXS$KRa}w  {wiQV2=)YTLdH3W.Q|=9J Ӯm 8MÈ\y ›>6esL-fNT|ry=D(0>/Šu8b쥳zeJ~9'Zf .g0r{?@x[{ٶp!M]_O_xo~ވ{#^x%Pz");&UhqHWva8xdsVV:B޺3oTfU T~FЋu,Q,uX.B,TlCLeG S!><{[C8<8N1T?&G=5sfBZK2uD^o2(SO);JnMكT>w*",!*1W乶zw@!cNW &is[[ħߞKcU74w]]t4B1֤twǻ<|X!ZR;llsOXJ:Œڔ%`T)C>kj|<b?9E=<|MJiN ԑg~T1˟2gHcR+(g^V,wk}m$e[9#>M 5 ưalz;|H/Qs?"b"d ؽǐRsyI"G/UjB{ B 796GbrEki9}w ' -VoTPK!Hb%"anki_export-0.1.1.dist-info/RECORD}̽r0g ra!-"D¡U<}x}O߾Y$0RE]>b=jxs[@-Mҗ-(8H٧Gkt.S+u&Sk%Bm͜%&-yވ5o v B{N]}WWs)o"yZㆅ1|x41LWPlDQPKV}]ZXPUͽ2r E #E|F.#}N PK!\ anki_export/__init__.pyPK!?mQanki_export/app.pyPK! ::#anki_export-0.1.1.dist-info/LICENSEPK!HMWX!anki_export-0.1.1.dist-info/WHEELPK!HB$anki_export-0.1.1.dist-info/METADATAPK!Hb%" anki_export-0.1.1.dist-info/RECORDPKn