From 9ba5c08c965885222613ebf45c0edf38ab50b3ac Mon Sep 17 00:00:00 2001 From: Markus Reil Date: Tue, 17 Mar 2020 12:29:49 +0700 Subject: [PATCH] Support apkg export (#143) Support apkg export --- .gitignore | 1 + README.md | 30 ++++++++++++++++++++++++++++++ plugin/__init__.py | 24 ++++++++++++++++++++++++ tests/test_export.py | 21 +++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100755 tests/test_export.py diff --git a/.gitignore b/.gitignore index 2b18174..34ac317 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pyc AnkiConnect.zip meta.json +.idea/ diff --git a/README.md b/README.md index e06134b..06a8bbb 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ with the latest stable (2.1.x) releases of Anki; older versions (2.0.x and below * [Cards](https://foosoft.net/projects/anki-connect/#cards) * [Media](https://foosoft.net/projects/anki-connect/#media) * [Graphical](https://foosoft.net/projects/anki-connect/#graphical) + * [Export](https://foosoft.net/projects/anki-connect/#export) ## Installation ## @@ -1866,3 +1867,32 @@ guarantee that your application continues to function properly in the future. "error": null } ``` + +### Export ### + +* **exportPackage** + + Exports a given deck in .apkg format. Returns `true` if successful or `false` otherwise. + The optional property `includeSched` (default is `false`) can be specified to include the + cards' scheduling data. + + *Sample request*: + ```json + { + "action": "exportPackage", + "version": 6, + "params": { + "deck": "Default", + "path": "/data/Deck.apkg", + "includeSched": true + } + } + ``` + + *Sample result*: + ```json + { + "result": true, + "error": null + } + ``` diff --git a/plugin/__init__.py b/plugin/__init__.py index d945661..107188d 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -29,8 +29,11 @@ from PyQt5.QtCore import QTimer from PyQt5.QtWidgets import QMessageBox import anki +import anki.exporting import anki.lang +import anki.storage import aqt +from anki.exporting import AnkiPackageExporter from . import web, util @@ -1198,6 +1201,27 @@ class AnkiConnect: return results + @util.api() + def exportPackage(self, deck, path, includeSched=False): + results = [] + results.append(self.createPackage(deck, path, includeSched)) + + return results + + + def createPackage(self, deck, path, includeSched): + collection = self.collection() + if collection is not None: + deck = collection.decks.byName(deck) + if deck is not None: + exporter = AnkiPackageExporter(collection) + exporter.did = deck["id"] + exporter.includeSched = includeSched + exporter.exportInto(path) + return True + return False + + # # Entry # diff --git a/tests/test_export.py b/tests/test_export.py new file mode 100755 index 0000000..a13a2b1 --- /dev/null +++ b/tests/test_export.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +import os +import tempfile +import unittest + +import util + + +class TestExport(unittest.TestCase): + def runTest(self): + fd, newname = tempfile.mkstemp(prefix="testexport", suffix=".apkg") + os.close(fd) + os.unlink(newname) + success = util.invoke('exportPackage', deck='Default', path=newname) + self.assertTrue(success) + self.assertTrue(os.path.exists(newname)) + + +if __name__ == '__main__': + unittest.main()