initial
This commit is contained in:
31
src/tidal/client.ts
Normal file
31
src/tidal/client.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { printNetworkError } from "@dpu/shared/dist/utility";
|
||||
import type { AxiosInstance } from "axios";
|
||||
|
||||
export class TidalClient {
|
||||
private axiosInstance: AxiosInstance;
|
||||
|
||||
constructor(axiosInstance: AxiosInstance) {
|
||||
this.axiosInstance = axiosInstance;
|
||||
}
|
||||
|
||||
async get<T>(endpoint: string): Promise<T> {
|
||||
try {
|
||||
const response = await this.axiosInstance.get<T>(`/${endpoint}`);
|
||||
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async post<T>(endpoint: string, data: unknown): Promise<T> {
|
||||
try {
|
||||
const response = await this.axiosInstance.post<T>(`/${endpoint}`, data);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
printNetworkError(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
83
src/tidal/service.ts
Normal file
83
src/tidal/service.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import type { TidalSong, TidalVolume } from "@dpu/shared";
|
||||
import { logWarning } from "@dpu/shared/dist/logger.js";
|
||||
import type { TidalClient } from "./client.js";
|
||||
|
||||
export class TidalService {
|
||||
private client: TidalClient;
|
||||
|
||||
constructor(client: TidalClient) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
async getSongFormatted(): Promise<string> {
|
||||
const song = await this.getSong();
|
||||
if (song) {
|
||||
const status = song.status === "playing" ? "▶️" : "⏸️";
|
||||
return `listening to ${song.title} by ${song.artists}. ${status} ${song.current}/${song.duration}. link: ${song.url}`;
|
||||
} else {
|
||||
return `no song found`;
|
||||
}
|
||||
}
|
||||
|
||||
async getSong(): Promise<TidalSong | null> {
|
||||
try {
|
||||
const response = this.client.get<TidalSong>("current");
|
||||
|
||||
return response;
|
||||
} catch {
|
||||
logWarning("error getting song from tidal");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getVolume(): Promise<TidalVolume | null> {
|
||||
try {
|
||||
const response = await this.client.get<TidalVolume>("volume");
|
||||
|
||||
return response;
|
||||
} catch {
|
||||
logWarning("error getting volume from tidal");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
clamp(value: number): number {
|
||||
return Math.min(Math.max(value, 0), 100);
|
||||
}
|
||||
|
||||
async setVolume(argument: string): Promise<TidalVolume | null> {
|
||||
const value = parseInt(argument, 10);
|
||||
// relative
|
||||
const adjustMatch = argument.match(/^([+-])(\d+)$/);
|
||||
if (adjustMatch) {
|
||||
const volume = await this.getVolume();
|
||||
if (volume) {
|
||||
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);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async setVolumeToTidal(volume: number): Promise<TidalVolume | null> {
|
||||
try {
|
||||
const response = await this.client.post<TidalVolume>("volume", {
|
||||
volume,
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch {
|
||||
logWarning("error setting volume from tidal");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user