0

I was working on the python confluence API for downloading attachment from confluence page, I need to download only files with .mpp extension. Tried with glob and direct parameters but didnt work.

Here is my code:

file_name = glob.glob("*.mpp")
attachments_container = confluence.get_attachments_from_content(page_id=33110, start=0, limit=1,filename=file_name)
print(attachments_container)
attachments = attachments_container['results']
for attachment in attachments:
    fname = attachment['title']
    download_link = confluence.url + attachment['_links']['download']
    r = requests.get(download_link, auth = HTTPBasicAuth(confluence.username,confluence.password))
    if r.status_code == 200:
        if not os.path.exists('phoenix'):
            os.makedirs('phoenix')
        fname = ".\\phoenix\\" +fname
martineau
  • 119,623
  • 25
  • 170
  • 301
Naveen
  • 356
  • 2
  • 10

1 Answers1

0

glob.glob() operates on your local folder. So you can't use that as a filter for get_attachments_from_content(). Also, don't specify a limit of since that gets you just one/the first attachment. Specify a high limit or whatever default will include all of them. (You may have to paginate results.)

However, you can exclude the files you don't want by checking the title of each attachment before you download it, which you have as fname = attachment['title'].

attachments_container = confluence.get_attachments_from_content(page_id=33110, limit=1000)
attachments = attachments_container['results']
for attachment in attachments:
    fname = attachment['title']
    if not fname.lower().endswith('.mpp'):
        # skip file if it's not got that extension
        continue
    download_link = ...
    # rest of your code here

Also, your code looks like a copy-paste from this answer but you've changed the actual "downloading" part of it. So if your next StackOverflow question is going to be "how to download a file from confluence", use that answer's code.

aneroid
  • 12,983
  • 3
  • 36
  • 66
  • Thanks for the reply , I am using limit attribute coz i only want the latest (last updated) attachment from the confluence page, does it work that way? – Naveen Jan 23 '21 at 14:34
  • I don't think that's how the `limit` parameter works. `limit` _usually_ means the number of results you want. So `start=0` and `limit=10` will give you 10 items (0-9). You'll need to verify that from the docs. Also, if you want to check if you've already downloaded the attachment, check with [`os.path.exists(".\\phoenix\\" +fname)`](https://docs.python.org/3/library/os.path.html#os.path.exists) and don't download if it does exist. If it's only the last attachment uploaded, what happens if two '.mpp' files are uploaded after your script last runs? You'll end up missing one of them. – aneroid Jan 23 '21 at 16:42
  • Yes Sir , you are correct , i think the confluence sorting the attachment list based on the updated time , may be thats the limit =1 will fetch the last updated one. i am planning for polling using this python script , and only having one MPP file. – Naveen Jan 24 '21 at 15:43