From 418566f6317960e83edff117a036b54df2eac07b Mon Sep 17 00:00:00 2001 From: 4gray Date: Wed, 13 Jan 2021 21:36:03 +0100 Subject: [PATCH] feat: add epg worker with new ipc commands --- .gitignore | 1 + epg-worker.html | 12 +++++ epg-worker.ts | 99 ++++++++++++++++++++++++++++++++++ src/app/shared/ipc-commands.ts | 5 ++ tsconfig.serve.json | 1 + 5 files changed, 118 insertions(+) create mode 100644 epg-worker.html create mode 100644 epg-worker.ts create mode 100644 src/app/shared/ipc-commands.ts diff --git a/.gitignore b/.gitignore index 9b4eb8df1..e1d550c36 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ /app-builds /release api.js +epg-worker.js main.js src/**/*.js !src/karma.conf.js diff --git a/epg-worker.html b/epg-worker.html new file mode 100644 index 000000000..e6fa93f7e --- /dev/null +++ b/epg-worker.html @@ -0,0 +1,12 @@ + + + + + Hidden EPG worker + + + + + diff --git a/epg-worker.ts b/epg-worker.ts new file mode 100644 index 000000000..cb4c25c5c --- /dev/null +++ b/epg-worker.ts @@ -0,0 +1,99 @@ +const electron = require('electron'); +const ipcRenderer = electron.ipcRenderer; +const zlib = require('zlib'); +const parser = require('epg-parser'); +const axios = require('axios'); +import { EpgChannel } from './src/app/player/models/epg-channel.model'; +import { EpgProgram } from './src/app/player/models/epg-program.model'; +import { + EPG_ERROR, + EPG_FETCH, + EPG_FETCH_DONE, + EPG_GET_PROGRAM, + EPG_GET_PROGRAM_DONE, +} from './src/app/shared/ipc-commands'; + +// EPG data store +let EPG_DATA: { channels: EpgChannel[]; programs: EpgProgram[] }; +const loggerLabel = '[EPG Worker]'; + +/** + * Fetches the epg data from the given url + * @param epgUrl url of the epg file + */ +const fetchEpgDataFromUrl = (epgUrl: string) => { + try { + let axiosConfig = {}; + if (epgUrl.endsWith('.gz')) { + axiosConfig = { + responseType: 'arraybuffer', + }; + } + axios + .get(epgUrl.trim(), axiosConfig) + .then((response) => { + console.log(loggerLabel, 'url content was fetched...'); + const { data } = response; + if (epgUrl.endsWith('.gz')) { + console.log(loggerLabel, 'start unzipping...'); + zlib.gunzip(data, (_err, output) => { + parseAndSetEpg(output); + }); + } else { + parseAndSetEpg(data); + } + }) + .catch((err) => { + console.log(loggerLabel, err); + ipcRenderer.send(EPG_ERROR); + }); + } catch (error) { + console.log(loggerLabel, error); + ipcRenderer.send(EPG_ERROR); + } +}; + +/** + * Parses and sets the epg data + * @param xmlString xml file content from the fetched url as string + */ +const parseAndSetEpg = (xmlString) => { + console.log(loggerLabel, 'start parsing...'); + EPG_DATA = parser.parse(xmlString.toString()); + ipcRenderer.send(EPG_FETCH_DONE); + console.log(loggerLabel, 'done, parsing was finished...'); +}; + +// fetches epg data from the provided URL +ipcRenderer.on(EPG_FETCH, (event, arg) => { + console.log(loggerLabel, 'epg fetch command was triggered'); + fetchEpgDataFromUrl(arg); +}); + +// returns the epg data for the provided channel name and date +ipcRenderer.on(EPG_GET_PROGRAM, (event, args) => { + if (!EPG_DATA || !EPG_DATA.channels) return; + const foundChannel = EPG_DATA?.channels?.find((epgChannel) => { + if ( + epgChannel.name.find((nameObj) => { + if (nameObj.value.trim() === args.channelName.trim()) + return nameObj; + }) + ) { + return epgChannel; + } + }); + + if (foundChannel) { + const programs = EPG_DATA?.programs?.filter( + (ch) => ch.channel === foundChannel.id + ); + ipcRenderer.send(EPG_GET_PROGRAM_DONE, { + payload: { channel: foundChannel, items: programs }, + }); + } else { + ipcRenderer.send(EPG_GET_PROGRAM_DONE, { + payload: { channel: {}, items: [] }, + }); + } +}); diff --git a/src/app/shared/ipc-commands.ts b/src/app/shared/ipc-commands.ts new file mode 100644 index 000000000..69d15000f --- /dev/null +++ b/src/app/shared/ipc-commands.ts @@ -0,0 +1,5 @@ +export const EPG_FETCH = 'EPG:FETCH'; +export const EPG_FETCH_DONE = 'EPG:FETCH_DONE'; +export const EPG_ERROR = 'EPG:ERROR'; +export const EPG_GET_PROGRAM = 'EPG:GET_PROGRAM'; +export const EPG_GET_PROGRAM_DONE = 'EPG:GET_PROGRAM_DONE'; diff --git a/tsconfig.serve.json b/tsconfig.serve.json index ed176e2cc..4e66eb9d5 100644 --- a/tsconfig.serve.json +++ b/tsconfig.serve.json @@ -18,6 +18,7 @@ }, "files": [ "api.ts", + "epg-worker.ts", "main.ts" ], "exclude": [