From 3d27fd729d15c34155290262f9ef538142c74074 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Tue, 12 Oct 2021 00:34:46 +0300 Subject: [PATCH] Create parser.js --- scripts/parser.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 scripts/parser.js diff --git a/scripts/parser.js b/scripts/parser.js new file mode 100644 index 00000000..ed93fe08 --- /dev/null +++ b/scripts/parser.js @@ -0,0 +1,30 @@ +const convert = require('xml-js') + +const parser = {} + +parser.parseChannels = function (xml) { + const result = convert.xml2js(xml) + const siteTag = result.elements.find(el => el.name === 'site') + const channelsTags = siteTag.elements.filter(el => el.name === 'channels') + + let output = [] + + channelsTags.forEach(channelsTag => { + const channels = channelsTag.elements + .filter(el => el.name === 'channel') + .map(el => { + const channel = el.attributes + if (!el.elements) throw new Error(`Channel '${channel.xmltv_id}' has no valid name`) + channel.name = el.elements.find(el => el.type === 'text').text + channel.country = channelsTag.attributes.country + channel.site = siteTag.attributes.site + + return channel + }) + output = output.concat(channels) + }) + + return output +} + +module.exports = parser