mirror of
https://github.com/Mastermindzh/tidal-hifi.git
synced 2025-08-06 04:46:49 +02:00
feat: reworked the api, added duration/current in seconds + shuffle & repeat
This commit is contained in:
@@ -3,18 +3,13 @@ import { app, ipcMain } from "electron";
|
||||
import { globalEvents } from "../constants/globalEvents";
|
||||
import { settings } from "../constants/settings";
|
||||
import { Logger } from "../features/logger";
|
||||
import { convertDurationToSeconds } from "../features/time/parse";
|
||||
import { MediaStatus } from "../models/mediaStatus";
|
||||
import { mediaInfo } from "./mediaInfo";
|
||||
import { settingsStore } from "./settings";
|
||||
|
||||
const clientId = "833617820704440341";
|
||||
|
||||
function timeToSeconds(timeArray: string[]) {
|
||||
const minutes = parseInt(timeArray[0]) * 1;
|
||||
const seconds = minutes * 60 + parseInt(timeArray[1]) * 1;
|
||||
return seconds;
|
||||
}
|
||||
|
||||
export let rpc: Client;
|
||||
|
||||
const observer = () => {
|
||||
@@ -59,7 +54,7 @@ const getActivity = (): Presence => {
|
||||
return { includeTimestamps, detailsPrefix, buttonText };
|
||||
}
|
||||
|
||||
function setPresenceFromMediaInfo(detailsPrefix: any, buttonText: any) {
|
||||
function setPresenceFromMediaInfo(detailsPrefix: string, buttonText: string) {
|
||||
if (mediaInfo.url) {
|
||||
presence.details = `${detailsPrefix}${mediaInfo.title}`;
|
||||
presence.state = mediaInfo.artists ? mediaInfo.artists : "unknown artist(s)";
|
||||
@@ -74,10 +69,10 @@ const getActivity = (): Presence => {
|
||||
}
|
||||
}
|
||||
|
||||
function includeTimeStamps(includeTimestamps: any) {
|
||||
function includeTimeStamps(includeTimestamps: boolean) {
|
||||
if (includeTimestamps) {
|
||||
const currentSeconds = timeToSeconds(mediaInfo.current.split(":"));
|
||||
const durationSeconds = timeToSeconds(mediaInfo.duration.split(":"));
|
||||
const currentSeconds = convertDurationToSeconds(mediaInfo.current);
|
||||
const durationSeconds = convertDurationToSeconds(mediaInfo.duration);
|
||||
const date = new Date();
|
||||
const now = (date.getTime() / 1000) | 0;
|
||||
const remaining = date.setSeconds(date.getSeconds() + (durationSeconds - currentSeconds));
|
||||
|
@@ -1,69 +0,0 @@
|
||||
import { BrowserWindow, dialog } from "electron";
|
||||
import express, { Response } from "express";
|
||||
import fs from "fs";
|
||||
import { settings } from "../constants/settings";
|
||||
import { MediaStatus } from "../models/mediaStatus";
|
||||
import { globalEvents } from "./../constants/globalEvents";
|
||||
import { mediaInfo } from "./mediaInfo";
|
||||
import { settingsStore } from "./settings";
|
||||
|
||||
/**
|
||||
* Function to enable TIDAL Hi-Fi's express api
|
||||
*/
|
||||
|
||||
// expressModule.run = function (mainWindow)
|
||||
export const startExpress = (mainWindow: BrowserWindow) => {
|
||||
/**
|
||||
* Shorthand to handle a fire and forget global event
|
||||
* @param {*} res
|
||||
* @param {*} action
|
||||
*/
|
||||
function handleGlobalEvent(res: Response, action: string) {
|
||||
mainWindow.webContents.send("globalEvent", action);
|
||||
res.sendStatus(200);
|
||||
}
|
||||
|
||||
const expressApp = express();
|
||||
expressApp.get("/", (req, res) => res.send("Hello World!"));
|
||||
expressApp.get("/current", (req, res) => res.json({ ...mediaInfo, artist: mediaInfo.artists }));
|
||||
expressApp.get("/image", (req, res) => {
|
||||
const stream = fs.createReadStream(mediaInfo.icon);
|
||||
stream.on("open", function () {
|
||||
res.set("Content-Type", "image/png");
|
||||
stream.pipe(res);
|
||||
});
|
||||
stream.on("error", function () {
|
||||
res.set("Content-Type", "text/plain");
|
||||
res.status(404).end("Not found");
|
||||
});
|
||||
});
|
||||
|
||||
if (settingsStore.get(settings.playBackControl)) {
|
||||
expressApp.get("/play", (req, res) => handleGlobalEvent(res, globalEvents.play));
|
||||
expressApp.post("/favorite/toggle", (req, res) =>
|
||||
handleGlobalEvent(res, globalEvents.toggleFavorite)
|
||||
);
|
||||
expressApp.get("/pause", (req, res) => handleGlobalEvent(res, globalEvents.pause));
|
||||
expressApp.get("/next", (req, res) => handleGlobalEvent(res, globalEvents.next));
|
||||
expressApp.get("/previous", (req, res) => handleGlobalEvent(res, globalEvents.previous));
|
||||
expressApp.get("/playpause", (req, res) => {
|
||||
if (mediaInfo.status === MediaStatus.playing) {
|
||||
handleGlobalEvent(res, globalEvents.pause);
|
||||
} else {
|
||||
handleGlobalEvent(res, globalEvents.play);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const port = settingsStore.get<string, number>(settings.apiSettings.port);
|
||||
|
||||
const expressInstance = expressApp.listen(port, "127.0.0.1");
|
||||
expressInstance.on("error", function (e: { code: string }) {
|
||||
let message = e.code;
|
||||
if (e.code === "EADDRINUSE") {
|
||||
message = `Port ${port} in use.`;
|
||||
}
|
||||
|
||||
dialog.showErrorBox("Api failed to start.", message);
|
||||
});
|
||||
};
|
@@ -9,7 +9,9 @@ export const mediaInfo = {
|
||||
status: MediaStatus.paused as string,
|
||||
url: "",
|
||||
current: "",
|
||||
currentInSeconds: 0,
|
||||
duration: "",
|
||||
durationInSeconds: 0,
|
||||
image: "tidal-hifi-icon",
|
||||
favorite: false,
|
||||
};
|
||||
@@ -22,7 +24,9 @@ export const updateMediaInfo = (arg: MediaInfo) => {
|
||||
mediaInfo.url = toUniversalUrl(propOrDefault(arg.url));
|
||||
mediaInfo.status = propOrDefault(arg.status);
|
||||
mediaInfo.current = propOrDefault(arg.current);
|
||||
mediaInfo.currentInSeconds = arg.currentInSeconds ?? 0;
|
||||
mediaInfo.duration = propOrDefault(arg.duration);
|
||||
mediaInfo.durationInSeconds = arg.durationInSeconds ?? 0;
|
||||
mediaInfo.image = propOrDefault(arg.image);
|
||||
mediaInfo.favorite = arg.favorite;
|
||||
};
|
||||
|
Reference in New Issue
Block a user