From 6fb4b302947b1a0f00eea01c9167688bfbf28892 Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Tue, 15 Jun 2021 13:17:40 +0900 Subject: [PATCH] impl artistInfo --- src/subsonic/client.ts | 43 +++++++++++++++++++++++++++++----- src/subsonic/response.ts | 50 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/src/subsonic/client.ts b/src/subsonic/client.ts index e802441..dc1481c 100644 --- a/src/subsonic/client.ts +++ b/src/subsonic/client.ts @@ -1,5 +1,5 @@ import { DOMParser } from 'xmldom'; -import { GetArtistsResponse, GetIndexesResponse, SubsonicResponse } from './response'; +import { GetArtistInfo2Response, GetArtistInfoResponse, GetArtistsResponse, GetIndexesResponse, SubsonicResponse } from './response'; export class SubsonicApiClient { address: string; @@ -50,22 +50,19 @@ export class SubsonicApiClient { async getArtists(): Promise> { const xml = await this.apiRequest('getArtists'); - const data = new GetArtistsResponse(xml); - const response = new SubsonicResponse(xml, data); + const response = new SubsonicResponse(xml, new GetArtistsResponse(xml)); return response; } async getIndexes(ifModifiedSince?: number): Promise> { const params = new URLSearchParams(); - console.log(params); if (ifModifiedSince !== undefined) { params.append('ifModifiedSince', ifModifiedSince.toString()); } const xml = await this.apiRequest('getIndexes', params); - const data = new GetIndexesResponse(xml); - const response = new SubsonicResponse(xml, data); + const response = new SubsonicResponse(xml, new GetIndexesResponse(xml)); console.log(response.status); console.log(response.version); @@ -73,6 +70,40 @@ export class SubsonicApiClient { return response; } + + async getArtistInfo(id: string, count?: number, includeNotPresent?: boolean): Promise> { + const params = new URLSearchParams(); + params.append('id', id); + if (count !== undefined) { + params.append('count', count.toString()); + } + if (includeNotPresent !== undefined) { + params.append('includeNotPresent', includeNotPresent.toString()); + } + + const xml = await this.apiRequest('getArtistInfo', params); + const response = new SubsonicResponse(xml, new GetArtistInfoResponse(xml)); + console.log(response.data); + + return response; + } + + async getArtistInfo2(id: string, count?: number, includeNotPresent?: boolean): Promise> { + const params = new URLSearchParams(); + params.append('id', id); + if (count !== undefined) { + params.append('count', count.toString()); + } + if (includeNotPresent !== undefined) { + params.append('includeNotPresent', includeNotPresent.toString()); + } + + const xml = await this.apiRequest('getArtistInfo2', params); + const response = new SubsonicResponse(xml, new GetArtistInfo2Response(xml)); + console.log(response.data); + + return response; + } } export class SubsonicApiError extends Error { diff --git a/src/subsonic/response.ts b/src/subsonic/response.ts index 0040922..c25434c 100644 --- a/src/subsonic/response.ts +++ b/src/subsonic/response.ts @@ -1,4 +1,4 @@ -import { Artist, ArtistID3 } from "./element"; +import { Artist, ArtistID3, BaseArtist } from "./element"; export type ResponseStatus = 'ok' | 'failed'; @@ -45,3 +45,51 @@ export class GetIndexesResponse { } } } + +class BaseGetArtistInfoResponse { + similarArtists: T[] = []; + biography?: string; + musicBrainzId?: string; + lastFmUrl?: string; + smallImageUrl?: string; + mediumImageUrl?: string; + largeImageUrl?: string; + + constructor(xml: Document, artistType: new (e: Element) => T) { + if (xml.getElementsByTagName('biography').length > 0) { + this.biography = xml.getElementsByTagName('biography')[0].textContent as string; + } + if (xml.getElementsByTagName('musicBrainzId').length > 0) { + this.musicBrainzId = xml.getElementsByTagName('musicBrainzId')[0].textContent as string; + } + if (xml.getElementsByTagName('lastFmUrl').length > 0) { + this.lastFmUrl = xml.getElementsByTagName('lastFmUrl')[0].textContent as string; + } + if (xml.getElementsByTagName('smallImageUrl').length > 0) { + this.smallImageUrl = xml.getElementsByTagName('smallImageUrl')[0].textContent as string; + } + if (xml.getElementsByTagName('mediumImageUrl').length > 0) { + this.mediumImageUrl = xml.getElementsByTagName('mediumImageUrl')[0].textContent as string; + } + if (xml.getElementsByTagName('largeImageUrl').length > 0) { + this.largeImageUrl = xml.getElementsByTagName('largeImageUrl')[0].textContent as string; + } + + const similarArtistElements = xml.getElementsByTagName('similarArtist'); + for (let i = 0; i < similarArtistElements.length; i++) { + this.similarArtists.push(new artistType(similarArtistElements[i])); + } + } +} + +export class GetArtistInfoResponse extends BaseGetArtistInfoResponse { + constructor(xml: Document) { + super(xml, Artist); + } +} + +export class GetArtistInfo2Response extends BaseGetArtistInfoResponse { + constructor(xml: Document) { + super(xml, ArtistID3); + } +}