n8n-nodes-kodi
Version:
A powerful n8n community node for controlling Kodi media center through JSON-RPC API with intelligent method discovery and comprehensive media management capabilities
153 lines • 7.34 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KodiService = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
class KodiService {
constructor(credentials) {
this.availableMethods = [];
this.methodsLoaded = false;
this.credentials = credentials;
this.baseUrl = this.buildUrl();
}
buildUrl() {
const { host, port, username, password } = this.credentials;
if (username.trim().length > 0 && password.trim().length > 0) {
return `http://${username}:${password}@${host}:${port}/jsonrpc`;
}
return `http://${host}:${port}/jsonrpc`;
}
async discoverMethods() {
if (this.methodsLoaded) {
return this.availableMethods;
}
if (this.credentials.enableDiscovery === false) {
this.availableMethods = this.getCommonMethods();
this.methodsLoaded = true;
return this.availableMethods;
}
try {
const methods = await this.getAvailableMethods();
if (methods.length > 0) {
this.availableMethods = methods;
this.methodsLoaded = true;
return methods;
}
this.availableMethods = this.getCommonMethods();
this.methodsLoaded = true;
return this.availableMethods;
}
catch (error) {
this.availableMethods = this.getCommonMethods();
this.methodsLoaded = true;
return this.availableMethods;
}
}
async getAvailableMethods() {
try {
const response = await this.makeRequest({
jsonrpc: '2.0',
method: 'JSONRPC.Introspect',
id: 'discovery'
});
if (response.result && response.result.methods) {
return Object.keys(response.result.methods).map(methodName => ({
name: methodName,
description: response.result.methods[methodName].description || '',
params: response.result.methods[methodName].params || [],
returns: response.result.methods[methodName].returns || {}
}));
}
}
catch (error) {
}
return [];
}
getCommonMethods() {
return [
{ name: 'VideoLibrary.Scan', description: 'Scan video library for new content' },
{ name: 'VideoLibrary.Clean', description: 'Clean video library' },
{ name: 'VideoLibrary.GetMovies', description: 'Get all movies' },
{ name: 'VideoLibrary.GetTVShows', description: 'Get all TV shows' },
{ name: 'VideoLibrary.GetEpisodes', description: 'Get episodes for a TV show' },
{ name: 'VideoLibrary.GetMusicVideos', description: 'Get all music videos' },
{ name: 'VideoLibrary.GetMovieDetails', description: 'Get details for a specific movie' },
{ name: 'VideoLibrary.GetTVShowDetails', description: 'Get details for a specific TV show' },
{ name: 'AudioLibrary.Scan', description: 'Scan audio library for new content' },
{ name: 'AudioLibrary.Clean', description: 'Clean audio library' },
{ name: 'AudioLibrary.GetAlbums', description: 'Get all albums' },
{ name: 'AudioLibrary.GetArtists', description: 'Get all artists' },
{ name: 'AudioLibrary.GetSongs', description: 'Get all songs' },
{ name: 'AudioLibrary.GetAlbumDetails', description: 'Get details for a specific album' },
{ name: 'AudioLibrary.GetArtistDetails', description: 'Get details for a specific artist' },
{ name: 'Player.GetActivePlayers', description: 'Get currently active players' },
{ name: 'Player.GetProperties', description: 'Get player properties' },
{ name: 'Player.PlayPause', description: 'Play or pause current media' },
{ name: 'Player.Stop', description: 'Stop current media' },
{ name: 'Player.Seek', description: 'Seek to position in current media' },
{ name: 'Player.SetSpeed', description: 'Set playback speed' },
{ name: 'Playlist.GetItems', description: 'Get items in current playlist' },
{ name: 'Playlist.Add', description: 'Add items to playlist' },
{ name: 'Playlist.Clear', description: 'Clear current playlist' },
{ name: 'Playlist.Remove', description: 'Remove item from playlist' },
{ name: 'System.GetProperties', description: 'Get system properties' },
{ name: 'System.Hibernate', description: 'Hibernate system' },
{ name: 'System.Reboot', description: 'Reboot system' },
{ name: 'System.Shutdown', description: 'Shutdown system' },
{ name: 'System.Suspend', description: 'Suspend system' },
{ name: 'Application.GetProperties', description: 'Get application properties' },
{ name: 'Application.SetVolume', description: 'Set application volume' },
{ name: 'Application.Quit', description: 'Quit application' },
{ name: 'Files.GetDirectory', description: 'Get directory contents' },
{ name: 'Files.GetFileDetails', description: 'Get file details' },
{ name: 'Files.GetSources', description: 'Get media sources' },
{ name: 'Addons.GetAddons', description: 'Get installed addons' },
{ name: 'Addons.GetAddonDetails', description: 'Get addon details' },
{ name: 'Addons.ExecuteAddon', description: 'Execute an addon' },
{ name: 'GUI.ShowNotification', description: 'Show notification' },
{ name: 'GUI.ActivateWindow', description: 'Activate a window' },
{ name: 'GUI.GetProperties', description: 'Get GUI properties' }
];
}
async makeRequest(request) {
const response = await (0, node_fetch_1.default)(this.baseUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(request)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
async executeMethod(method, params) {
const request = {
jsonrpc: '2.0',
method,
params,
id: `n8n-${Date.now()}`
};
const response = await this.makeRequest(request);
if (response.error) {
throw new Error(`Kodi error: ${response.error.message} (code: ${response.error.code})`);
}
return response.result;
}
getMethodsByCategory() {
const categories = {};
for (const method of this.availableMethods) {
const category = method.name.split('.')[0];
if (!categories[category]) {
categories[category] = [];
}
categories[category].push(method);
}
return categories;
}
}
exports.KodiService = KodiService;
//# sourceMappingURL=KodiService.js.map