Skip to content

Commit

Permalink
Dump HTML snapshots with unique file names based on item key
Browse files Browse the repository at this point in the history
This change also alters dump() to return the path and file name, see
discussion in #75
  • Loading branch information
urschrei committed Apr 27, 2019
1 parent d7ab1cd commit c4b285d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions pyzotero/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c4b285d

Please sign in to comment.