-
-
Save wcoder/bb62eac43a63bcc49847665f59697306 to your computer and use it in GitHub Desktop.
Download file from Yandex.Disk through share link for large file. Pure Node.js script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// How to | |
// wget http://gist.github.com/... | |
// chmod +x ya.js | |
// ./ya.js download_url path/to/directory | |
const https = require('https'); | |
const { URL } = require('url'); | |
const { spawn } = require('child_process'); | |
const base_url = 'https://cloud-api.yandex.net:443/v1/disk/public/resources/download?public_key='; | |
const apiUrl = process.argv[2]; | |
const folder = process.argv[3] || '.'; | |
const encodedUrl = encodeURIComponent(apiUrl); | |
const apiUrlWithQuery = base_url + encodedUrl; | |
https.get(apiUrlWithQuery, (res) => { | |
let data = ''; | |
res.on('data', (chunk) => { | |
data += chunk; | |
}); | |
res.on('end', () => { | |
const json_res = JSON.parse(data); | |
const filename = new URL(json_res.href).searchParams.get('filename'); | |
const command = 'wget'; | |
const args = [ | |
'-c', | |
'--retry-connrefused', | |
'--tries=0', | |
'--timeout=5', | |
json_res.href, | |
'-O', | |
`${folder}${filename}`, | |
]; | |
const childProcess = spawn(command, args); | |
childProcess.stdout.on('data', (data) => { | |
console.log(data.toString()); | |
}); | |
childProcess.stderr.on('data', (data) => { | |
console.error(data.toString()); | |
}); | |
childProcess.on('error', (err) => { | |
console.error('Error:', err.message); | |
}); | |
childProcess.on('exit', (code) => { | |
if (code === 0) { | |
console.log('File downloaded successfully!'); | |
} else { | |
console.error(`Command execution failed with code ${code}`); | |
} | |
}); | |
}); | |
}).on('error', (err) => { | |
console.error('Error:', err.message); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment