Browse Source

Add jf album endpoints

- getAlbum
- getAlbums
master
jeffvli 3 years ago
committed by Jeff
parent
commit
1db37acdba
  1. 11
      src/api/controller.ts
  2. 92
      src/api/jellyfinApi.ts
  3. 16
      src/components/library/AlbumList.tsx

11
src/api/controller.ts

@ -33,7 +33,12 @@ import {
updatePlaylistSongs,
updatePlaylistSongsLg,
} from './api';
import { getPlaylist as jfGetPlaylist, getPlaylists as jfGetPlaylists } from './jellyfinApi';
import {
getAlbum as jfGetAlbum,
getAlbums as jfGetAlbums,
getPlaylist as jfGetPlaylist,
getPlaylists as jfGetPlaylists,
} from './jellyfinApi';
import { APIEndpoints, ServerType } from '../types';
// prettier-ignore
@ -41,8 +46,8 @@ const endpoints = [
{ id: 'getPlaylist', endpoint: { subsonic: getPlaylist, jellyfin: jfGetPlaylist } },
{ id: 'getPlaylists', endpoint: { subsonic: getPlaylists, jellyfin: jfGetPlaylists } },
{ id: 'getStarred', endpoint: { subsonic: getStarred, jellyfin: undefined } },
{ id: 'getAlbum', endpoint: { subsonic: getAlbum, jellyfin: undefined } },
{ id: 'getAlbums', endpoint: { subsonic: getAlbums, jellyfin: undefined } },
{ id: 'getAlbum', endpoint: { subsonic: getAlbum, jellyfin: jfGetAlbum } },
{ id: 'getAlbums', endpoint: { subsonic: getAlbums, jellyfin: jfGetAlbums } },
{ id: 'getRandomSongs', endpoint: { subsonic: getRandomSongs, jellyfin: undefined } },
{ id: 'getArtist', endpoint: { subsonic: getArtist, jellyfin: undefined } },
{ id: 'getArtists', endpoint: { subsonic: getArtists, jellyfin: undefined } },

92
src/api/jellyfinApi.ts

@ -24,11 +24,7 @@ export const jellyfinApi = axios.create({
jellyfinApi.interceptors.request.use(
(config) => {
const { token } = auth;
config.headers.common['X-MediaBrowser-Token'] = token;
// config.headers[
// 'X-Emby-Authorization'
// ] = `MediaBrowser Client="Sonixd", Device="PC", DeviceId="${deviceId}", Version="N/a"`;
return config;
},
@ -116,6 +112,28 @@ const normalizeSong = (item: any) => {
};
};
const normalizeAlbum = (item: any) => {
return {
id: item.Id,
title: item.Name,
albumId: item.Id,
artist: item.ArtistItems && item.ArtistItems.map((entry: any) => normalizeItem(entry)),
albumArtist: item.AlbumArtists && item.AlbumArtists[0]?.Name,
albumArtistId: item.AlbumArtists && item.AlbumArtists[0]?.Id,
songCount: item.ChildCount,
duration: item.RunTimeTicks / 10000000,
created: item.DateCreated,
year: item.ProductionYear,
genre: item.GenreItems && item.GenreItems.map((entry: any) => normalizeItem(entry)),
image: getCoverArtUrl(item, 350),
isDir: false,
starred: item.UserData.IsFavorite ? 'true' : undefined,
type: Item.Album,
uniqueId: nanoid(),
song: (item.song || []).map((entry: any) => normalizeSong(entry)),
};
};
const normalizePlaylist = (item: any) => {
return {
id: item.Id,
@ -165,3 +183,69 @@ export const getPlaylists = async () => {
normalizePlaylist(entry)
);
};
export const getAlbum = async (options: { id: string }) => {
const { data } = await jellyfinApi.get(`/users/${auth.username}/items/${options.id}`, {
params: {
fields: 'Genres, DateCreated, ChildCount',
},
});
const { data: songData } = await jellyfinApi.get(`/users/${auth.username}/items`, {
params: {
parentId: options.id,
sortBy: 'SortName',
fields: 'Genres',
},
});
return normalizeAlbum({ ...data, song: songData.Items });
};
export const getAlbums = async (options: {
type: any;
size: number;
offset: number;
recursive: boolean;
}) => {
const sortTypes = [
{ original: 'alphabeticalByName', replacement: 'SortName', sortOrder: 'Ascending' },
{ original: 'alphabeticalByArtist', replacement: 'Artist', sortOrder: 'Ascending' },
{ original: 'frequent', replacement: 'PlayCount', sortOrder: 'Ascending' },
{ original: 'random', replacement: 'Random', sortOrder: 'Ascending' },
{ original: 'newest', replacement: 'DateCreated', sortOrder: 'Descending' },
{ original: 'recent', replacement: 'DatePlayed', sortOrder: 'Descending' },
];
const sortType = sortTypes.find((type) => type.original === options.type);
if (options.recursive) {
const { data } = await jellyfinApi.get(`/users/${auth.username}/items`, {
params: {
sortBy: sortType!.replacement,
sortOrder: sortType!.sortOrder,
includeItemTypes: 'MusicAlbum',
fields: 'Genres, DateCreated, ChildCount',
recursive: true,
},
});
return (data.Items || []).map((entry: any) => normalizeAlbum(entry));
}
const { data } = await jellyfinApi.get(`/users/${auth.username}/items`, {
params: {
sortBy: sortType!.replacement,
sortOrder: sortType!.sortOrder,
includeItemTypes: 'MusicAlbum',
fields: 'Genres, DateCreated, ChildCount',
recursive: true,
limit: options.size,
offset: options.offset,
},
});
return (data.Items || []).map((entry: any) => normalizeAlbum(entry));
};
// http://192.168.14.11:8096/Users/0e5716f27d7f4b48aadb4a3bd55a38e9/Items/70384e0059a925138783c7275f717859

16
src/components/library/AlbumList.tsx

@ -68,7 +68,12 @@ const AlbumList = () => {
offset: 0,
musicFolderId: musicFolder,
}
: null,
: {
type: 'random',
size: 100,
offset: 0,
recursive: false,
},
})
: apiController({
serverType: config.serverType,
@ -82,7 +87,10 @@ const AlbumList = () => {
musicFolderId: musicFolder,
recursive: true,
}
: null,
: {
type: album.active.filter,
recursive: true,
},
}),
{
cacheTime: 3600000, // Stay in cache for 1 hour
@ -258,8 +266,8 @@ const AlbumList = () => {
}}
cardSubtitle={{
prefix: 'artist',
property: 'artist',
urlProperty: 'artistId',
property: 'albumArtist',
urlProperty: 'albumArtistId',
unit: '',
}}
playClick={{ type: 'album', idProperty: 'id' }}

Loading…
Cancel
Save