Skip to content

Commit

Permalink
Типы для музыки
Browse files Browse the repository at this point in the history
  • Loading branch information
kraineff committed Feb 8, 2025
1 parent e5e8667 commit 4954dab
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 13 deletions.
12 changes: 6 additions & 6 deletions drivers/Device.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Device extends Homey.Device {

#lastAliceState = AliceState.Idle;
#lastTrackId = "";
#lastTrackAlbumId = "";
#lastTrackAlbumId?: number;
#lastTrackImage = "https://";
#lastTrackLyrics: Array<string> = [];
#lastTrackLyricsTimeout?: NodeJS.Timeout;
Expand Down Expand Up @@ -191,18 +191,18 @@ export default class Device extends Homey.Device {
if (trackId !== this.#lastTrackId) {
const track = await this.#yandex.api.music.getTrack(trackId).catch(() => undefined);
this.#lastTrackId = track?.id || trackId;
this.#lastTrackAlbumId = track?.albums?.[0]?.id || "";
this.#lastTrackAlbumId = track?.albums?.[0]?.id || undefined;
this.#lastTrackLyrics = [];

const [likes, dislikes] = await Promise.all([
this.#yandex.api.music.getLikes(this.#userId).catch(() => [] as any[]),
this.#yandex.api.music.getDislikes(this.#userId).catch(() => [] as any[]),
this.#yandex.api.music.getLikes(this.#userId),
this.#yandex.api.music.getDislikes(this.#userId),
this.updateTrackCover(state, track),
this.updateTrackLyrics(state, track)
]);

capabilities.speaker_track = track?.title || state.playerState?.title || "";
capabilities.speaker_artist = track?.artists?.map((a: any) => a.name)?.join(", ") || state.playerState?.subtitle || "";
capabilities.speaker_artist = track?.artists?.map((a) => a.name)?.join(", ") || state.playerState?.subtitle || "";
capabilities.speaker_album = track?.albums?.[0]?.title || state.playerState?.playlistId || "";
capabilities.media_like = !!likes.find(like => like.id === track?.id);
capabilities.media_dislike = !!dislikes.find(dislike => dislike.id === track?.id);
Expand Down Expand Up @@ -280,7 +280,7 @@ export default class Device extends Homey.Device {

if (this.#lastAliceState !== aliceState) {
this.#lastAliceState = aliceState;

const aliceUrl = "https://i.imgur.com/vTa3rif.png";
this.#image.setUrl(aliceState !== AliceState.Idle ? aliceUrl : this.#lastTrackImage);
await this.#image.update();
Expand Down
15 changes: 8 additions & 7 deletions library/api/services/music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createHmac } from "node:crypto";
import qs from "node:querystring";
import type { AxiosInstance } from "axios";
import type { YandexStorage } from "../../storage.js";
import type * as Types from "../../typings/index.js";
import { createInstance } from "../utils.js";
import type { YandexPassportAPI } from "./passport.js";

Expand Down Expand Up @@ -36,7 +37,7 @@ export class YandexMusicAPI {
async getTracks(trackIds: string[]) {
return await this.#client
.post("/tracks", qs.stringify({ "track-ids": trackIds }))
.then((res) => res.data.result);
.then((res) => res.data.result as Types.MusicTrack[]);
}

async getTrack(trackId: string) {
Expand All @@ -60,10 +61,10 @@ export class YandexMusicAPI {
async getLikes(userId: string) {
return await this.#client
.get(`/users/${userId}/likes/tracks`)
.then((res) => res.data.result.library.tracks as any[]);
.then((res) => res.data.result.library.tracks as Types.MusicLike[]);
}

async addLike(userId: string, trackId: string, albumId: string) {
async addLike(userId: string, trackId: string | number, albumId: string | number) {
const params = new URLSearchParams();
params.append("track-ids", `${trackId}:${albumId}`);

Expand All @@ -72,7 +73,7 @@ export class YandexMusicAPI {
});
}

async removeLike(userId: string, trackId: string) {
async removeLike(userId: string, trackId: string | number) {
const params = new URLSearchParams();
params.append("track-ids", `${trackId}`);

Expand All @@ -82,10 +83,10 @@ export class YandexMusicAPI {
async getDislikes(userId: string) {
return await this.#client
.get(`/users/${userId}/dislikes/tracks`)
.then((res) => res.data.result.library.tracks as any[]);
.then((res) => res.data.result.library.tracks as Types.MusicLike[]);
}

async addDislike(userId: string, trackId: string, albumId: string) {
async addDislike(userId: string, trackId: string | number, albumId: string | number) {
const params = new URLSearchParams();
params.append("track-ids", `${trackId}:${albumId}`);

Expand All @@ -94,7 +95,7 @@ export class YandexMusicAPI {
});
}

async removeDislike(userId: string, trackId: string) {
async removeDislike(userId: string, trackId: string | number) {
const params = new URLSearchParams();
params.append("track-ids", `${trackId}`);

Expand Down
1 change: 1 addition & 0 deletions library/typings/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./alice";
export * from "./home";
export * from "./music";
export * from "./passport";
110 changes: 110 additions & 0 deletions library/typings/music/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
export type MusicTrack = {
id: string;
realId: string;
title: string;
contentWarning?: string;
major: {
id: number;
name: string;
};
available: boolean;
availableForPremiumUsers: boolean;
availableFullWithoutPermission: boolean;
availableForOptions: string[];
disclaimers: string[];
storageDir: string;
durationMs: number;
fileSize: number;
r128: {
i: number;
tp: number;
};
fade: {
inStart: number;
inStop: number;
outStart: number;
outStop: number;
};
previewDurationMs: number;
artists: MusicArtist[];
albums: MusicAlbum[];
coverUri: string;
derivedColors: {
average: string;
waveText: string;
miniPlayer: string;
accent: string;
};
ogImage: string;
lyricsAvailable: boolean;
type: string;
rememberPosition: boolean;
shortDescription?: string;
isSuitableForChildren?: boolean;
pubDate?: string;
trackSharingFlag: string;
lyricsInfo: {
hasAvailableSyncLyrics: boolean;
hasAvailableTextLyrics: boolean;
};
trackSource: string;
specialAudioResources?: string[];
};

export type MusicArtist = {
id: number;
name: string;
various: boolean;
composer: boolean;
available: boolean;
cover: {
type: string;
uri: string;
prefix: string;
};
genres: unknown[];
disclaimers: unknown[];
};

export type MusicAlbum = {
id: number;
title: string;
type: string;
metaType: string;
year?: number;
releaseDate?: string;
contentWarning?: string;
coverUri: string;
ogImage: string;
genre?: string;
trackCount: number;
likesCount: number;
childContent?: boolean;
recent: boolean;
veryImportant: boolean;
artists: MusicArtist[];
labels: Array<{
id: number;
name: string;
}>;
available: boolean;
availableForPremiumUsers: boolean;
availableForOptions: string[];
availableForMobile: boolean;
availablePartially: boolean;
bests: number[];
shortDescription?: string;
description?: string;
disclaimers: string[];
listeningFinished: boolean;
trackPosition: {
volume: number;
index: number;
};
};

export type MusicLike = {
id: string;
albumId: string;
timestamp: string;
};

0 comments on commit 4954dab

Please sign in to comment.