iptv/test/index.js

92 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-04-23 20:49:52 +02:00
const parsers = require('playlist-parser')
const M3U = parsers.M3U
const fs = require("fs")
2018-12-22 20:07:47 +01:00
const axios = require('axios')
const path = require('path')
2018-11-16 00:41:15 +01:00
2018-12-22 20:07:47 +01:00
const errorLog = 'error.log'
const timeout = 60000
const delay = 200
2018-11-16 00:41:15 +01:00
2018-12-22 20:07:47 +01:00
let tests = 0
let channels = 0
let failures = 0
2018-11-16 00:41:15 +01:00
2018-12-22 20:07:47 +01:00
const http = axios.create({ timeout })
http.defaults.headers.common["User-Agent"] = "VLC/2.2.4 LibVLC/2.2.4"
2019-04-23 20:49:52 +02:00
function writeToLog(test, country, msg, url) {
2018-11-16 00:41:15 +01:00
var now = new Date()
2018-12-22 20:07:47 +01:00
var line = `${test}(): ${country}: ${msg} '${url}'`
fs.appendFileSync(path.resolve(__dirname) + '/../' + errorLog, now.toISOString() + ' ' + line + '\n')
console.log(line)
2018-11-16 00:41:15 +01:00
}
function skipPlaylist(filename) {
let test_country = process.env.npm_config_country
if (test_country && filename !== 'channels/' + test_country + '.m3u') {
return true;
}
return false;
}
2019-04-23 20:49:52 +02:00
function loadPlaylist(filename) {
2018-12-22 20:07:47 +01:00
return M3U.parse(fs.readFileSync(path.resolve(__dirname) + "/../" + filename, { encoding: "utf8" }))
}
2018-11-16 00:41:15 +01:00
2018-12-22 20:07:47 +01:00
async function testAllLinksIsWorking() {
2018-11-16 00:41:15 +01:00
2018-12-22 20:07:47 +01:00
tests++
2018-11-16 00:41:15 +01:00
2019-04-23 20:49:52 +02:00
let countries = loadPlaylist('index.m3u')
2018-12-22 20:07:47 +01:00
// countries = countries.slice(0, 2)
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
if (skipPlaylist(country.file)) {
continue;
}
2019-04-23 20:49:52 +02:00
const playlist = loadPlaylist(country.file)
2018-12-22 20:07:47 +01:00
for(let channel of playlist) {
await new Promise(resolve => {
setTimeout(resolve, delay)
})
2018-12-22 20:07:47 +01:00
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
await http.get(channel.file)
continue
} catch (err) {
failures++
2019-04-23 20:49:52 +02:00
writeToLog('testAllLinksIsWorking', country.file, err.message, channel.file)
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
2018-12-22 20:07:47 +01:00
if(failures === 0) {
2018-11-16 00:41:15 +01:00
2018-12-22 20:07:47 +01:00
console.log(`OK (${tests} tests, ${channels} channels)`)
} else {
2018-11-16 00:41:15 +01:00
2018-12-22 22:04:15 +01:00
console.log(`FAILURES! (${tests} tests, ${channels} channels, ${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...')
2018-12-22 20:07:47 +01:00
testAllLinksIsWorking()