From a8ce2efacb1e2f46304cf3645b3168d7e64dd8a3 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Sat, 20 Nov 2021 16:53:29 -0800 Subject: [PATCH] Add search for jellyfin --- src/api/controller.ts | 3 ++- src/api/jellyfinApi.ts | 30 ++++++++++++++++++++++++++++ src/components/search/SearchView.tsx | 6 +----- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/api/controller.ts b/src/api/controller.ts index 16acc63..090b348 100644 --- a/src/api/controller.ts +++ b/src/api/controller.ts @@ -49,6 +49,7 @@ import { batchUnstar as jfBatchUnstar, getGenres as jfGetGenres, getMusicFolders as jfGetMusicFolders, + getSearch as jfGetSearch, } from './jellyfinApi'; import { APIEndpoints, ServerType } from '../types'; @@ -78,7 +79,7 @@ const endpoints = [ { id: 'updatePlaylist', endpoint: { subsonic: updatePlaylist, jellyfin: undefined } }, { id: 'clearPlaylist', endpoint: { subsonic: clearPlaylist, jellyfin: undefined } }, { id: 'getGenres', endpoint: { subsonic: getGenres, jellyfin: jfGetGenres } }, - { id: 'getSearch', endpoint: { subsonic: getSearch, jellyfin: undefined } }, + { id: 'getSearch', endpoint: { subsonic: getSearch, jellyfin: jfGetSearch } }, { id: 'scrobble', endpoint: { subsonic: scrobble, jellyfin: undefined } }, { id: 'getIndexes', endpoint: { subsonic: getIndexes, jellyfin: undefined } }, { id: 'getMusicFolders', endpoint: { subsonic: getMusicFolders, jellyfin: jfGetMusicFolders } }, diff --git a/src/api/jellyfinApi.ts b/src/api/jellyfinApi.ts index f1a435a..5cd9436 100644 --- a/src/api/jellyfinApi.ts +++ b/src/api/jellyfinApi.ts @@ -455,3 +455,33 @@ export const getMusicFolders = async () => { const { data } = await jellyfinApi.get(`/users/${auth.username}/items`); return (data.Items || []).map((entry: any) => normalizeFolder(entry)); }; + +export const getSearch = async (options: { query: string; musicFolderId?: string | number }) => { + const { data } = await jellyfinApi.get(`/users/${auth.username}/items`, { + params: { + fields: 'Genres, DateCreated, MediaSources, ChildCount, UserData', + includeArtists: false, + includeGenres: false, + includeItemTypes: 'Audio, MusicArtist, MusicAlbum', + includeMedia: false, + includeStudios: false, + limit: 50, + parentId: options.musicFolderId, + recursive: true, + searchTerm: options.query, + }, + }); + + const { data: artistData } = await jellyfinApi.get(`/artists`, { + params: { limit: 10, parentId: options.musicFolderId, searchTerm: options.query }, + }); + + const albumItems = data.Items.filter((entry: any) => entry.Type === 'MusicAlbum'); + const songItems = data.Items.filter((entry: any) => entry.Type === 'Audio'); + + return { + artist: (artistData.Items || []).map((entry: any) => normalizeArtist(entry)), + album: (albumItems || []).map((entry: any) => normalizeAlbum(entry)), + song: (songItems || []).map((entry: any) => normalizeSong(entry)), + }; +}; diff --git a/src/components/search/SearchView.tsx b/src/components/search/SearchView.tsx index 3b3f5f0..64b79ef 100644 --- a/src/components/search/SearchView.tsx +++ b/src/components/search/SearchView.tsx @@ -20,7 +20,6 @@ import { setStatus } from '../../redux/playerSlice'; import ListViewTable from '../viewtypes/ListViewTable'; import { SectionTitle, SectionTitleWrapper, StyledPanel } from '../shared/styled'; import { apiController } from '../../api/controller'; -import { Server } from '../../types'; const SearchView = () => { const dispatch = useAppDispatch(); @@ -44,10 +43,7 @@ const SearchView = () => { apiController({ serverType: config.serverType, endpoint: 'getSearch', - args: - config.serverType === Server.Subsonic - ? { query: urlQuery, songCount: 100, musicFolderId: musicFolder } - : null, + args: { query: urlQuery, songCount: 100, musicFolderId: musicFolder }, }) );