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

[Function request] A way to download tarfiles. #238

Open
ggavelis opened this issue Mar 1, 2023 · 0 comments
Open

[Function request] A way to download tarfiles. #238

ggavelis opened this issue Mar 1, 2023 · 0 comments

Comments

@ggavelis
Copy link

ggavelis commented Mar 1, 2023

Hello and thanks for your immensely useful library!

I've found .get_file_contents() to be especially useful. Do you have any plans to make a way to download non-string type data. (i.e. contents for tarfiles?)

I'm a novice, but have tried tweaking .get_file_contents() to work on tarfiles.
The only difference with my code (at end of this messsage) is that

  • it makes a request (r) and returns r.raw

whereas your code, .get_file_contents():

  • returns r.text

I find that my code successfully writes .tar.gz archives most of the time, but is also prone to silent failure (e.g. occasionally writes archives that are corrupted)--for reasons that I do not understand. If you have any advice on how to download tar.gz files from Clarity, I would greatly appreciate it!

~Greg

The code:

from urllib.parse import urljoin
import tarfile

PATH_tgz = "example.tar.gz"
DIR_download = "./directory/"

make a request

r = API.request_session.get(URL, auth=(API.username, API.password), timeout=15, stream=True)
API.validate_response(r)

Write tgz and extract it

with open(PATH_tgz, 'wb') as f: f.write(r.raw.read())
FILE_tgz = tarfile.open(PATH_tgz)
FILE_tgz.extractall(DIR_download)
FILE_tgz.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant