Artist images from getArtistInfo

This commit is contained in:
simojenki
2021-03-05 14:46:12 +11:00
parent 4dae907826
commit 5ad0da5168
7 changed files with 407 additions and 210 deletions

View File

@@ -22,15 +22,19 @@ export type AuthFailure = {
message: string;
};
export type Artist = {
export type ArtistSummary = {
id: string;
name: string;
image: {
small: string | undefined,
medium: string | undefined,
large: string | undefined,
}
image: Images
}
export type Images = {
small: string | undefined,
medium: string | undefined,
large: string | undefined,
}
export type Artist = ArtistSummary & {
};
export type Album = {
@@ -71,7 +75,7 @@ export interface MusicService {
}
export interface MusicLibrary {
artists(q: ArtistQuery): Promise<Result<Artist>>;
artist(id: string): Artist;
artists(q: ArtistQuery): Promise<Result<ArtistSummary>>;
artist(id: string): Promise<Artist>;
albums(q: AlbumQuery): Promise<Result<Album>>;
}

View File

@@ -4,11 +4,14 @@ import {
MusicService,
Album,
Artist,
ArtistSummary,
Result,
slice2,
asResult,
AlbumQuery,
ArtistQuery,
MusicLibrary,
Images,
} from "./music_service";
import X2JS from "x2js";
@@ -35,15 +38,17 @@ export type SubsonicResponse = {
_status: string;
};
export type artist = {
_id: string;
_name: string;
_albumCount: string;
_artistImageUrl: string | undefined;
};
export type GetArtistsResponse = SubsonicResponse & {
artists: {
index: {
artist: {
_id: string;
_name: string;
_albumCount: string;
_artistImageUrl: string | undefined;
}[];
artist: artist[];
_name: string;
}[];
};
@@ -65,16 +70,29 @@ export type artistInfo = {
largeImageUrl: string | undefined;
};
export type ArtistInfo = {
image: Images;
};
export type GetArtistInfoResponse = {
artistInfo: artistInfo;
};
export type GetArtistResponse = {
artist: artist;
};
export function isError(
subsonicResponse: SubsonicResponse
): subsonicResponse is SubsonicError {
return (subsonicResponse as SubsonicError).error !== undefined;
}
export type IdName = {
id: string;
name: string;
};
export class Navidrome implements MusicService {
url: string;
encryption: Encryption;
@@ -124,46 +142,80 @@ export class Navidrome implements MusicService {
)
);
artistInfo = (credentials: Credentials, id: string): Promise<ArtistInfo> =>
this.get<GetArtistInfoResponse>(credentials, "/rest/getArtistInfo", {
id,
}).then((it) => ({
image: {
small: it.artistInfo.smallImageUrl,
medium: it.artistInfo.mediumImageUrl,
large: it.artistInfo.largeImageUrl,
},
}));
async login(token: string) {
const navidrome = this;
const credentials: Credentials = this.parseToken(token);
return Promise.resolve({
artists: (q: ArtistQuery): Promise<Result<Artist>> =>
const musicLibrary: MusicLibrary = {
artists: (q: ArtistQuery): Promise<Result<ArtistSummary>> =>
navidrome
.get<GetArtistsResponse>(credentials, "/rest/getArtists")
.then((it) => it.artists.index.flatMap((it) => it.artist))
.then((it) => it.artists.index.flatMap((it) => it.artist || []))
.then((artists) =>
artists.map((artist) => ({
id: artist._id,
name: artist._name,
}))
)
.then(slice2(q))
.then(asResult)
.then((result) =>
Promise.all(
artists.map((artist) =>
navidrome
.get<GetArtistInfoResponse>(
credentials,
"/rest/getArtistInfo",
{ id: artist._id }
)
.then((it) => it.artistInfo)
.then((artistInfo) => ({
id: artist._id,
name: artist._name,
image: {
small: artistInfo.smallImageUrl,
medium: artistInfo.mediumImageUrl,
large: artistInfo.largeImageUrl,
},
}))
result.results.map((idName: IdName) =>
navidrome.artistInfo(credentials, idName.id).then((artist) => ({
total: result.total,
result: {
id: idName.id,
name: idName.name,
image: artist.image,
},
}))
)
)
)
.then(slice2(q))
.then(asResult),
artist: (id: string) => ({
id,
name: id,
image: { small: undefined, medium: undefined, large: undefined },
}),
.then((resultWithInfo) => {
return {
total: resultWithInfo[0]?.total || 0,
results: resultWithInfo.map((it) => it.result),
};
}),
artist: async (id: string): Promise<Artist> => {
return navidrome
.get<GetArtistResponse>(credentials, "/rest/getArtist", {
id,
})
.then(async (artist: GetArtistResponse) => {
return navidrome
.get<GetArtistInfoResponse>(credentials, "/rest/getArtistInfo", {
id,
})
.then((artistInfo: GetArtistInfoResponse) => ({
id: artist.artist._id,
name: artist.artist._name,
image: {
small: artistInfo.artistInfo.smallImageUrl,
medium: artistInfo.artistInfo.mediumImageUrl,
large: artistInfo.artistInfo.largeImageUrl,
},
}));
});
},
albums: (_: AlbumQuery): Promise<Result<Album>> => {
return Promise.resolve({ results: [], total: 0 });
},
});
};
return Promise.resolve(musicLibrary);
}
}

View File

@@ -2,17 +2,18 @@ import express, { Express } from "express";
import * as Eta from "eta";
import morgan from "morgan";
import { Sonos, Service } from "./sonos";
import {
Sonos,
Service,
} from "./sonos";
import { SOAP_PATH, STRINGS_ROUTE, PRESENTATION_MAP_ROUTE, LOGIN_ROUTE } from './smapi';
SOAP_PATH,
STRINGS_ROUTE,
PRESENTATION_MAP_ROUTE,
LOGIN_ROUTE,
} from "./smapi";
import { LinkCodes, InMemoryLinkCodes } from "./link_codes";
import { MusicService, isSuccess } from "./music_service";
// import logger from "./logger";
import bindSmapiSoapServiceToExpress from "./smapi";
function server(
sonos: Sonos,
bonobService: Service,
@@ -65,7 +66,7 @@ function server(
res.render("login", {
bonobService,
linkCode: req.query.linkCode,
loginRoute: LOGIN_ROUTE
loginRoute: LOGIN_ROUTE,
});
});
@@ -85,7 +86,7 @@ function server(
res.render("success", {
message: `Login successful!`,
});
} else {
} else {
res.status(403).render("failure", {
message: `Login failed! ${authResult.message}!`,
});
@@ -112,7 +113,33 @@ function server(
res.send("");
});
bindSmapiSoapServiceToExpress(app, SOAP_PATH, webAddress, linkCodes, musicService);
// app.get("/artist/:artistId/image", (req, res) => {
// console.log(`Trying to load image for ${req.params["artistId"]}, token ${JSON.stringify(req.cookies)}`)
// const authToken = req.headers["X-AuthToken"]! as string;
// const artistId = req.params["artistId"]!;
// musicService
// .login(authToken)
// .then((it) => it.artist(artistId))
// .then(artist => artist.image.small)
// .then((url) => {
// if (url) {
// console.log(`${artistId} sending 307 -> ${url}`)
// res.setHeader("Location", url);
// res.status(307).send();
// } else {
// console.log(`${artistId} sending 404`)
// res.status(404).send();
// }
// });
// });
bindSmapiSoapServiceToExpress(
app,
SOAP_PATH,
webAddress,
linkCodes,
musicService
);
return app;
}

View File

@@ -6,7 +6,12 @@ import path from "path";
import logger from "./logger";
import { LinkCodes } from "./link_codes";
import { Album, Artist, MusicLibrary, MusicService } from "./music_service";
import {
Album,
ArtistSummary,
MusicLibrary,
MusicService,
} from "./music_service";
export const LOGIN_ROUTE = "/login";
export const SOAP_PATH = "/ws/sonos";
@@ -227,35 +232,41 @@ function bindSmapiSoapServiceToExpress(
case "artists":
return await musicLibrary
.artists(paging)
.then(({ results, total }: { results: Artist[], total: number}) =>
getMetadataResult({
mediaCollection: results.map((it) =>
({
.then(
({
results,
total,
}: {
results: ArtistSummary[];
total: number;
}) =>
getMetadataResult({
mediaCollection: results.map((it) => ({
itemType: "artist",
id: `artist:${it.id}`,
artistId: it.id,
title: it.name,
albumArtURI: it.image.small
})
),
index: paging._index,
total,
})
albumArtURI: it.image.small,
})),
index: paging._index,
total,
})
);
case "albums":
return await musicLibrary
.albums(paging)
.then(({ results, total }: { results: Album[], total: number}) =>
getMetadataResult({
mediaCollection: results.map((it) =>
container({
id: `album:${it.id}`,
title: it.name,
})
),
index: paging._index,
total,
})
.then(
({ results, total }: { results: Album[]; total: number }) =>
getMetadataResult({
mediaCollection: results.map((it) =>
container({
id: `album:${it.id}`,
title: it.name,
})
),
index: paging._index,
total,
})
);
default:
throw `Unsupported id:${id}`;