From 7d7d0ead13ef6014158292005a2a5403580bb7a2 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Wed, 9 Mar 2022 19:18:16 -0800 Subject: [PATCH] Add getSongsByGenre for jellyfin API --- src/api/controller.ts | 3 ++- src/api/jellyfinApi.ts | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/api/controller.ts b/src/api/controller.ts index c664a33..e6eb0f5 100644 --- a/src/api/controller.ts +++ b/src/api/controller.ts @@ -65,6 +65,7 @@ import { updatePlaylist as jfUpdatePlaylist, getSongs as jfGetSongs, getSimilarSongs as jfGetSimilarSongs, + getSongsByGenre as jfGetSongsByGenre, } from './jellyfinApi'; import { APIEndpoints, ServerType } from '../types'; @@ -97,7 +98,7 @@ const endpoints = [ { id: 'getDownloadUrl', endpoint: { subsonic: getDownloadUrl, jellyfin: jfGetDownloadUrl } }, { id: 'getSongs', endpoint: { subsonic: undefined, jellyfin: jfGetSongs } }, { id: 'getTopSongs', endpoint: { subsonic: getTopSongs, jellyfin: undefined } }, - { id: 'getSongsByGenre', endpoint: { subsonic: getSongsByGenre, jellyfin: jfGetSongs } }, + { id: 'getSongsByGenre', endpoint: { subsonic: getSongsByGenre, jellyfin: jfGetSongsByGenre } }, { id: 'getLyrics', endpoint: { subsonic: getLyrics, jellyfin: undefined } }, // Playlist handling logic is split up by server type due to differences in how each server handles them. diff --git a/src/api/jellyfinApi.ts b/src/api/jellyfinApi.ts index 82a13d9..7b4568b 100644 --- a/src/api/jellyfinApi.ts +++ b/src/api/jellyfinApi.ts @@ -688,6 +688,51 @@ export const getSimilarSongs = async (options: { return (data.Items || []).map((entry: any) => normalizeSong(entry)); }; +export const getSongsByGenre = async (options: { + genre: string; + size: number; + offset: number; + musicFolderId?: string | number; + recursive?: boolean; +}) => { + if (options.recursive) { + const { data } = await jellyfinApi.get(`/users/${auth.username}/items`, { + params: { + fields: 'Genres, DateCreated, MediaSources, UserData, ParentId', + genres: options.genre, + recursive: true, + includeItemTypes: 'Audio', + StartIndex: 0, + }, + }); + + const entries = (data.Items || []).map((entry: any) => normalizeSong(entry)); + + return normalizeAPIResult( + _.orderBy(entries || [], ['album', 'track'], ['asc', 'asc']), + data.TotalRecordCount + ); + } + + const { data } = await jellyfinApi.get(`/users/${auth.username}/items`, { + params: { + fields: 'Genres, DateCreated, MediaSources, UserData, ParentId', + genres: options.genre, + recursive: true, + includeItemTypes: 'Audio', + limit: options.size || 100, + startIndex: options.offset, + }, + }); + + const entries = (data.Items || []).map((entry: any) => normalizeSong(entry)); + + return normalizeAPIResult( + _.orderBy(entries || [], ['album', 'track'], ['asc', 'asc']), + data.TotalRecordCount + ); +}; + export const getGenres = async (options: { musicFolderId?: string }) => { const { data } = await jellyfinApi.get(`/musicgenres`, { params: { parentId: options.musicFolderId },