Update validate.js

what-a-patch-1
Aleksandr Statciuk 4 years ago
parent eaa56dd2a2
commit 1cacb97ea2

@ -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) {
const basename = file.basename(filepath) if (!filepath.endsWith('.m3u')) continue
const [_, country] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
const items = playlist.items const basename = file.basename(filepath)
.map(item => { const [_, countryCode] = basename.match(/([a-z]{2})(|_.*)\.m3u/i) || [null, null]
const details = check(item, country)
return details ? { ...item, details } : null const fileLog = []
const streams = await parser.parsePlaylist(filepath)
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})`
}) })
.filter(i => i) }
items.forEach(item => { if (stream.tvg.id && !api.channels.find({ id: stream.tvg.id })) {
errors.push( fileLog.push({
`${filepath}:${item.line} '${item.details.name}' is on the blocklist due to claims of copyright holders (${item.details.reference})` type: 'warning',
) line: stream.line,
message: `"${stream.tvg.id}" is not in the database`
}) })
} }
}
if (fileLog.length) {
logger.info(`\n${chalk.underline(filepath)}`)
errors.forEach(error => { fileLog.forEach(err => {
logger.error(error) 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}`)
}) })
if (errors.length) { errors = errors.concat(fileLog.filter(e => e.type === 'error'))
logger.info('') warnings = warnings.concat(fileLog.filter(e => e.type === 'warning'))
process.exit(1) }
} }
}
function check(channel, country) { logger.error(
return blocklist.find(item => { chalk.red(
const regexp = new RegExp(item.regex, 'i') `\n${errors.length + warnings.length} problems (${errors.length} errors, ${
const hasSameName = regexp.test(channel.name) warnings.length
const fromSameCountry = country === item.country.toLowerCase() } warnings)`
)
)
return hasSameName && fromSameCountry if (errors.length) {
}) process.exit(1)
}
} }
main() main()

Loading…
Cancel
Save