Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix/enhancement]: Support Navidrome structured lyrics #484

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/renderer/api/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ import type {
SearchResponse,
LyricsArgs,
LyricsResponse,
ServerInfo,
ServerInfoArgs,
StructuredLyricsArgs,
StructuredLyric,
} from '/@/renderer/api/types';
import { ServerType } from '/@/renderer/types';
import { DeletePlaylistResponse, RandomSongListArgs } from './types';
Expand Down Expand Up @@ -85,8 +89,10 @@ export type ControllerEndpoint = Partial<{
getPlaylistList: (args: PlaylistListArgs) => Promise<PlaylistListResponse>;
getPlaylistSongList: (args: PlaylistSongListArgs) => Promise<SongListResponse>;
getRandomSongList: (args: RandomSongListArgs) => Promise<SongListResponse>;
getServerInfo: (args: ServerInfoArgs) => Promise<ServerInfo>;
getSongDetail: (args: SongDetailArgs) => Promise<SongDetailResponse>;
getSongList: (args: SongListArgs) => Promise<SongListResponse>;
getStructuredLyrics: (args: StructuredLyricsArgs) => Promise<StructuredLyric[]>;
getTopSongs: (args: TopSongListArgs) => Promise<TopSongListResponse>;
getUserList: (args: UserListArgs) => Promise<UserListResponse>;
removeFromPlaylist: (args: RemoveFromPlaylistArgs) => Promise<RemoveFromPlaylistResponse>;
Expand Down Expand Up @@ -129,8 +135,10 @@ const endpoints: ApiController = {
getPlaylistList: jfController.getPlaylistList,
getPlaylistSongList: jfController.getPlaylistSongList,
getRandomSongList: jfController.getRandomSongList,
getServerInfo: jfController.getServerInfo,
getSongDetail: jfController.getSongDetail,
getSongList: jfController.getSongList,
getStructuredLyrics: undefined,
getTopSongs: jfController.getTopSongList,
getUserList: undefined,
removeFromPlaylist: jfController.removeFromPlaylist,
Expand Down Expand Up @@ -165,8 +173,10 @@ const endpoints: ApiController = {
getPlaylistList: ndController.getPlaylistList,
getPlaylistSongList: ndController.getPlaylistSongList,
getRandomSongList: ssController.getRandomSongList,
getServerInfo: ssController.getServerInfo,
getSongDetail: ndController.getSongDetail,
getSongList: ndController.getSongList,
getStructuredLyrics: ssController.getStructuredLyrics,
getTopSongs: ssController.getTopSongList,
getUserList: ndController.getUserList,
removeFromPlaylist: ndController.removeFromPlaylist,
Expand Down Expand Up @@ -198,8 +208,10 @@ const endpoints: ApiController = {
getMusicFolderList: ssController.getMusicFolderList,
getPlaylistDetail: undefined,
getPlaylistList: undefined,
getServerInfo: ssController.getServerInfo,
getSongDetail: undefined,
getSongList: undefined,
getStructuredLyrics: ssController.getStructuredLyrics,
getTopSongs: ssController.getTopSongList,
getUserList: undefined,
scrobble: ssController.scrobble,
Expand Down Expand Up @@ -481,6 +493,24 @@ const getLyrics = async (args: LyricsArgs) => {
)?.(args);
};

const getServerInfo = async (args: ServerInfoArgs) => {
return (
apiController(
'getServerInfo',
args.apiClientProps.server?.type,
) as ControllerEndpoint['getServerInfo']
)?.(args);
};

const getStructuredLyrics = async (args: StructuredLyricsArgs) => {
return (
apiController(
'getStructuredLyrics',
args.apiClientProps.server?.type,
) as ControllerEndpoint['getStructuredLyrics']
)?.(args);
};

export const controller = {
addToPlaylist,
authenticate,
Expand All @@ -500,8 +530,10 @@ export const controller = {
getPlaylistList,
getPlaylistSongList,
getRandomSongList,
getServerInfo,
getSongDetail,
getSongList,
getStructuredLyrics,
getTopSongList,
getUserList,
removeFromPlaylist,
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/api/jellyfin/jellyfin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ export const contract = c.router({
400: jfType._response.error,
},
},
getServerInfo: {
method: 'GET',
path: 'system/info',
responses: {
200: jfType._response.serverInfo,
400: jfType._response.error,
},
},
getSimilarArtistList: {
method: 'GET',
path: 'artists/:id/similar',
Expand Down
15 changes: 15 additions & 0 deletions src/renderer/api/jellyfin/jellyfin-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import {
genreListSortMap,
SongDetailArgs,
SongDetailResponse,
ServerInfo,
ServerInfoArgs,
} from '/@/renderer/api/types';
import { jfApiClient } from '/@/renderer/api/jellyfin/jellyfin-api';
import { jfNormalize } from './jellyfin-normalize';
Expand Down Expand Up @@ -946,6 +948,18 @@ const getSongDetail = async (args: SongDetailArgs): Promise<SongDetailResponse>
return jfNormalize.song(res.body, apiClientProps.server, '');
};

const getServerInfo = async (args: ServerInfoArgs): Promise<ServerInfo> => {
const { apiClientProps } = args;

const res = await jfApiClient(apiClientProps).getServerInfo();

if (res.status !== 200) {
throw new Error('Failed to get server info');
}

return { id: apiClientProps.server?.id, version: res.body.Version };
};

export const jfController = {
addToPlaylist,
authenticate,
Expand All @@ -965,6 +979,7 @@ export const jfController = {
getPlaylistList,
getPlaylistSongList,
getRandomSongList,
getServerInfo,
getSongDetail,
getSongList,
getTopSongList,
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/api/jellyfin/jellyfin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,10 @@ const lyrics = z.object({
Lyrics: z.array(lyricText),
});

const serverInfo = z.object({
Version: z.string(),
});

export const jfType = {
_enum: {
albumArtistList: albumArtistListSort,
Expand Down Expand Up @@ -707,6 +711,7 @@ export const jfType = {
removeFromPlaylist,
scrobble,
search,
serverInfo,
song,
songList,
topSongsList,
Expand Down
22 changes: 22 additions & 0 deletions src/renderer/api/subsonic/subsonic-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ export const contract = c.router({
200: ssType._response.randomSongList,
},
},
getServerInfo: {
method: 'GET',
path: 'getOpenSubsonicExtensions.view',
responses: {
200: ssType._response.serverInfo,
},
},
getStructuredLyrics: {
method: 'GET',
path: 'getLyricsBySongId.view',
query: ssType._parameters.structuredLyrics,
responses: {
200: ssType._response.structuredLyrics,
},
},
getTopSongsList: {
method: 'GET',
path: 'getTopSongs.view',
Expand All @@ -58,6 +73,13 @@ export const contract = c.router({
200: ssType._response.topSongsList,
},
},
ping: {
method: 'GET',
path: 'ping.view',
responses: {
200: ssType._response.ping,
},
},
removeFavorite: {
method: 'GET',
path: 'unstar.view',
Expand Down
78 changes: 78 additions & 0 deletions src/renderer/api/subsonic/subsonic-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import {
SearchResponse,
RandomSongListResponse,
RandomSongListArgs,
ServerInfo,
ServerInfoArgs,
StructuredLyricsArgs,
StructuredLyric,
} from '/@/renderer/api/types';
import { randomString } from '/@/renderer/utils';

Expand Down Expand Up @@ -368,12 +372,86 @@ const getRandomSongList = async (args: RandomSongListArgs): Promise<RandomSongLi
};
};

const getServerInfo = async (args: ServerInfoArgs): Promise<ServerInfo> => {
const { apiClientProps } = args;

const ping = await ssApiClient(apiClientProps).ping();

if (ping.status !== 200) {
throw new Error('Failed to ping server');
}

if (!ping.body.openSubsonic || !ping.body.serverVersion) {
return { version: ping.body.version };
}

const res = await ssApiClient(apiClientProps).getServerInfo();

if (res.status !== 200) {
throw new Error('Failed to get server extensions');
}

const features: Record<string, number[]> = {};
for (const extension of res.body.openSubsonicExtensions) {
features[extension.name] = extension.versions;
}

return { features, id: apiClientProps.server?.id, version: ping.body.serverVersion };
};

export const getStructuredLyrics = async (
args: StructuredLyricsArgs,
): Promise<StructuredLyric[]> => {
const { query, apiClientProps } = args;

const res = await ssApiClient(apiClientProps).getStructuredLyrics({
query: {
id: query.songId,
},
});

if (res.status !== 200) {
throw new Error('Failed to get structured lyrics');
}

const lyrics = res.body.lyricsList?.structuredLyrics;

if (!lyrics) {
return [];
}

return lyrics.map((lyric) => {
const baseLyric = {
artist: lyric.displayArtist || '',
lang: lyric.lang,
name: lyric.displayTitle || '',
remote: false,
source: apiClientProps.server?.name || 'music server',
};

if (lyric.synced) {
return {
...baseLyric,
lyrics: lyric.line.map((line) => [line.start!, line.value]),
synced: true,
};
}
return {
...baseLyric,
lyrics: lyric.line.map((line) => [line.value]).join('\n'),
synced: false,
};
});
};

export const ssController = {
authenticate,
createFavorite,
getArtistInfo,
getMusicFolderList,
getRandomSongList,
getServerInfo,
getStructuredLyrics,
getTopSongList,
removeFavorite,
scrobble,
Expand Down
45 changes: 45 additions & 0 deletions src/renderer/api/subsonic/subsonic-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,47 @@ const randomSongList = z.object({
}),
});

const ping = z.object({
openSubsonic: z.boolean().optional(),
serverVersion: z.string().optional(),
version: z.string(),
});

const extension = z.object({
name: z.string(),
versions: z.number().array(),
});

const serverInfo = z.object({
openSubsonicExtensions: z.array(extension),
});

const structuredLyricsParameters = z.object({
id: z.string(),
});

const lyricLine = z.object({
start: z.number().optional(),
value: z.string(),
});

const structuredLyric = z.object({
displayArtist: z.string().optional(),
displayTitle: z.string().optional(),
lang: z.string(),
line: z.array(lyricLine),
offset: z.number().optional(),
synced: z.boolean(),
});

const structuredLyrics = z.object({
lyricsList: z
.object({
structuredLyrics: z.array(structuredLyric).optional(),
})
.optional(),
});

export const ssType = {
_parameters: {
albumList: albumListParameters,
Expand All @@ -217,6 +258,7 @@ export const ssType = {
scrobble: scrobbleParameters,
search3: search3Parameters,
setRating: setRatingParameters,
structuredLyrics: structuredLyricsParameters,
topSongsList: topSongsListParameters,
},
_response: {
Expand All @@ -229,12 +271,15 @@ export const ssType = {
baseResponse,
createFavorite,
musicFolderList,
ping,
randomSongList,
removeFavorite,
scrobble,
search3,
serverInfo,
setRating,
song,
structuredLyrics,
topSongsList,
},
};
Loading
Loading