// Copyright (c) 2026 Gordon Bolton. MIT License. const axios = require('axios'); const { logToFile } = require('../utils/logger'); /** * Ombi API client for fetching requests and searching media. * Provides integration with Ombi request management system. */ class OmbiClient { constructor(url, apiKey) { this.url = url.replace(/\/$/, ''); // Remove trailing slash this.apiKey = apiKey; this.axios = axios.create({ headers: { 'ApiKey': this.apiKey }, timeout: 10000 }); } /** * Get all movie requests from Ombi * @returns {Promise} Array of movie request objects */ async getMovieRequests() { try { const response = await this.axios.get(`${this.url}/api/v1/Request/movie`); return response.data || []; } catch (error) { logToFile(`[OmbiClient] Movie requests error: ${error.message}`); return []; } } /** * Get all TV requests from Ombi * @returns {Promise} Array of TV request objects */ async getTvRequests() { try { const response = await this.axios.get(`${this.url}/api/v1/Request/tv`); return response.data || []; } catch (error) { logToFile(`[OmbiClient] TV requests error: ${error.message}`); return []; } } /** * Search for movies by TMDB ID * @param {string} tmdbId - TheMovieDB ID * @returns {Promise} Search result object or null if not found */ async searchMovieByTmdbId(tmdbId) { if (!tmdbId) return null; try { const response = await this.axios.get(`${this.url}/api/v1/Search/movie/${tmdbId}`); return response.data || null; } catch (error) { logToFile(`[OmbiClient] Movie search error for TMDB ID ${tmdbId}: ${error.message}`); return null; } } /** * Search for movies by IMDB ID * @param {string} imdbId - IMDB ID * @returns {Promise} Search result object or null if not found */ async searchMovieByImdbId(imdbId) { if (!imdbId) return null; try { const response = await this.axios.get(`${this.url}/api/v1/Search/movie/imdb/${imdbId}`); return response.data || null; } catch (error) { logToFile(`[OmbiClient] Movie search error for IMDB ID ${imdbId}: ${error.message}`); return null; } } /** * Search for TV shows by TVDB ID * @param {string} tvdbId - TheTVDB ID * @returns {Promise} Search result object or null if not found */ async searchTvByTvdbId(tvdbId) { if (!tvdbId) return null; try { const response = await this.axios.get(`${this.url}/api/v1/Search/tv/${tvdbId}`); return response.data || null; } catch (error) { logToFile(`[OmbiClient] TV search error for TVDB ID ${tvdbId}: ${error.message}`); return null; } } /** * Search for TV shows by TMDB ID * @param {string} tmdbId - TheMovieDB ID * @returns {Promise} Search result object or null if not found */ async searchTvByTmdbId(tmdbId) { if (!tmdbId) return null; try { const response = await this.axios.get(`${this.url}/api/v1/Search/tv/tmdb/${tmdbId}`); return response.data || null; } catch (error) { logToFile(`[OmbiClient] TV search error for TMDB ID ${tmdbId}: ${error.message}`); return null; } } /** * Test connection to Ombi API * @returns {Promise} True if connection is successful */ async testConnection() { try { const response = await this.axios.get(`${this.url}/api/v1/Request/movie`); return response.status === 200; } catch (error) { logToFile(`[OmbiClient] Connection test failed: ${error.message}`); return false; } } /** * Get all users from Ombi * @returns {Promise} Array of user objects */ async getUsers() { try { const response = await this.axios.get(`${this.url}/api/v1/Identity/Users`); return response.data || []; } catch (error) { logToFile(`[OmbiClient] Get users error: ${error.message}`); return []; } } } module.exports = OmbiClient;