UNPKG

andrade-soulseek-downloader

Version:

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

124 lines 4.49 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.APIHandler = void 0; const dotenv = __importStar(require("dotenv")); const core_1 = require("../../core"); dotenv.config(); /** * API handler for Soulseek downloader. * Manages singleton connection and provides simple API. */ class APIHandler { static instance = null; downloader = null; constructor() { } /** * Gets singleton instance of API handler. * @returns APIHandler instance */ static getInstance() { if (!APIHandler.instance) { APIHandler.instance = new APIHandler(); } return APIHandler.instance; } /** * Creates download configuration from environment. * @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: 1, // Always use 1 to prevent bans cooldownAfterError: parseInt(process.env.SOULSEEK_ERROR_COOLDOWN || '10000') }; } /** * Ensures downloader instance is initialized. */ ensureDownloader() { if (!this.downloader) { const config = this.createConfig(); this.downloader = new core_1.SoulseekDownloader(config); } } /** * Downloads a track from Soulseek network. * @param artist - Artist name * @param title - Track title * @param folderName - Optional subfolder name within SOULSEEK_DOWNLOAD_DIR * @param customFileName - Optional custom filename (without extension) * @returns Path to downloaded file or null if failed */ async download(artist, title, folderName, customFileName) { try { this.ensureDownloader(); const result = await this.downloader.searchAndDownload(artist, title, folderName, customFileName); return result; } catch (error) { console.error('Download error:', error); return null; } } /** * Disconnects from Soulseek and cleans up resources. */ async disconnect() { if (this.downloader) { await this.downloader.disconnect(); this.downloader = null; } } /** * Resets the singleton instance. * Useful for testing or forced reconnection. */ static reset() { if (APIHandler.instance?.downloader) { APIHandler.instance.downloader.disconnect(); } APIHandler.instance = null; } } exports.APIHandler = APIHandler; //# sourceMappingURL=api-handler.js.map