From c4b285dec600b994f4436f864038733a4245a932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20H=C3=BCgel?= Date: Thu, 18 May 2017 22:04:47 +0100 Subject: [PATCH] Dump HTML snapshots with unique file names based on item key This change also alters dump() to return the path and file name, see discussion in #75 --- doc/index.rst | 4 ++-- pyzotero/zotero.py | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index 6e047c6..d539461 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -315,9 +315,9 @@ Retrieving Files A convenient wrapper around :py:meth:`Zotero.file()`. Writes an attachment to disk using the optional path and filename. If neither are supplied, the file is written to the current working directory, and a :py:meth:`Zotero.item()` call is first made to determine the attachment - filename. No error checking is done regarding the path. + filename. No error checking is done regarding the path. If successful, the full path including the file name is returned. - .. note:: HTML snapshots will be dumped as zip files, with "zip" appended to the file name. + .. note:: HTML snapshots will be dumped as zip files. These will be named with their API item key, and a .zip extension. .. code-block:: python diff --git a/pyzotero/zotero.py b/pyzotero/zotero.py index 9083c3a..f6a7a69 100644 --- a/pyzotero/zotero.py +++ b/pyzotero/zotero.py @@ -645,17 +645,18 @@ def dump(self, itemkey, filename=None, path=None): Dump a file attachment to disk, with optional filename and path """ if not filename: - filename = self.item(itemkey)["data"]["filename"] + try: + filename = self.item(itemkey)['data']['filename'] + except TypeError: + filename = "{i}.zip".format(i=itemkey) if path: pth = os.path.join(path, filename) else: pth = filename - file = self.file(itemkey) - if self.snapshot: - self.snapshot = False - pth = pth + ".zip" - with open(pth, "wb") as f: - f.write(file) + to_write = self.file(itemkey) + with open(pth, 'wb') as f: + f.write(to_write) + return pth @retrieve def children(self, item, **kwargs):