Skip to content

Commit

Permalink
Download attachments from page (#1283)
Browse files Browse the repository at this point in the history
* [Confluence] new method confluence_download_attachments_from_page

* [Confluence] new method confluence_download_attachments_from_page

* added example

---------

Co-authored-by: gkowalc <gkowalc>
  • Loading branch information
gkowalc authored Dec 12, 2023
1 parent 6a085e3 commit 2a8a77f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
32 changes: 32 additions & 0 deletions atlassian/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,38 @@ def attach_file(
comment=comment,
)

def download_attachments_from_page(self, page_id, path=None):
"""
Downloads all attachments from a page
:param page_id:
:param path: path to directory where attachments will be saved. If None, current working directory will be used.
:return info message: number of saved attachments + path to directory where attachments were saved:
"""
if path is None:
path = os.getcwd()
try:
attachments = self.get_attachments_from_content(page_id=page_id)["results"]
if not attachments:
return "No attachments found"
for attachment in attachments:
file_name = attachment["title"]
if not file_name:
file_name = attachment["id"] # if the attachment has no title, use attachment_id as a filename
download_link = self.url + attachment["_links"]["download"]
r = self._session.get(f"{download_link}")
file_path = os.path.join(path, file_name)
with open(file_path, "wb") as f:
f.write(r.content)
except NotADirectoryError:
raise NotADirectoryError("Verify if directory path is correct and/or if directory exists")
except PermissionError:
raise PermissionError(
"Directory found, but there is a problem with saving file to this directory. Check directory permissions"
)
except Exception as e:
raise e
return {"attachments downloaded": len(attachments), " to path ": path}

def delete_attachment(self, page_id, filename, version=None):
"""
Remove completely a file if version is None or delete version
Expand Down
3 changes: 3 additions & 0 deletions docs/confluence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ Page actions
# automatically version the new file and keep the old one
confluence.attach_content(content, name=None, content_type=None, page_id=None, title=None, space=None, comment=None)
# Download attachments from a page to local system.
confluence.download_attachments(page_id, download_path, path=None)
# Remove completely a file if version is None or delete version
confluence.delete_attachment(page_id, filename, version=None)
Expand Down
21 changes: 21 additions & 0 deletions examples/confluence/confluence_download_attachments_from_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from atlassian import Confluence
import os

host = "<cloud_instance_url/wiki>"
username = "<user_email>"
password = "<API_TOKEN>"
confluence = Confluence(
url=host,
username=username,
password=password,
)

# this is the directory where the attachments will be saved.
# In this example we use current working directory where script is executed + subdirectory 'attachment_tests'

current_dir = os.getcwd()
my_path = current_dir + "/attachment_tests"
page = 393464 # make sure the page id exists and has attachments

confluence.download_attachments_from_page(page)
# Directory 'attachment_tests' should include saved attachment. If directory deosn't exist or if there is permission issue function should raise an error.
Empty file added test.file.py
Empty file.

0 comments on commit 2a8a77f

Please sign in to comment.