Skip to content

Instantly share code, notes, and snippets.

@wcoder
Forked from wildcard/ya.py
Last active May 14, 2024 04:31
Show Gist options
  • Save wcoder/bb62eac43a63bcc49847665f59697306 to your computer and use it in GitHub Desktop.
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
#!/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