Update validate.js
This commit is contained in:
parent
1e681c897b
commit
e50a478f9f
|
@ -1,55 +1,67 @@
|
||||||
const blocklist = require('../data/blocklist')
|
const { file, logger, api, parser, blocklist } = require('../core')
|
||||||
const parser = require('iptv-playlist-parser')
|
|
||||||
const { file, logger } = require('../core')
|
|
||||||
const { program } = require('commander')
|
const { program } = require('commander')
|
||||||
|
const chalk = require('chalk')
|
||||||
|
|
||||||
const options = program
|
program.argument('<filepath>', 'Path to file to validate').parse(process.argv)
|
||||||
.option('--input-dir <input-dir>', 'Set path to input directory', 'channels')
|
|
||||||
.parse(process.argv)
|
|
||||||
.opts()
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const files = await file.list(`${options.inputDir}/**/*.m3u`)
|
await api.channels.load()
|
||||||
const errors = []
|
|
||||||
for (const filepath of files) {
|
let errors = []
|
||||||
const content = await file.read(filepath)
|
let warnings = []
|
||||||
const playlist = parser.parse(content)
|
for (const filepath of program.args) {
|
||||||
|
if (!filepath.endsWith('.m3u')) continue
|
||||||
|
|
||||||
const basename = file.basename(filepath)
|
const basename = file.basename(filepath)
|
||||||
const [_, country] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
|
const [_, countryCode] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
|
||||||
|
|
||||||
const items = playlist.items
|
const fileLog = []
|
||||||
.map(item => {
|
const streams = await parser.parsePlaylist(filepath)
|
||||||
const details = check(item, country)
|
for (const stream of streams) {
|
||||||
|
const found = blocklist.find(stream.name, countryCode.toUpperCase())
|
||||||
|
if (found) {
|
||||||
|
fileLog.push({
|
||||||
|
type: 'error',
|
||||||
|
line: stream.line,
|
||||||
|
message: `"${found.name}" is on the blocklist due to claims of copyright holders (${found.reference})`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return details ? { ...item, details } : null
|
if (stream.tvg.id && !api.channels.find({ id: stream.tvg.id })) {
|
||||||
|
fileLog.push({
|
||||||
|
type: 'warning',
|
||||||
|
line: stream.line,
|
||||||
|
message: `"${stream.tvg.id}" is not in the database`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileLog.length) {
|
||||||
|
logger.info(`\n${chalk.underline(filepath)}`)
|
||||||
|
|
||||||
|
fileLog.forEach(err => {
|
||||||
|
const position = err.line.toString().padEnd(6, ' ')
|
||||||
|
const type = err.type.padEnd(9, ' ')
|
||||||
|
const status = err.type === 'error' ? chalk.red(type) : chalk.yellow(type)
|
||||||
|
logger.info(` ${chalk.gray(position)}${status}${err.message}`)
|
||||||
})
|
})
|
||||||
.filter(i => i)
|
|
||||||
|
|
||||||
items.forEach(item => {
|
errors = errors.concat(fileLog.filter(e => e.type === 'error'))
|
||||||
errors.push(
|
warnings = warnings.concat(fileLog.filter(e => e.type === 'warning'))
|
||||||
`${filepath}:${item.line} '${item.details.name}' is on the blocklist due to claims of copyright holders (${item.details.reference})`
|
}
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errors.forEach(error => {
|
logger.error(
|
||||||
logger.error(error)
|
chalk.red(
|
||||||
})
|
`\n${errors.length + warnings.length} problems (${errors.length} errors, ${
|
||||||
|
warnings.length
|
||||||
|
} warnings)`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
if (errors.length) {
|
if (errors.length) {
|
||||||
logger.info('')
|
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function check(channel, country) {
|
|
||||||
return blocklist.find(item => {
|
|
||||||
const regexp = new RegExp(item.regex, 'i')
|
|
||||||
const hasSameName = regexp.test(channel.name)
|
|
||||||
const fromSameCountry = country === item.country.toLowerCase()
|
|
||||||
|
|
||||||
return hasSameName && fromSameCountry
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue