half decent album list with art

This commit is contained in:
austinried
2021-06-21 22:39:10 +09:00
parent ff94889644
commit b4fee0aff4
18 changed files with 1964 additions and 90 deletions

View File

@@ -1,5 +1,6 @@
import { DOMParser } from 'xmldom';
import { GetAlbumList2Params, GetAlbumListParams, GetArtistInfo2Params, GetArtistInfoParams, GetIndexesParams } from './params';
import RNFS from 'react-native-fs';
import { GetAlbumList2Params, GetAlbumListParams, GetArtistInfo2Params, GetArtistInfoParams, GetCoverArtParams, GetIndexesParams } from './params';
import { GetAlbumList2Response, GetAlbumListResponse, GetArtistInfo2Response, GetArtistInfoResponse, GetArtistsResponse, GetIndexesResponse, SubsonicResponse } from './responses';
export class SubsonicApiError extends Error {
@@ -35,7 +36,7 @@ export class SubsonicApiClient {
this.params.append('c', 'subsonify-cool-unique-app-string')
}
private async apiRequest(method: string, params?: {[key: string]: any}): Promise<Document> {
private buildUrl(method: string, params?: {[key: string]: any}): string {
let query = this.params.toString();
if (params) {
const urlParams = this.obj2Params(params);
@@ -45,10 +46,21 @@ export class SubsonicApiClient {
}
const url = `${this.address}/rest/${method}?${query}`;
console.log(url);
return url;
}
const response = await fetch(url);
private async apiDownload(method: string, path: string, params?: {[key: string]: any}): Promise<string> {
await RNFS.downloadFile({
fromUrl: this.buildUrl(method, params),
toFile: path,
}).promise;
return path;
}
private async apiGetXml(method: string, params?: {[key: string]: any}): Promise<Document> {
const response = await fetch(this.buildUrl(method, params));
const text = await response.text();
console.log(text);
@@ -80,7 +92,7 @@ export class SubsonicApiClient {
//
async ping(): Promise<SubsonicResponse<null>> {
const xml = await this.apiRequest('ping');
const xml = await this.apiGetXml('ping');
return new SubsonicResponse<null>(xml, null);
}
@@ -89,22 +101,22 @@ export class SubsonicApiClient {
//
async getArtists(): Promise<SubsonicResponse<GetArtistsResponse>> {
const xml = await this.apiRequest('getArtists');
const xml = await this.apiGetXml('getArtists');
return new SubsonicResponse<GetArtistsResponse>(xml, new GetArtistsResponse(xml));
}
async getIndexes(params?: GetIndexesParams): Promise<SubsonicResponse<GetIndexesResponse>> {
const xml = await this.apiRequest('getIndexes', params);
const xml = await this.apiGetXml('getIndexes', params);
return new SubsonicResponse<GetIndexesResponse>(xml, new GetIndexesResponse(xml));
}
async getArtistInfo(params: GetArtistInfoParams): Promise<SubsonicResponse<GetArtistInfoResponse>> {
const xml = await this.apiRequest('getArtistInfo', params);
const xml = await this.apiGetXml('getArtistInfo', params);
return new SubsonicResponse<GetArtistInfoResponse>(xml, new GetArtistInfoResponse(xml));
}
async getArtistInfo2(params: GetArtistInfo2Params): Promise<SubsonicResponse<GetArtistInfo2Response>> {
const xml = await this.apiRequest('getArtistInfo2', params);
const xml = await this.apiGetXml('getArtistInfo2', params);
return new SubsonicResponse<GetArtistInfo2Response>(xml, new GetArtistInfo2Response(xml));
}
@@ -113,12 +125,21 @@ export class SubsonicApiClient {
//
async getAlbumList(params: GetAlbumListParams): Promise<SubsonicResponse<GetAlbumListResponse>> {
const xml = await this.apiRequest('getAlbumList', params);
const xml = await this.apiGetXml('getAlbumList', params);
return new SubsonicResponse<GetAlbumListResponse>(xml, new GetAlbumListResponse(xml));
}
async getAlbumList2(params: GetAlbumList2Params): Promise<SubsonicResponse<GetAlbumList2Response>> {
const xml = await this.apiRequest('getAlbumList2', params);
const xml = await this.apiGetXml('getAlbumList2', params);
return new SubsonicResponse<GetAlbumList2Response>(xml, new GetAlbumList2Response(xml));
}
//
// Media retrieval
//
async getCoverArt(params: GetCoverArtParams): Promise<string> {
const path = `${RNFS.DocumentDirectoryPath}/image_cache/${params.id}`;
return await this.apiDownload('getCoverArt', path, params);
}
}

View File

@@ -45,3 +45,12 @@ export type GetAlbumList2Params = {
} | GetAlbumList2TypeByYear | GetAlbumList2TypeByGenre;
export type GetAlbumListParams = GetAlbumList2Params;
//
// Media retrieval
//
export type GetCoverArtParams = {
id: string;
size?: string;
}