2021-11-19 11:52:28 +00:00
|
|
|
const { configs } = require('apache-api');
|
2021-11-16 17:36:39 +00:00
|
|
|
|
|
|
|
const { getStreamsNew } = require('../routes/getStreams')
|
2021-11-19 11:52:28 +00:00
|
|
|
const { getURLandStreams } = require('./getStreamNames')
|
|
|
|
const { allUserCheck } = require('./checker')
|
2021-11-16 17:36:39 +00:00
|
|
|
|
|
|
|
apache_config = 'tv.k-world'
|
|
|
|
|
2021-11-17 10:11:54 +00:00
|
|
|
const { exec } = require("child_process");
|
|
|
|
|
|
|
|
function restart() {
|
|
|
|
exec("sudo service apache2 reload", (error, stdout, stderr) => {
|
|
|
|
if (error) {
|
|
|
|
console.log(`error: ${error.message}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.log(`stderr: ${stderr}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`no issues`);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeid() {
|
|
|
|
var result = '';
|
|
|
|
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
var charactersLength = characters.length;
|
|
|
|
for (var i = 0; i < 5; i++) {
|
|
|
|
result += characters.charAt(Math.floor(Math.random() *
|
|
|
|
charactersLength));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-11-16 17:36:39 +00:00
|
|
|
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')) {
|
|
|
|
var cleanString = item.content.replace(/^(.*?)\//g, "");
|
|
|
|
cleanString = cleanString.split(' ')
|
|
|
|
cleanString[1] = cleanString[1].replace(/"/g, "")
|
|
|
|
return cleanString
|
|
|
|
}
|
|
|
|
}).filter(notUndefined)
|
|
|
|
return final_array
|
|
|
|
}
|
|
|
|
|
2021-11-19 11:52:28 +00:00
|
|
|
async function updateStream(updatedStreams) {
|
|
|
|
config = await get_config()
|
|
|
|
updatedStreams.forEach(async stream => {
|
|
|
|
config.children[0].children.map(async function (item) {
|
|
|
|
if (item.content.includes(stream[1])) {
|
|
|
|
return item.content = `Redirect 302 /${stream[0]} "${stream[2]}"`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
try {
|
|
|
|
await configs.saveConfig(apache_config, config, true, true)
|
|
|
|
console.log(stream[0] + ' url updated')
|
|
|
|
} catch (error) {
|
|
|
|
console.log(stream[0] + ' url failed to update')
|
|
|
|
console.log(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function updateStreamCheck(streamsFromKtvDB, streamsFromConfig) {
|
|
|
|
updatedStreams = []
|
|
|
|
streamsFromConfig.forEach(sfc => {
|
|
|
|
streamsFromKtvDB.forEach(sfktv => {
|
|
|
|
if (sfc[0] == sfktv.stream) {
|
|
|
|
sfc[0]
|
|
|
|
sfktv.stream
|
|
|
|
if (sfc[1] != sfktv.streamURL) {
|
|
|
|
console.log(sfc[0] + ' url changed')
|
|
|
|
updatedStreams.push([sfc[0], sfc[1], sfktv.streamURL])
|
|
|
|
} else {
|
|
|
|
// console.log(sfc[0] + ' not updated')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return updatedStreams
|
|
|
|
}
|
|
|
|
|
2021-11-16 17:36:39 +00:00
|
|
|
|
|
|
|
async function checkExists(fullStreamList, mappedStreams) {
|
|
|
|
newstreams = mappedStreams.map(function (s) {
|
|
|
|
return s[1]
|
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 11:52:28 +00:00
|
|
|
async function syncApache() {
|
|
|
|
// console.log('Updating all user account from ktv')
|
|
|
|
// await allUserCheck()
|
|
|
|
// console.log('Update complete')
|
|
|
|
console.log('Comparing streams from ktv and apache')
|
|
|
|
streams = await getStreamsNew()
|
|
|
|
config = await get_config()
|
|
|
|
streamsFromConfig = await split_streams(config.children[0].children)
|
2021-11-16 17:36:39 +00:00
|
|
|
|
2021-11-19 11:52:28 +00:00
|
|
|
streamsFromKtvDB = await getURLandStreams()
|
|
|
|
updatedStreams = await updateStreamCheck(streamsFromKtvDB, streamsFromConfig)
|
|
|
|
await updateStream(updatedStreams)
|
|
|
|
console.log('Complete')
|
|
|
|
console.log('Adding any missing streams to apache')
|
2021-11-16 17:36:39 +00:00
|
|
|
streams = await getStreamsNew()
|
2021-11-17 10:11:54 +00:00
|
|
|
config = await get_config()
|
|
|
|
streamsFromConfig = await split_streams(config.children[0].children)
|
|
|
|
missingStreamsInApache = await checkExists(streams, streamsFromConfig)
|
|
|
|
resultOfAddStreams = await addNewStreams(config, missingStreamsInApache)
|
2021-11-19 11:52:28 +00:00
|
|
|
console.log('Complete')
|
|
|
|
console.log('Restarting Apache')
|
2021-11-17 10:11:54 +00:00
|
|
|
await restart()
|
2021-11-19 11:52:28 +00:00
|
|
|
console.log('Complete')
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
syncApache
|
2021-11-16 17:36:39 +00:00
|
|
|
}
|
|
|
|
|