From a315c3e6b7fa4515fcfcb82f354a05dc1f9a6135 Mon Sep 17 00:00:00 2001 From: Karl Date: Tue, 16 Nov 2021 17:36:39 +0000 Subject: [PATCH] working apache logic --- lib/apache_functions.js | 95 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 lib/apache_functions.js diff --git a/lib/apache_functions.js b/lib/apache_functions.js new file mode 100644 index 0000000..08b9d2f --- /dev/null +++ b/lib/apache_functions.js @@ -0,0 +1,95 @@ +const { actions, configs } = require('apache-api'); + +const { getStreamsNew } = require('../routes/getStreams') + +apache_config = 'tv.k-world' + +async function get_config() { + return await configs.readConfig(apache_config, true, true) +} + +async function split_streams(siteList) { + const notUndefined = anyValue => typeof anyValue !== 'undefined' + + final_array = siteList.map(function (item) { + if (item.content.includes('Redirect')) { + // console.log(item) + var cleanString = item.content.replace(/^(.*?)\//g, ""); + cleanString = cleanString.split(' ') + cleanString[1] = cleanString[1].replace(/"/g, "") + // console.log(cleanString) + return cleanString + } + }).filter(notUndefined) + return final_array +} + +// async function updateStream(sub, oldUrl, newUrl) { +// config = await get_config() +// config.children[0].children.map(function (item) { +// // string = `Redirect 302 /${sub} "${oldUrl}"` +// if (item.content.includes(oldUrl)) { +// console.log('found') +// return item.content = `Redirect 302 /${sub} "${newUrl}"` +// } +// }) +// try { +// await configs.saveConfig(apache_config, config, true, true) +// } catch (error) { +// console.log(error) +// } +// await actions.restartApache() +// } + +async function checkExists(fullStreamList, mappedStreams) { + newstreams = mappedStreams.map(function (s) { + return s[1] + }) + // console.log(newstreams) + myArray = fullStreamList.filter(function (el) { + return newstreams.indexOf(el) < 0; + }); + if (myArray.length == 0) { + console.log('No new streams') + } else { + console.log('New streams ' + myArray) + } + return myArray +} + +function makeid() { + var result = ''; + var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + var charactersLength = characters.length; + for (var i = 0; i < 5; i++) { + result += characters.charAt(Math.floor(Math.random() * + charactersLength)); + } + return result; +} + + +async function addNewStreams(config, streamList) { + streamList.forEach(stream => { + config.children[0].children.push({ "tagName": '$rule', "content": `Redirect 302 /${makeid()} "${stream}"` }) + }) + try { + await configs.saveConfig(apache_config, config, true, true) + } catch (error) { + console.log(error) + } + await actions.restartApache() +} + +// update('DIAZ', 'http://google.com', 'https://watchtower.ddns.net:8443') + + +async function main() { + streams = await getStreamsNew() + a = await get_config() + b = await split_streams(a.children[0].children) + c = await checkExists(streams, b) + d = await addNewStreams(a, c) +} + +main() \ No newline at end of file