rework to work with non forked version

This commit is contained in:
Darius
2026-02-03 21:55:10 +01:00
parent fd27cac031
commit 6859ba27b9
3 changed files with 301 additions and 696 deletions

793
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -2,24 +2,24 @@ import { BaseClient } from "@dpu/shared";
import { printNetworkError } from "@dpu/shared/dist/logger.js"; import { printNetworkError } from "@dpu/shared/dist/logger.js";
export class TidalClient extends BaseClient { export class TidalClient extends BaseClient {
async get<T>(endpoint: string): Promise<T> { async get<T>(endpoint: string): Promise<T> {
try { try {
const response = await this.getAxios().get<T>(`/${endpoint}`); const response = await this.getAxios().get<T>(`/${endpoint}`);
return response.data; return response.data;
} catch (error) { } catch (error) {
printNetworkError(error); printNetworkError(error);
throw error; throw error;
} }
} }
async post<T>(endpoint: string, data: unknown): Promise<T> { async put<T>(endpoint: string, data: unknown): Promise<T> {
try { try {
const response = await this.getAxios().post<T>(`/${endpoint}`, data); const response = await this.getAxios().put<T>(`/${endpoint}`, data);
return response.data; return response.data;
} catch (error) { } catch (error) {
printNetworkError(error); printNetworkError(error);
throw error; throw error;
} }
} }
} }

View File

@@ -1,95 +1,107 @@
import { import {
BaseService, BaseService,
type ServiceResult, type ServiceResult,
type TidalSong, type TidalGetCurrent,
type TidalVolume, type TidalPutVolume,
} from "@dpu/shared"; } from "@dpu/shared";
import { logWarning } from "@dpu/shared/dist/logger.js"; import { logWarning } from "@dpu/shared/dist/logger.js";
import type { TidalClient } from "./client.js"; import type { TidalClient } from "./client.js";
export class TidalService extends BaseService<TidalClient> { export class TidalService extends BaseService<TidalClient> {
async getSongFormatted(): Promise<ServiceResult<string>> { async getSongFormatted(): Promise<ServiceResult<string>> {
const req = await this.getSong(); const req = await this.getSong();
if (req.successful) { if (req.successful) {
const song = req.result as TidalSong; const song = req.result as TidalGetCurrent;
const status = song.status === "playing" ? "▶️" : "⏸️"; const status = song.status === "playing" ? "▶️" : "⏸️";
return this.getSuccessfulResult( return this.getSuccessfulResult(
`listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`, `listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`,
); );
} else { } else {
return this.getErrorResult(req.result as string); return this.getErrorResult(req.result as string);
} }
} }
async getSong(): Promise<ServiceResult<TidalSong | string>> { async getSong(): Promise<ServiceResult<TidalGetCurrent | string>> {
try { try {
const response = await this.getClient().get<TidalSong>("current"); const response = await this.getClient().get<TidalGetCurrent>("current");
return this.getSuccessfulResult(response); return this.getSuccessfulResult(response);
} catch { } catch {
const error_message = "error getting song from tidal"; const error_message = "error getting song from tidal";
logWarning(error_message); logWarning(error_message);
return this.getErrorResult(error_message); return this.getErrorResult(error_message);
} }
} }
async getVolume(): Promise<ServiceResult<TidalVolume | string>> { async getVolume(): Promise<ServiceResult<TidalPutVolume | string>> {
try { try {
const response = await this.getClient().get<TidalVolume>("volume"); const response = await this.getClient().get<TidalGetCurrent>("current");
return this.getSuccessfulResult(response); return this.getSuccessfulResult({
} catch { volume: response.volume,
const error_message = "error getting volume from tidal"; });
logWarning(error_message); } catch {
return this.getErrorResult(error_message); const error_message = "error getting volume from tidal";
} logWarning(error_message);
} return this.getErrorResult(error_message);
}
}
clamp(value: number): number { clamp(value: number): number {
return Math.min(Math.max(value, 0), 100); return Math.min(Math.max(value, 0), 100);
} }
async setVolume( async setVolume(
argument: string, argument: string,
): Promise<ServiceResult<TidalVolume | string>> { ): Promise<ServiceResult<TidalPutVolume | string>> {
const value = parseInt(argument, 10); const value = this.percentageToDecimal(argument);
// relative // relative
const adjustMatch = argument.match(/^([+-])(\d+)$/); const adjustMatch = argument.match(/^([+-])/);
if (adjustMatch) { if (adjustMatch) {
const req = await this.getVolume(); const req = await this.getVolume();
if (req.successful) { if (req.successful) {
const volume = req.result as TidalVolume; const volume = req.result as TidalPutVolume;
const wantedVolume = volume.volume + value; const wantedVolume = volume.volume + value;
const clampWantedVolume = this.clamp(wantedVolume); const clampWantedVolume = this.clamp(wantedVolume);
return await this.setVolumeToTidal(clampWantedVolume); return await this.setVolumeToTidal(clampWantedVolume);
} }
} }
// absolute // absolute
const setMatch = argument.match(/^(\d+)$/); const setMatch = argument.match(/^(\d+)/);
if (setMatch) { if (setMatch) {
const clampValue = this.clamp(value); const clampValue = this.clamp(value);
return await this.setVolumeToTidal(clampValue); return await this.setVolumeToTidal(clampValue);
} }
const error_message = "error parsing volume to set"; const error_message = "error parsing volume to set";
logWarning(error_message); logWarning(error_message);
return this.getErrorResult(error_message); return this.getErrorResult(error_message);
} }
async setVolumeToTidal( async setVolumeToTidal(
volume: number, volume: number,
): Promise<ServiceResult<TidalVolume | string>> { ): Promise<ServiceResult<TidalPutVolume | string>> {
try { try {
const response = await this.getClient().post<TidalVolume>("volume", { const response = await this.getClient().put<TidalPutVolume>(
volume, `player/volume?volume=${volume}`,
}); {},
);
return this.getSuccessfulResult(response); return this.getSuccessfulResult(response);
} catch { } catch {
const error_message = "error setting volume from tidal"; const error_message = "error setting volume from tidal";
logWarning(error_message); logWarning(error_message);
return this.getErrorResult(error_message); return this.getErrorResult(error_message);
} }
} }
decimalToPercentage(decimal: number): number {
return decimal * 100;
}
percentageToDecimal(percentage: string): number {
const num = parseFloat(percentage);
return num / 100;
}
} }