Skip to content

Instantly share code, notes, and snippets.

@wildcard
Forked from Yegorov/ya.py
Last active April 12, 2024 13:23
Show Gist options
  • Save wildcard/8c1cb5ac0bb5713a17ef1e4dd6e4780b to your computer and use it in GitHub Desktop.
Save wildcard/8c1cb5ac0bb5713a17ef1e4dd6e4780b to your computer and use it in GitHub Desktop.
Download file from Yandex.Disk through share link for large file + reconnect option on MacOS with caffeinate to prevent computer from sleep
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# https://toster.ru/q/72866
# How to
# wget http://gist.github.com/...
# chmod +x ya.py
# ./ya.py download_url path/to/directory
import os, sys, json
import urllib.parse as ul
sys.argv.append('.') if len(sys.argv) == 2 else None
base_url = 'https://cloud-api.yandex.net:443/v1/disk/public/resources/download?public_key='
url = ul.quote_plus(sys.argv[1])
folder = sys.argv[2]
res = os.popen('wget -qO - {}{}'.format(base_url, url)).read()
json_res = json.loads(res)
filename = ul.parse_qs(ul.urlparse(json_res['href']).query)['filename'][0]
os.system("caffeinate -s wget -c --retry-connrefused --tries=0 --timeout=5 '{}' -P '{}' -O '{}'".format(json_res['href'], folder, filename))
# os.system("wget '{}'".format(json_res['href']))
@DataGreed
Copy link

At first I wanted to comment that it does not work for me, but, surprisingly, after the fifth attempt it managed to connect and actually started downloading. Thank you!

@wcoder
Copy link

wcoder commented Aug 1, 2023

Thanks for the script.
Link Node.js fork: https://gist.github.com/wcoder/bb62eac43a63bcc49847665f59697306

@Glemhel
Copy link

Glemhel commented Feb 19, 2024

It worked perfectly, thank you!
In case someone is trying to get this work for Ubuntu, you can remove caffeinate -s from line 21.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment