Skip to content

Instantly share code, notes, and snippets.

@pansapiens
Last active March 2, 2021 22:52
Show Gist options
  • Save pansapiens/ee9440873686de5723787931e69d753e to your computer and use it in GitHub Desktop.
Save pansapiens/ee9440873686de5723787931e69d753e to your computer and use it in GitHub Desktop.
Download a password protected file from CloudStor / ownCloud / NextCloud on the commandline
#!/usr/bin/env python3
# https://colab.research.google.com/drive/1CGOygPAmjTxWnmsywv178dh3u36ZRBgv
# You'll need to install the `robobrowser` Python package.
# Do it in a virtualenv like:
#
# python3 -m venv ~/.virtualenvs/cloudstor
# source ~/.virtualenvs/cloudstor/activate
# pip3 install robobrowser
# python3 grab_from_cloudstor_pw_protect.py
import werkzeug
werkzeug.cached_property = werkzeug.utils.cached_property
import re
from robobrowser import RoboBrowser
url = 'https://cloudstor.aarnet.edu.au/plus/s/JYLaiEf6Osv7YDQ/download'
password = 'xxx'
output_filename = 'file.tar.gz'
chunk_size = 512
browser = RoboBrowser(history=True)
browser.open(url)
form = browser.get_form()
form['password'].value = password
browser.submit_form(form)
with open(output_filename, 'wb') as f:
# TODO: Catch OSError (ECONNRESET) and other
# timeout / connectivity related exceptions in here, restart / retry as required.
# (ideally continue with an HTTP Range request)
with browser.session.request('GET', url, stream=True) as r:
for chunk in r.iter_content(chunk_size=chunk_size):
f.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment