mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
[feature-107] we can now download playlist from beatsaver + other internal changes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { BsvMapDetail } from "shared/models/maps";
|
||||
import { BsvPlaylist, BsvPlaylistPage, MapFilter, SearchParams, SearchResponse } from "shared/models/maps/beat-saver.model";
|
||||
import { BsvPlaylist, BsvPlaylistPage, MapFilter, PlaylistSearchParams, PlaylistSearchResponse, SearchParams, SearchResponse } from "shared/models/maps/beat-saver.model";
|
||||
import { RequestService } from "../../request.service";
|
||||
|
||||
export class BeatSaverApiService {
|
||||
@@ -20,6 +20,10 @@ export class BeatSaverApiService {
|
||||
this.request = RequestService.getInstance();
|
||||
}
|
||||
|
||||
private objectToStringRecord(obj: Record<string, any>): Record<string, string> {
|
||||
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, String(value)] as [string, string]));
|
||||
}
|
||||
|
||||
private mapFilterToUrlParams(filter: MapFilter): URLSearchParams {
|
||||
if (!filter) {
|
||||
return new URLSearchParams();
|
||||
@@ -30,24 +34,7 @@ export class BeatSaverApiService {
|
||||
|
||||
const tags = enbledTagsString || excludedTagsString ? [...enbledTagsString, excludedTagsString].join("|") : null;
|
||||
|
||||
const params = {
|
||||
...(filter.automapper && { automapper: String(filter.automapper) }),
|
||||
...(filter.chroma && { chroma: String(filter.chroma) }),
|
||||
...(filter.cinema && { cinema: String(filter.cinema) }),
|
||||
...(filter.me && { me: String(filter.me) }),
|
||||
...(filter.noodle && { noodle: String(filter.noodle) }),
|
||||
...(filter.ranked && { ranked: String(filter.ranked) }),
|
||||
...(filter.verified && { verified: String(filter.verified) }),
|
||||
...(filter.curated && { curated: String(filter.curated) }),
|
||||
...(filter.fullSpread && { fullSpread: String(filter.fullSpread) }),
|
||||
...(filter.from && { from: String(filter.from) }),
|
||||
...(filter.to && { to: String(filter.to) }),
|
||||
...(tags && { tags: String(tags) }),
|
||||
...(filter.minDuration && { minDuration: String(filter.minDuration) }),
|
||||
...(filter.maxDuration && { maxDuration: String(filter.maxDuration) }),
|
||||
...(filter.minNps && { minNps: String(filter.minNps) }),
|
||||
...(filter.maxNps && { maxNps: String(filter.maxNps) }),
|
||||
};
|
||||
const params: Record<string, string> = this.objectToStringRecord({...filter, tags});
|
||||
|
||||
return new URLSearchParams(params);
|
||||
}
|
||||
@@ -95,14 +82,15 @@ export class BeatSaverApiService {
|
||||
return this.request.getJSON<BsvMapDetail>(`${this.bsaverApiUrl}/maps/id/${id}`);
|
||||
}
|
||||
|
||||
public async searchMaps(search: SearchParams): Promise<SearchResponse> {
|
||||
public searchMaps(search: SearchParams): Promise<SearchResponse> {
|
||||
const url = new URL(`${this.bsaverApiUrl}/search/text/${search?.page ?? 0}`);
|
||||
url.search = this.searchParamsToUrlParams(search).toString();
|
||||
return this.request.getJSON<SearchResponse>(url.toString());
|
||||
}
|
||||
|
||||
public async getPlaylistDetails(id: string): Promise<BsvPlaylist> {
|
||||
const res = await this.request.getJSON<BsvPlaylistPage>(`${this.bsaverApiUrl}/playlists/id/${id}/0`);
|
||||
return res.playlist;
|
||||
public searchPlaylists(search: PlaylistSearchParams): Promise<PlaylistSearchResponse> {
|
||||
const url = new URL(`${this.bsaverApiUrl}/playlists/search/${search?.page ?? 0}`);
|
||||
url.search = new URLSearchParams(this.objectToStringRecord(search)).toString();
|
||||
return this.request.getJSON<PlaylistSearchResponse>(url.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { splitIntoChunk } from "../../../../shared/helpers/array.helpers";
|
||||
import { BsvMapDetail } from "shared/models/maps";
|
||||
import { BsvPlaylist, SearchParams } from "shared/models/maps/beat-saver.model";
|
||||
import { BsvPlaylist, PlaylistSearchParams, SearchParams } from "shared/models/maps/beat-saver.model";
|
||||
import { BeatSaverApiService } from "./beat-saver-api.service";
|
||||
import log from "electron-log";
|
||||
|
||||
@@ -59,11 +59,17 @@ export class BeatSaverService {
|
||||
})
|
||||
.catch(e => {
|
||||
log.error(e);
|
||||
return [];
|
||||
return e;
|
||||
});
|
||||
}
|
||||
|
||||
public async getPlaylistPage(id: string): Promise<BsvPlaylist> {
|
||||
return this.bsaverApi.getPlaylistDetails(id);
|
||||
public searchPlaylists(search: PlaylistSearchParams): Promise<BsvPlaylist[]> {
|
||||
return this.bsaverApi
|
||||
.searchPlaylists(search)
|
||||
.then(res => res.docs)
|
||||
.catch(e => {
|
||||
log.error(e);
|
||||
return e;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user