Skip to content

Commit

Permalink
download files via aria2
Browse files Browse the repository at this point in the history
  • Loading branch information
seiya-git committed Sep 13, 2024
1 parent 2c3b348 commit 6bbd7f3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 40 deletions.
5 changes: 4 additions & 1 deletion .config.yaml.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
accounts:
main: ...
main: ...
aria2:
url: http://localhost:6800/jsonrpc
secret: YOUR_ARIA2_RPC_SECRET
23 changes: 8 additions & 15 deletions app-filemeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ async function getMeta(){
const remotePathData = await app.getRemoteDir(remotePath);

if(remotePathData.errno == 0){
console.log(':: Selected Remote Path:', remotePath, '\n');
await showMeta(remotePath, remotePathData);
}
}
Expand All @@ -92,20 +93,12 @@ async function showMeta(rPath, pathData){
continue;
}

console.log(f);

const fdata = {
dlink: f.dlink,
root: f.path.split('/').slice(0, -1).join('/') || '/',
file: f.server_filename,
size: f.size,
sizef: filesize(f.size, {standard: 'iec', round: 3, pad: true}),
};

// reqh.headers.get('x-bs-meta-crc32')
// reqh.headers.get('content-md5')
// reqh.headers.get('x-bs-file-size')

// console.log(fdata);
console.log('FS ID:', f.fs_id);
console.log('Path :', f.path.split('/').slice(0, -1).join('/') || '/');
console.log('File :', f.server_filename);
console.log('Size :', f.size);
console.log('Size :', filesize(f.size, {standard: 'iec', round: 2}));
// console.log('DLink:', f.dlink);
console.log();
}
}
93 changes: 69 additions & 24 deletions app-getdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import { fileURLToPath } from 'url';
import { request } from 'undici';

import Argv from './modules/app-argv.js';
import TeraBoxApp from './modules/api.js';
Expand All @@ -21,6 +23,7 @@ const config = loadYaml(path.resolve(__dirname, './.config.yaml'));
const meta = loadYaml(path.resolve(__dirname, './package.json'));

console.log('[INFO] TeraBox App', 'v' + meta.version, '(GetDL Module)');
let sRoot = '';

const yargs = new Argv(config, ['a','l','r']);
if(yargs.getArgv('help')){
Expand All @@ -33,11 +36,10 @@ if(yargs.getArgv('help')){
await getDL();
}
catch(error){
console.log(error);
console.error(error);
}
})();


async function getDL(){
if(!config.accounts){
console.error('[ERROR] Accounts not set!');
Expand All @@ -64,43 +66,86 @@ async function getDL(){
console.log();

const remotePath = await selectRemotePath(yargs.getArgv('r'));
const fsList = await getRemotePaths(remotePath);

if(fsList.length > 0){
await addDownloads(fsList);
}

};

async function getRemotePaths(remotePath){
console.log(':: Requesting Remote:', remotePath);
const remotePathData = await app.getRemoteDir(remotePath);

if(remotePathData.errno == 0){
let getMeta;
if(remotePathData.list.length == 0){
getMeta = await app.getFileMeta([remotePath]);
sRoot = remotePath.split('/').slice(0, -1).join('/');
return [remotePath];
}
else{
if(sRoot == ''){
sRoot = remotePathData.list[0].path.split('/').slice(0, -1).join('/') || '/';
}
const fileList = [];
for(const f of remotePathData.list){
if(f.isdir == 0){
if(f.isdir == 1){
const subList = await getRemotePaths(f.path);
fileList.push(...subList);
}
else{
fileList.push(f.path);
}
}
getMeta = await app.getFileMeta(fileList);
}
if(getMeta.info.length > 0){
console.log('Aria2 WebUI ULRs:\n');

const rRoot = getMeta.info[0].path.split('/').slice(0, -1).join('/') || '/';
let folderName = '/';

if(rRoot != '/'){
folderName = rRoot.split('/').at(-1);
folderName += '/'
}

console.log('Folder:', folderName, '\n');

for(const f of getMeta.info){
console.log(`${f.dlink} -o "${f.server_filename}"`);
}
return fileList;
}
}
else{
console.log(':: Wrong remote path!');
return [];
}

console.log();
};
}

async function addDownloads(fsList){
const getMeta = await app.getFileMeta(fsList);

// aria2c -x 16 -s 10 -j 4 -k 1M --enable-rpc --rpc-allow-origin-all=true --dir=D:/Downloads --rpc-secret=YOUR_ARIA2_RPC_SECRET
// https://aria2.github.io/manual/en/html/aria2c.html#aria2.addUri

const jsonReq = {
jsonrpc: '2.0',
id: 'DOWNLOAD_ID',
method: 'aria2.addUri',
params: [ 'token:' + config.aria2.secret ],
};

const rpcReq = [];
for(const [i, f] of getMeta.info.entries()){
rpcReq.push(structuredClone(jsonReq));

const folderName = f.path.split('/').slice(0, -1).join('/')
.replace(new RegExp('^' + sRoot), '')
.replace(new RegExp('^/'), '');

rpcReq[i].id = crypto.randomUUID();
rpcReq[i].params.push([f.dlink]);
rpcReq[i].params.push({ out: (folderName?folderName+'/':'') + f.server_filename });
}

try{
const rpcUrl = new URL(config.aria2.url);
const req = await request(rpcUrl, {
method: 'POST',
body: JSON.stringify(rpcReq),
});
console.log('ADDING...');
console.log('CODE:', req.statusCode);
console.log(await req.body.json());
}
catch(error){
error = new Error('aria2.addUri', { cause: error });
console.error(error);
}
}
5 changes: 5 additions & 0 deletions modules/app-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import * as YargsInit from 'yargs';

class Args {
constructor(config, reqArgs = []){
if(typeof config.accounts !== 'object' || Array.isArray(config.accounts) && config.accounts === null){
config.accounts = {
empty: '',
};
}
this.accounts = Object.keys(config.accounts);
this.remote_dir = config.remote_dir;
this.yargs = YargsInit.default(process.argv);
Expand Down

0 comments on commit 6bbd7f3

Please sign in to comment.