2019-07-20 09:33:16 +02:00
|
|
|
const util = require('../helpers/util')
|
2018-12-22 20:07:47 +01:00
|
|
|
const axios = require('axios')
|
2019-08-08 13:11:11 +02:00
|
|
|
const https = require('https')
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-08-08 19:45:13 +02:00
|
|
|
const verbose = process.env.npm_config_debug || false
|
2018-12-22 20:07:47 +01:00
|
|
|
const errorLog = 'error.log'
|
2019-07-20 09:33:16 +02:00
|
|
|
const config = {
|
2019-08-18 20:38:29 +02:00
|
|
|
timeout: 5000,
|
2019-07-20 09:33:16 +02:00
|
|
|
delay: 200
|
|
|
|
}
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-07-20 09:33:16 +02:00
|
|
|
let stats = {
|
|
|
|
tests: 0,
|
|
|
|
channels: 0,
|
|
|
|
failures: 0
|
|
|
|
}
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-08-08 14:56:23 +02:00
|
|
|
const instance = axios.create({
|
2019-08-08 13:11:11 +02:00
|
|
|
timeout: config.timeout,
|
|
|
|
httpsAgent: new https.Agent({
|
|
|
|
rejectUnauthorized: false
|
2019-08-19 02:31:55 +02:00
|
|
|
}),
|
|
|
|
validateStatus: function (status) {
|
|
|
|
return status >= 200 && status < 404
|
|
|
|
}
|
2019-08-08 13:11:11 +02:00
|
|
|
})
|
2018-12-22 20:07:47 +01:00
|
|
|
|
2019-07-20 09:33:16 +02:00
|
|
|
async function test() {
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-07-20 09:33:16 +02:00
|
|
|
stats.tests++
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-08-07 22:59:44 +02:00
|
|
|
const playlist = util.parsePlaylist('index.m3u')
|
|
|
|
|
|
|
|
const countries = playlist.items
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2018-12-22 20:07:47 +01:00
|
|
|
for(let country of countries) {
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-08-12 14:34:42 +02:00
|
|
|
if (util.skipPlaylist(country.url)) {
|
|
|
|
continue
|
2019-05-09 19:04:18 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 22:59:44 +02:00
|
|
|
console.log(`Checking '${country.url}'...`)
|
|
|
|
|
|
|
|
const playlist = util.parsePlaylist(country.url)
|
2018-12-22 20:07:47 +01:00
|
|
|
|
2019-08-07 22:59:44 +02:00
|
|
|
for(let item of playlist.items) {
|
2018-12-22 20:07:47 +01:00
|
|
|
|
2019-03-19 23:26:21 +01:00
|
|
|
await new Promise(resolve => {
|
2019-07-20 09:33:16 +02:00
|
|
|
setTimeout(resolve, config.delay)
|
2019-03-19 23:26:21 +01:00
|
|
|
})
|
|
|
|
|
2019-07-20 09:33:16 +02:00
|
|
|
stats.channels++
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2018-11-16 04:46:05 +01:00
|
|
|
try {
|
2018-12-22 20:07:47 +01:00
|
|
|
|
2019-08-08 14:56:23 +02:00
|
|
|
if(verbose) {
|
|
|
|
console.log(`Checking '${item.url}'...`)
|
|
|
|
}
|
|
|
|
|
|
|
|
await instance.get(item.url)
|
2018-12-22 20:07:47 +01:00
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
2019-08-18 20:38:29 +02:00
|
|
|
if(err.response || (err.request && ['ENOTFOUND'].indexOf(err.code) > -1)) {
|
|
|
|
stats.failures++
|
2019-08-08 13:11:11 +02:00
|
|
|
|
2019-08-18 20:38:29 +02:00
|
|
|
writeToLog(country.url, err.message, item.url)
|
|
|
|
}
|
2018-12-22 20:07:47 +01:00
|
|
|
|
2018-11-16 00:41:15 +01:00
|
|
|
}
|
2018-12-22 20:07:47 +01:00
|
|
|
|
2018-11-16 00:41:15 +01:00
|
|
|
}
|
2018-12-22 20:07:47 +01:00
|
|
|
}
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-07-20 09:33:16 +02:00
|
|
|
if(stats.failures === 0) {
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-07-20 09:33:16 +02:00
|
|
|
console.log(`OK (${stats.tests} tests, ${stats.channels} channels)`)
|
2018-12-22 20:07:47 +01:00
|
|
|
|
|
|
|
} else {
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2019-07-20 09:33:16 +02:00
|
|
|
console.log(`FAILURES! (${stats.tests} tests, ${stats.channels} channels, ${stats.failures} failures)`)
|
2018-11-16 00:41:15 +01:00
|
|
|
|
2018-12-22 20:07:47 +01:00
|
|
|
}
|
2018-11-16 00:41:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-26 20:41:17 +01:00
|
|
|
console.log('Test is running...')
|
|
|
|
|
2019-07-20 09:33:16 +02:00
|
|
|
test()
|
2019-08-07 22:59:44 +02:00
|
|
|
|
2019-08-08 02:32:27 +02:00
|
|
|
function writeToLog(country, msg, url) {
|
2019-08-07 22:59:44 +02:00
|
|
|
var now = new Date()
|
2019-08-08 02:32:27 +02:00
|
|
|
var line = `${country}: ${msg} '${url}'`
|
2019-08-07 22:59:44 +02:00
|
|
|
util.appendToFile(errorLog, now.toISOString() + ' ' + line + '\n')
|
|
|
|
console.log(`Error: ${msg} '${url}'`)
|
|
|
|
}
|