rework to work with non forked version
This commit is contained in:
@@ -2,24 +2,24 @@ import { BaseClient } from "@dpu/shared";
|
||||
import { printNetworkError } from "@dpu/shared/dist/logger.js";
|
||||
|
||||
export class TidalClient extends BaseClient {
|
||||
async get<T>(endpoint: string): Promise<T> {
|
||||
try {
|
||||
const response = await this.getAxios().get<T>(`/${endpoint}`);
|
||||
async get<T>(endpoint: string): Promise<T> {
|
||||
try {
|
||||
const response = await this.getAxios().get<T>(`/${endpoint}`);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async post<T>(endpoint: string, data: unknown): Promise<T> {
|
||||
try {
|
||||
const response = await this.getAxios().post<T>(`/${endpoint}`, data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
async put<T>(endpoint: string, data: unknown): Promise<T> {
|
||||
try {
|
||||
const response = await this.getAxios().put<T>(`/${endpoint}`, data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,95 +1,107 @@
|
||||
import {
|
||||
BaseService,
|
||||
type ServiceResult,
|
||||
type TidalSong,
|
||||
type TidalVolume,
|
||||
BaseService,
|
||||
type ServiceResult,
|
||||
type TidalGetCurrent,
|
||||
type TidalPutVolume,
|
||||
} from "@dpu/shared";
|
||||
import { logWarning } from "@dpu/shared/dist/logger.js";
|
||||
import type { TidalClient } from "./client.js";
|
||||
|
||||
export class TidalService extends BaseService<TidalClient> {
|
||||
async getSongFormatted(): Promise<ServiceResult<string>> {
|
||||
const req = await this.getSong();
|
||||
if (req.successful) {
|
||||
const song = req.result as TidalSong;
|
||||
const status = song.status === "playing" ? "▶️" : "⏸️";
|
||||
return this.getSuccessfulResult(
|
||||
`listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`,
|
||||
);
|
||||
} else {
|
||||
return this.getErrorResult(req.result as string);
|
||||
}
|
||||
}
|
||||
async getSongFormatted(): Promise<ServiceResult<string>> {
|
||||
const req = await this.getSong();
|
||||
if (req.successful) {
|
||||
const song = req.result as TidalGetCurrent;
|
||||
const status = song.status === "playing" ? "▶️" : "⏸️";
|
||||
return this.getSuccessfulResult(
|
||||
`listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`,
|
||||
);
|
||||
} else {
|
||||
return this.getErrorResult(req.result as string);
|
||||
}
|
||||
}
|
||||
|
||||
async getSong(): Promise<ServiceResult<TidalSong | string>> {
|
||||
try {
|
||||
const response = await this.getClient().get<TidalSong>("current");
|
||||
async getSong(): Promise<ServiceResult<TidalGetCurrent | string>> {
|
||||
try {
|
||||
const response = await this.getClient().get<TidalGetCurrent>("current");
|
||||
|
||||
return this.getSuccessfulResult(response);
|
||||
} catch {
|
||||
const error_message = "error getting song from tidal";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
return this.getSuccessfulResult(response);
|
||||
} catch {
|
||||
const error_message = "error getting song from tidal";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
async getVolume(): Promise<ServiceResult<TidalVolume | string>> {
|
||||
try {
|
||||
const response = await this.getClient().get<TidalVolume>("volume");
|
||||
async getVolume(): Promise<ServiceResult<TidalPutVolume | string>> {
|
||||
try {
|
||||
const response = await this.getClient().get<TidalGetCurrent>("current");
|
||||
|
||||
return this.getSuccessfulResult(response);
|
||||
} catch {
|
||||
const error_message = "error getting volume from tidal";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
return this.getSuccessfulResult({
|
||||
volume: response.volume,
|
||||
});
|
||||
} catch {
|
||||
const error_message = "error getting volume from tidal";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
|
||||
clamp(value: number): number {
|
||||
return Math.min(Math.max(value, 0), 100);
|
||||
}
|
||||
clamp(value: number): number {
|
||||
return Math.min(Math.max(value, 0), 100);
|
||||
}
|
||||
|
||||
async setVolume(
|
||||
argument: string,
|
||||
): Promise<ServiceResult<TidalVolume | string>> {
|
||||
const value = parseInt(argument, 10);
|
||||
// relative
|
||||
const adjustMatch = argument.match(/^([+-])(\d+)$/);
|
||||
if (adjustMatch) {
|
||||
const req = await this.getVolume();
|
||||
if (req.successful) {
|
||||
const volume = req.result as TidalVolume;
|
||||
const wantedVolume = volume.volume + value;
|
||||
const clampWantedVolume = this.clamp(wantedVolume);
|
||||
return await this.setVolumeToTidal(clampWantedVolume);
|
||||
}
|
||||
}
|
||||
async setVolume(
|
||||
argument: string,
|
||||
): Promise<ServiceResult<TidalPutVolume | string>> {
|
||||
const value = this.percentageToDecimal(argument);
|
||||
// relative
|
||||
const adjustMatch = argument.match(/^([+-])/);
|
||||
if (adjustMatch) {
|
||||
const req = await this.getVolume();
|
||||
if (req.successful) {
|
||||
const volume = req.result as TidalPutVolume;
|
||||
const wantedVolume = volume.volume + value;
|
||||
const clampWantedVolume = this.clamp(wantedVolume);
|
||||
return await this.setVolumeToTidal(clampWantedVolume);
|
||||
}
|
||||
}
|
||||
|
||||
// absolute
|
||||
const setMatch = argument.match(/^(\d+)$/);
|
||||
if (setMatch) {
|
||||
const clampValue = this.clamp(value);
|
||||
return await this.setVolumeToTidal(clampValue);
|
||||
}
|
||||
// absolute
|
||||
const setMatch = argument.match(/^(\d+)/);
|
||||
if (setMatch) {
|
||||
const clampValue = this.clamp(value);
|
||||
return await this.setVolumeToTidal(clampValue);
|
||||
}
|
||||
|
||||
const error_message = "error parsing volume to set";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
const error_message = "error parsing volume to set";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
|
||||
async setVolumeToTidal(
|
||||
volume: number,
|
||||
): Promise<ServiceResult<TidalVolume | string>> {
|
||||
try {
|
||||
const response = await this.getClient().post<TidalVolume>("volume", {
|
||||
volume,
|
||||
});
|
||||
async setVolumeToTidal(
|
||||
volume: number,
|
||||
): Promise<ServiceResult<TidalPutVolume | string>> {
|
||||
try {
|
||||
const response = await this.getClient().put<TidalPutVolume>(
|
||||
`player/volume?volume=${volume}`,
|
||||
{},
|
||||
);
|
||||
|
||||
return this.getSuccessfulResult(response);
|
||||
} catch {
|
||||
const error_message = "error setting volume from tidal";
|
||||
logWarning(error_message);
|
||||
return this.getErrorResult(error_message);
|
||||
}
|
||||
}
|
||||
return this.getSuccessfulResult(response);
|
||||
} catch {
|
||||
const error_message = "error setting volume from tidal";
|
||||
logWarning(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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user