mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-16 12:42:06 +00:00
feat(api-server): Add repeat mode and seek time API (#2630)
Co-authored-by: sent44 <sent44@root533.premium-rootserver.net> Co-authored-by: JellyBrick <shlee1503@naver.com>
This commit is contained in:
committed by
GitHub
parent
9163b6f04b
commit
109e9f8166
@ -13,6 +13,7 @@ import { registerAuth, registerControl } from './routes';
|
|||||||
import { type APIServerConfig, AuthStrategy } from '../config';
|
import { type APIServerConfig, AuthStrategy } from '../config';
|
||||||
|
|
||||||
import type { BackendType } from './types';
|
import type { BackendType } from './types';
|
||||||
|
import type { RepeatMode } from '@/types/datahost-get-state';
|
||||||
|
|
||||||
export const backend = createBackend<BackendType, APIServerConfig>({
|
export const backend = createBackend<BackendType, APIServerConfig>({
|
||||||
async start(ctx) {
|
async start(ctx) {
|
||||||
@ -23,7 +24,14 @@ export const backend = createBackend<BackendType, APIServerConfig>({
|
|||||||
this.songInfo = songInfo;
|
this.songInfo = songInfo;
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.ipc.on('ytmd:player-api-loaded', () => ctx.ipc.send('ytmd:setup-time-changed-listener'));
|
ctx.ipc.on('ytmd:player-api-loaded', () =>
|
||||||
|
ctx.ipc.send('ytmd:setup-time-changed-listener'),
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.ipc.on(
|
||||||
|
'ytmd:repeat-changed',
|
||||||
|
(mode: RepeatMode) => (this.currentRepeatMode = mode),
|
||||||
|
);
|
||||||
|
|
||||||
this.run(config.hostname, config.port);
|
this.run(config.hostname, config.port);
|
||||||
},
|
},
|
||||||
@ -75,7 +83,12 @@ export const backend = createBackend<BackendType, APIServerConfig>({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// routes
|
// routes
|
||||||
registerControl(this.app, ctx, () => this.songInfo);
|
registerControl(
|
||||||
|
this.app,
|
||||||
|
ctx,
|
||||||
|
() => this.songInfo,
|
||||||
|
() => this.currentRepeatMode,
|
||||||
|
);
|
||||||
registerAuth(this.app, ctx);
|
registerAuth(this.app, ctx);
|
||||||
|
|
||||||
// swagger
|
// swagger
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import {
|
|||||||
SetFullscreenSchema,
|
SetFullscreenSchema,
|
||||||
} from '../scheme';
|
} from '../scheme';
|
||||||
|
|
||||||
|
import type { RepeatMode } from '@/types/datahost-get-state';
|
||||||
import type { SongInfo } from '@/providers/song-info';
|
import type { SongInfo } from '@/providers/song-info';
|
||||||
import type { BackendContext } from '@/types/contexts';
|
import type { BackendContext } from '@/types/contexts';
|
||||||
import type { APIServerConfig } from '../../config';
|
import type { APIServerConfig } from '../../config';
|
||||||
@ -160,6 +161,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.enum(['ONE', 'NONE', 'ALL']).nullable(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
switchRepeat: createRoute({
|
switchRepeat: createRoute({
|
||||||
method: 'post',
|
method: 'post',
|
||||||
path: `/api/${API_VERSION}/switch-repeat`,
|
path: `/api/${API_VERSION}/switch-repeat`,
|
||||||
@ -275,6 +294,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({
|
songInfo: createRoute({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
path: `/api/${API_VERSION}/song-info`,
|
path: `/api/${API_VERSION}/song-info`,
|
||||||
@ -300,6 +338,7 @@ export const register = (
|
|||||||
app: HonoApp,
|
app: HonoApp,
|
||||||
{ window }: BackendContext<APIServerConfig>,
|
{ window }: BackendContext<APIServerConfig>,
|
||||||
songInfoGetter: () => SongInfo | undefined,
|
songInfoGetter: () => SongInfo | undefined,
|
||||||
|
repeatModeGetter: () => RepeatMode | undefined,
|
||||||
) => {
|
) => {
|
||||||
const controller = getSongControls(window);
|
const controller = getSongControls(window);
|
||||||
|
|
||||||
@ -365,6 +404,11 @@ export const register = (
|
|||||||
ctx.status(204);
|
ctx.status(204);
|
||||||
return ctx.body(null);
|
return ctx.body(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.openapi(routes.repeatMode, (ctx) => {
|
||||||
|
ctx.status(200);
|
||||||
|
return ctx.json({ mode: repeatModeGetter() ?? null });
|
||||||
|
});
|
||||||
app.openapi(routes.switchRepeat, (ctx) => {
|
app.openapi(routes.switchRepeat, (ctx) => {
|
||||||
const { iteration } = ctx.req.valid('json');
|
const { iteration } = ctx.req.valid('json');
|
||||||
controller.switchRepeat(iteration);
|
controller.switchRepeat(iteration);
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { serve } from '@hono/node-server';
|
|||||||
|
|
||||||
import type { BackendContext } from '@/types/contexts';
|
import type { BackendContext } from '@/types/contexts';
|
||||||
import type { SongInfo } from '@/providers/song-info';
|
import type { SongInfo } from '@/providers/song-info';
|
||||||
|
import type { RepeatMode } from '@/types/datahost-get-state';
|
||||||
import type { APIServerConfig } from '../config';
|
import type { APIServerConfig } from '../config';
|
||||||
|
|
||||||
export type HonoApp = Hono;
|
export type HonoApp = Hono;
|
||||||
@ -11,6 +12,7 @@ export type BackendType = {
|
|||||||
server?: ReturnType<typeof serve>;
|
server?: ReturnType<typeof serve>;
|
||||||
oldConfig?: APIServerConfig;
|
oldConfig?: APIServerConfig;
|
||||||
songInfo?: SongInfo;
|
songInfo?: SongInfo;
|
||||||
|
currentRepeatMode?: RepeatMode;
|
||||||
|
|
||||||
init: (ctx: BackendContext<APIServerConfig>) => Promise<void>;
|
init: (ctx: BackendContext<APIServerConfig>) => Promise<void>;
|
||||||
run: (hostname: string, port: number) => void;
|
run: (hostname: string, port: number) => void;
|
||||||
|
|||||||
Reference in New Issue
Block a user