diff --git a/.config.yaml.example b/.config.yaml.example index 7306c32..c6686f8 100644 --- a/.config.yaml.example +++ b/.config.yaml.example @@ -1,2 +1,5 @@ accounts: - main: ... \ No newline at end of file + main: ... +aria2: + url: http://localhost:6800/jsonrpc + secret: YOUR_ARIA2_RPC_SECRET diff --git a/app-filemeta.js b/app-filemeta.js index ce8296a..372e58c 100644 --- a/app-filemeta.js +++ b/app-filemeta.js @@ -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); } } @@ -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(); } } diff --git a/app-getdl.js b/app-getdl.js index 56fd9f3..37f521e 100644 --- a/app-getdl.js +++ b/app-getdl.js @@ -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'; @@ -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')){ @@ -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!'); @@ -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); + } +} diff --git a/modules/app-argv.js b/modules/app-argv.js index 31842bd..9d0358c 100644 --- a/modules/app-argv.js +++ b/modules/app-argv.js @@ -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);