Skip to content

Commit

Permalink
Add repeat mode and seek time API
Browse files Browse the repository at this point in the history
  • Loading branch information
sent44 committed Nov 28, 2024
1 parent 4ab8829 commit a1039ba
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/plugins/api-server/backend/routes/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ const routes = {
},
},
}),
repeatMode: createRoute({
method: 'get',
path: `/api/${API_VERSION}/repeat-mode`,
summary: 'get current repeat mode',
description: 'Get the current repeat mode (NONE, ALL, ONE)',
responses: {
200: {
description: 'Success',
content: {
'application/json': {
schema: z.object({
mode: z.string().nullable().openapi({example: 'ONE'}),
}),
},
},
},
},
}),
switchRepeat: createRoute({
method: 'post',
path: `/api/${API_VERSION}/switch-repeat`,
Expand Down Expand Up @@ -275,6 +293,25 @@ const routes = {
},
},
}),
seekTime: createRoute({
method: 'get',
path: `/api/${API_VERSION}/seek-time`,
summary: 'get current play time and video duration',
description: 'Get current play time and video duration in seconds',
responses: {
200: {
description: 'Success',
content: {
'application/json': {
schema: z.object({
current: z.number().nullable().openapi({example: 3}),
duration: z.number().nullable().openapi({example: 233}),
}),
},
},
},
},
}),
songInfo: createRoute({
method: 'get',
path: `/api/${API_VERSION}/song-info`,
Expand Down Expand Up @@ -365,6 +402,23 @@ export const register = (
ctx.status(204);
return ctx.body(null);
});
app.openapi(routes.repeatMode, async (ctx) => {
const modeResponsePromise = new Promise<string|null>((resolve) => {
ipcMain.once(
'ytmd:repeat-mode-response',
(_, repeatmode: string | null) => {
return resolve(repeatmode);
},
);

controller.requestRepeatModeInformation();
});

const repeatmode = await modeResponsePromise;

ctx.status(200);
return ctx.json({ mode: repeatmode });
});
app.openapi(routes.switchRepeat, (ctx) => {
const { iteration } = ctx.req.valid('json');
controller.switchRepeat(iteration);
Expand Down Expand Up @@ -429,6 +483,23 @@ export const register = (
ctx.status(200);
return ctx.json(info);
});
app.openapi(routes.seekTime, async (ctx) => {
const timeResponsePromise = new Promise<object|null>((resolve) => {
ipcMain.once(
'ytmd:seek-time-response',
(_, time) => {
return resolve(time);
},
);

controller.requestSeekTimeInformation();
});

const time = await timeResponsePromise;

ctx.status(200);
return ctx.json(time);
});
app.openapi(routes.songInfo, (ctx) => {
const info = songInfoGetter();

Expand Down
6 changes: 6 additions & 0 deletions src/providers/song-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export default (win: BrowserWindow) => {
}
},
shuffle: () => win.webContents.send('ytmd:shuffle'),
requestRepeatModeInformation: () => {
win.webContents.send('ytmd:get-repeat-mode');
},
switchRepeat: (n: ArgsType<number> = 1) => {
const repeat = parseNumberFromArgsType(n);
if (repeat !== null) {
Expand Down Expand Up @@ -80,5 +83,8 @@ export default (win: BrowserWindow) => {
keyCode: '/',
});
},
requestSeekTimeInformation: () => {
win.webContents.send('ytmd:get-seek-time');
},
};
};
19 changes: 19 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ async function onApiLoaded() {
?.updateLikeStatus(status);
},
);
window.ipcRenderer.on('ytmd:get-repeat-mode', () => {
const repeatMode =
document
.querySelector<HTMLElement>('ytmusic-player-bar')
?.attributes.getNamedItem('repeat-mode') ?? null;
window.ipcRenderer.send('ytmd:repeat-mode-response', repeatMode?.value);
});
window.ipcRenderer.on('ytmd:switch-repeat', (_, repeat = 1) => {
for (let i = 0; i < repeat; i++) {
document
Expand Down Expand Up @@ -150,6 +157,18 @@ async function onApiLoaded() {
?.onVolumeTap();
});

window.ipcRenderer.on('ytmd:get-seek-time', () => {
const progressBar =
document
.querySelector<HTMLElement>('#progress-bar')
const currentTime = progressBar?.attributes.getNamedItem('value');
const duration = progressBar?.attributes.getNamedItem('aria-valuemax');
window.ipcRenderer.send('ytmd:seek-time-response', {
current: currentTime != null ? parseInt(currentTime.value): null,
duration: duration != null ? parseInt(duration.value): null
});
});

window.ipcRenderer.on('ytmd:get-queue', () => {
const queue = document.querySelector<QueueElement>('#queue');
window.ipcRenderer.send('ytmd:get-queue-response', {
Expand Down

0 comments on commit a1039ba

Please sign in to comment.