Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Confluence] new method: download_attachments_from_page #1282

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions atlassian/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,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
20 changes: 20 additions & 0 deletions examples/confluence/confluence_download_attachments_from_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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.
Loading