media importing

This commit is contained in:
Damien Elmes 2012-02-26 08:13:48 +09:00
parent d7c58f8142
commit 54b8ee059f
2 changed files with 10 additions and 6 deletions

View file

@ -240,9 +240,7 @@ insert into revlog values (?,?,?,?,?,?,?,?,?)""", revlog)
######################################################################
def _importMedia(self):
self.log.append(
_("%d media imported.") %
self.src.media.copyTo(self.dst.media.dir()))
self.src.media.copyTo(self.dst.media.dir())
# Post-import cleanup
######################################################################

View file

@ -2,7 +2,7 @@
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import zipfile
import zipfile, simplejson, os
from anki.utils import tmpfile
from anki.importing.anki2 import Anki2Importer
@ -11,9 +11,15 @@ class AnkiPackageImporter(Anki2Importer):
def run(self):
# extract the deck from the zip file
z = zipfile.ZipFile(self.file)
f = z.open("collection.anki2")
col = z.read("collection.anki2")
colpath = tmpfile(suffix=".anki2")
open(colpath, "w").write(f.read())
open(colpath, "wb").write(col)
# pass it to the anki2 importer
self.file = colpath
Anki2Importer.run(self)
# import media
media = simplejson.loads(z.read("media"))
for c, file in media.items():
path = os.path.join(self.col.media.dir(), file)
if not os.path.exists(path):
open(path, "wb").write(z.read(c))