impl artistInfo

This commit is contained in:
austinried 2021-06-15 13:17:40 +09:00
parent 5ce3b5bcdf
commit 6fb4b30294
2 changed files with 86 additions and 7 deletions

View File

@ -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<SubsonicResponse<GetArtistsResponse>> {
const xml = await this.apiRequest('getArtists');
const data = new GetArtistsResponse(xml);
const response = new SubsonicResponse<GetArtistsResponse>(xml, data);
const response = new SubsonicResponse<GetArtistsResponse>(xml, new GetArtistsResponse(xml));
return response;
}
async getIndexes(ifModifiedSince?: number): Promise<SubsonicResponse<GetIndexesResponse>> {
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<GetIndexesResponse>(xml, data);
const response = new SubsonicResponse<GetIndexesResponse>(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<SubsonicResponse<GetArtistInfoResponse>> {
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<GetArtistInfoResponse>(xml, new GetArtistInfoResponse(xml));
console.log(response.data);
return response;
}
async getArtistInfo2(id: string, count?: number, includeNotPresent?: boolean): Promise<SubsonicResponse<GetArtistInfo2Response>> {
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<GetArtistInfo2Response>(xml, new GetArtistInfo2Response(xml));
console.log(response.data);
return response;
}
}
export class SubsonicApiError extends Error {

View File

@ -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<T extends BaseArtist> {
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<Artist> {
constructor(xml: Document) {
super(xml, Artist);
}
}
export class GetArtistInfo2Response extends BaseGetArtistInfoResponse<ArtistID3> {
constructor(xml: Document) {
super(xml, ArtistID3);
}
}