UNPKG

andrade-soulseek-downloader

Version:

Simple, safe Soulseek download library with built-in rate limiting to prevent bans

131 lines 4.62 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.CLIHandler = void 0; const dotenv = __importStar(require("dotenv")); const core_1 = require("../../core"); const utils_1 = require("../../utils"); dotenv.config(); /** * CLI handler for Soulseek downloader. * Manages command-line interface interactions. */ class CLIHandler { logger; constructor() { this.logger = new utils_1.Logger(); } /** * Parses command-line arguments. * @returns Parsed artist and title or null if invalid */ parseArguments() { const args = process.argv.slice(2); if (args.length < 2) { this.logger.error('Usage: pnpm start <artist> <title>'); this.logger.info('Example: pnpm start "The Beatles" "Hey Jude"'); return null; } return { artist: args[0], title: args[1] }; } /** * Creates download configuration from environment variables. * @returns Download configuration */ createConfig() { return { maxAttempts: parseInt(process.env.SOULSEEK_MAX_ATTEMPTS || '10'), searchTimeout: parseInt(process.env.SOULSEEK_SEARCH_TIMEOUT || '30000'), downloadTimeout: parseInt(process.env.SOULSEEK_DOWNLOAD_TIMEOUT || '120000'), preferSlotsAvailable: process.env.SOULSEEK_PREFER_SLOTS !== 'false', minSpeed: parseInt(process.env.SOULSEEK_MIN_SPEED || '0'), searchDelay: parseInt(process.env.SOULSEEK_SEARCH_DELAY || '5000'), downloadDelay: parseInt(process.env.SOULSEEK_DOWNLOAD_DELAY || '3000'), maxConcurrent: parseInt(process.env.SOULSEEK_MAX_CONCURRENT || '1'), cooldownAfterError: parseInt(process.env.SOULSEEK_ERROR_COOLDOWN || '10000') }; } /** * Displays welcome message and download info. */ displayHeader(artist, title) { this.logger.title(`🎵 Soulseek Downloader`); this.logger.info(`Artist: ${artist}`); this.logger.info(`Title: ${title}`); this.logger.divider(); } /** * Displays download result. */ displayResult(result) { this.logger.divider(); if (result) { this.logger.success(`✨ Download complete!`); this.logger.info(`📁 Saved to: ${result}`); } else { this.logger.error('❌ Failed to download the track'); } } /** * Main CLI execution flow. */ async run() { const args = this.parseArguments(); if (!args) { process.exit(1); } const { artist, title } = args; const config = this.createConfig(); this.displayHeader(artist, title); const downloader = new core_1.SoulseekDownloader(config); try { const result = await downloader.searchAndDownload(artist, title); this.displayResult(result); } catch (error) { this.logger.error(`Fatal error: ${error}`); } finally { await downloader.disconnect(); process.exit(0); } } } exports.CLIHandler = CLIHandler; //# sourceMappingURL=cli-handler.js.map