Small refactoring

This commit is contained in:
freearhey 2019-11-03 20:21:35 +03:00
parent 39a12432d7
commit c67426689b
2 changed files with 36 additions and 54 deletions

View File

@ -136,20 +136,6 @@ helper.filterPlaylists = function(arr, include = '', exclude = '') {
return arr
}
helper.skipPlaylist = function(filename) {
let testCountry = process.env.npm_config_country
let excludeList = process.env.npm_config_exclude
let excludeCountries = excludeList ? excludeList.split(',') : []
if (testCountry && filename !== 'channels/' + testCountry + '.m3u') return true
for(const countryCode of excludeCountries) {
if (filename === 'channels/' + countryCode + '.m3u') return true
}
return false
}
helper.generateTable = function(data, options) {
let output = '<table>\n'
@ -191,6 +177,28 @@ helper.createChannel = function(data) {
})
}
helper.writeToLog = function(country, msg, url) {
var now = new Date()
var line = `${country}: ${msg} '${url}'`
this.appendToFile('error.log', now.toISOString() + ' ' + line + '\n')
}
helper.parseMessage = function(err, u) {
if(!err || !err.message) return
const msgArr = err.message.split('\n')
if(msgArr.length === 0) return
const line = msgArr.find(line => {
return line.indexOf(u) === 0
})
if(!line) return
return line.replace(`${u}: `, '')
}
class Playlist {
constructor(data) {
this.header = data.header

View File

@ -3,33 +3,30 @@ process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
const helper = require('./helper')
const ffmpeg = require('fluent-ffmpeg')
const verbose = process.env.npm_config_debug || false
const errorLog = 'error.log'
const config = {
debug: process.env.npm_config_debug || false,
country: process.env.npm_config_country,
exclude: process.env.npm_config_exclude,
timeout: 10
}
let stats = {
tests: 0,
playlists: 0,
channels: 0,
failures: 0
}
async function test() {
stats.tests++
const playlist = helper.parsePlaylist('index.m3u')
const countries = playlist.items
const countries = helper.filterPlaylists(playlist.items, config.country, config.exclude)
for(let country of countries) {
if (helper.skipPlaylist(country.url)) {
continue
}
stats.playlists++
console.log(`Checking '${country.url}'...`)
console.log(`Processing '${country.url}'...`)
const playlist = helper.parsePlaylist(country.url)
@ -37,9 +34,7 @@ async function test() {
stats.channels++
if(verbose) {
console.log(`Checking '${item.url}'...`)
}
if(config.debug) { console.log(`Checking '${item.url}'...`) }
await new Promise(resolve => {
@ -52,11 +47,13 @@ async function test() {
ffmpeg(item.url, { timeout: 60 }).ffprobe((err) => {
if(err) {
const message = parseMessage(err, item.url)
const message = helper.parseMessage(err, item.url)
stats.failures++
writeToLog(country.url, message, item.url)
helper.writeToLog(country.url, message, item.url)
console.log(`${message} '${item.url}'`)
}
clearTimeout(timeout)
@ -71,11 +68,11 @@ async function test() {
if(stats.failures === 0) {
console.log(`OK (${stats.tests} tests, ${stats.channels} channels)`)
console.log(`OK (${stats.playlists} playlists, ${stats.channels} channels)`)
} else {
console.log(`FAILURES! (${stats.tests} tests, ${stats.channels} channels, ${stats.failures} failures)`)
console.log(`FAILURES! (${stats.playlists} playlists, ${stats.channels} channels, ${stats.failures} failures)`)
process.exit(1)
@ -86,26 +83,3 @@ async function test() {
console.log('Test is running...')
test()
function writeToLog(country, msg, url) {
var now = new Date()
var line = `${country}: ${msg} '${url}'`
helper.appendToFile(errorLog, now.toISOString() + ' ' + line + '\n')
console.log(`${msg} '${url}'`)
}
function parseMessage(err, u) {
if(!err || !err.message) return
const msgArr = err.message.split('\n')
if(msgArr.length === 0) return
const line = msgArr.find(line => {
return line.indexOf(u) === 0
})
if(!line) return
return line.replace(`${u}: `, '')
}