Akshay
Akshay

Reputation: 2773

Can I download a file from JSON request using only wget (without curl)?

I am trying to download the latest just binary file and I am able to do it with the following line of code in bash:

wget -q $(curl -s https://api.github.com/repos/casey/just/releases/latest | python -c 'import json,sys;print(json.load(sys.stdin)["assets"][-1]["browser_download_url"])')

This works, but I am using curl and wget together which I feel looks wrong. Is there a way to do this with just wget?

Upvotes: 1

Views: 2365

Answers (3)

knittl
knittl

Reputation: 265595

wget URL is the mostly the same as curl -O URL.

curl is mostly the same as wget -o- URL.

But since you are already using python, why not initiate the HTTP request directly from python?

Or instead of using python, use to extract the relevant bits from the JSON:

curl -O "$(curl https://api.github.com/repos/casey/just/releases/latest |
               jq -r '.assets|last|.browser_download_url')"

Upvotes: 1

Richard
Richard

Reputation: 2625

Yes, use the -O flag with a file name.

wget -O ./filename.json "https://hostname/path-to-json"

Upvotes: 0

Daweo
Daweo

Reputation: 36650

I suggest to do everything in python using standard library following way

import json, urllib.request
with urllib.request.urlopen('https://api.github.com/repos/casey/just/releases/latest') as response:
    url = json.load(response)["assets"][-1]["browser_download_url"]
fname = url.split('/')[-1]
urllib.request.urlretrieve(url, fname)

Explanation: I use urllib.request observe that urlopen does behave similiar to open but allows reading data via network, it can be used together with json.load same as with open. I extract desired URL, then create fname from last part of it and then I use urlretrieve to download file from url saving it under name fname inside current working directory.

Upvotes: 0

Related Questions