/** * @fileoverview Example and test file for the TeraboxScraper library. * This file demonstrates the full capabilities of the library, including * complex initialization, file info retrieval, and robust download with progress. * * NOTE: For this test to work, you MUST provide a valid Terabox share link * and a valid 'ndus' cookie from a logged-in Terabox session. * The test will attempt to download a small file to the 'downloads' directory. */ import { TeraboxScraper, DownloadProgress, Logger, bytesToHuman } from '../index'; import * as path from 'path'; // --- Configuration --- // !!! REPLACE WITH YOUR ACTUAL COOKIE AND A VALID TERABOX SHARE LINK !!! const YOUR_TERABOX_COOKIE = "lang=en; ndus=YOUR_NDUS_COOKIE_HERE;"; const TEST_SHARE_LINK = "https://www.terabox.app/s/YOUR_SHARE_LINK_HERE"; // e.g., a link to a small, public file // --- Logger Setup (Optional, but recommended for better output) --- const logger = Logger.getInstance({ level: 'info', prettyPrint: true }).getLogger(); /** * Custom progress bar function for a powerful and attractive terminal display. * @param progress The download progress data. */ function customProgressBar(progress: DownloadProgress): void { const totalSizeHuman = bytesToHuman(progress.totalSize); const downloadedHuman = bytesToHuman(progress.downloaded); const speedHuman = bytesToHuman(progress.speedBps) + '/s'; const eta = progress.etaSeconds === Infinity ? 'N/A' : `${progress.etaSeconds.toFixed(0)}s`; const barLength = 40; const filledLength = Math.round(barLength * progress.percentage / 100); const emptyLength = barLength - filledLength; const bar = '█'.repeat(filledLength) + '░'.repeat(emptyLength); process.stdout.write( `\r[${bar}] ${progress.percentage.toFixed(2)}% | ${downloadedHuman} / ${totalSizeHuman} | Speed: ${speedHuman} | ETA: ${eta}` ); } /** * Main function to run the test. */ async function runTest() { logger.info('--- Starting TeraboxScraper Complex Test ---'); if (YOUR_TERABOX_COOKIE.includes('YOUR_NDUS_COOKIE_HERE') || TEST_SHARE_LINK.includes('YOUR_SHARE_LINK_HERE')) { logger.fatal('Please update YOUR_TERABOX_COOKIE and TEST_SHARE_LINK in src/test/test.ts with real values to run a functional test.'); return; } try { // 1. Initialize the Scraper with complex configuration const scraper = new TeraboxScraper({ cookie: YOUR_TERABOX_COOKIE, timeout: 60000, // 60 seconds timeout // Optional: proxy: 'http://user:pass@host:port', }); // 2. Fetch File Information logger.info(`Attempting to fetch info for link: ${TEST_SHARE_LINK}`); const infoResponse = await scraper.getFileInfo(TEST_SHARE_LINK); if (!infoResponse.success || !infoResponse.data) { logger.error({ error: infoResponse.error }, 'Failed to get file information.'); return; } const fileInfo = infoResponse.data; logger.info({ fileName: fileInfo.fileName, size: fileInfo.fileSizeHuman, downloadLink: fileInfo.downloadLink.substring(0, 50) + '...', // Truncate link for log }, 'File Info Retrieved Successfully.'); // 3. Download the File const downloadDir = path.join(process.cwd(), 'downloads'); logger.info({ downloadDir }, `Starting download to directory: ${downloadDir}`); const downloadResponse = await scraper.downloadFile( fileInfo, downloadDir, // Save to the 'downloads' directory customProgressBar // Use the custom, powerful progress bar ); // Clear the progress bar line process.stdout.write('\r' + ' '.repeat(100) + '\r'); if (!downloadResponse.success || !downloadResponse.data) { logger.error({ error: downloadResponse.error }, 'File download failed.'); return; } const result = downloadResponse.data; logger.info({ filePath: result.filePath, duration: `${(result.durationMs / 1000).toFixed(2)} seconds`, }, '--- Download Complete ---'); } catch (e: any) { logger.fatal({ error: e.message || e }, 'An unhandled critical error occurred during the test.'); } finally { logger.info('--- TeraboxScraper Complex Test Finished ---'); } } runTest();