From 9e24b1f7368c82d1b2a2d8227c4d9993d505293d Mon Sep 17 00:00:00 2001 From: 4gray <4gray@users.noreply.github.com> Date: Wed, 9 Aug 2023 22:53:48 +0200 Subject: [PATCH] feat: stalker portal support in the main process --- electron/api.ts | 52 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/electron/api.ts b/electron/api.ts index 4002108f8..c70bc57d9 100644 --- a/electron/api.ts +++ b/electron/api.ts @@ -32,6 +32,8 @@ import { PLAYLIST_UPDATE_RESPONSE, SET_MPV_PLAYER_PATH, SET_VLC_PLAYER_PATH, + STALKER_REQUEST, + STALKER_RESPONSE, XTREAM_REQUEST, XTREAM_RESPONSE, } from '../shared/ipc-commands'; @@ -318,17 +320,52 @@ export class Api { this.workerWindow.webContents.send(EPG_FORCE_FETCH, arg) ); - ipcMain.on( - XTREAM_REQUEST, - (event, arg: { url: string; params: Record }) => { - const xtreamApiPath = '/player_api.php'; + ipcMain + .on( + XTREAM_REQUEST, + ( + event, + arg: { url: string; params: Record } + ) => { + const xtreamApiPath = '/player_api.php'; + axios + .get(arg.url + xtreamApiPath, { + params: arg.params ?? {}, + }) + .then((result) => { + event.sender.send(XTREAM_RESPONSE, { + payload: result.data, + action: arg.params.action, + }); + }) + .catch((err) => { + event.sender.send(ERROR, { + message: + err.response?.statusText ?? + 'Error: not found', + status: err.response?.status ?? 404, + }); + }); + } + ) + .on(STALKER_REQUEST, (event, arg: any) => { axios - .get(arg.url + xtreamApiPath, { + .get(arg.url, { params: arg.params ?? {}, + headers: { + Cookie: `mac=${arg.macAddress as string}`, + ...(arg.params.token + ? { + Authorization: `Bearer ${ + arg.params.token as string + }`, + } + : {}), + }, }) .then((result) => { - event.sender.send(XTREAM_RESPONSE, { + event.sender.send(STALKER_RESPONSE, { payload: result.data, action: arg.params.action, }); @@ -340,8 +377,7 @@ export class Api { status: err.response?.status ?? 404, }); }); - } - ); + }); } createMpvInstance() {