iptv-database/scripts/db/validate.js

213 lines
5.3 KiB
JavaScript
Raw Normal View History

2022-02-11 21:55:50 -05:00
const { logger, file, csv } = require('../core')
const { program } = require('commander')
const schemes = require('./schemes')
const chalk = require('chalk')
const Joi = require('joi')
2022-04-09 07:42:41 -04:00
const _ = require('lodash')
2022-02-11 21:55:50 -05:00
program.argument('[filepath]', 'Path to file to validate').parse(process.argv)
2022-04-09 08:55:35 -04:00
const allFiles = [
'data/blocklist.csv',
'data/categories.csv',
'data/channels.csv',
'data/countries.csv',
'data/languages.csv',
'data/regions.csv',
'data/subdivisions.csv'
]
let db = {}
2022-02-11 21:55:50 -05:00
async function main() {
2022-04-09 08:11:13 -04:00
let globalErrors = []
2022-04-09 08:55:35 -04:00
for (let filepath of allFiles) {
2022-02-11 21:55:50 -05:00
if (!filepath.endsWith('.csv')) continue
2022-02-17 09:33:35 -05:00
const eol = await file.eol(filepath)
2022-04-09 08:11:13 -04:00
if (eol !== 'CRLF') return handleError(`file must have line endings with CRLF (${filepath})`)
2022-02-17 09:33:35 -05:00
const csvString = await file.read(filepath)
2022-04-09 08:11:13 -04:00
if (/\s+$/.test(csvString))
return handleError(`empty lines at the end of file not allowed (${filepath})`)
2022-02-17 09:33:35 -05:00
2022-02-11 21:55:50 -05:00
const filename = file.getFilename(filepath)
2022-04-09 08:55:35 -04:00
let data = await csv
2022-04-09 08:11:13 -04:00
.fromString(csvString)
.catch(err => handleError(`${err.message} (${filepath})`))
2022-02-21 06:07:37 -05:00
2022-04-09 08:55:35 -04:00
switch (filename) {
case 'blocklist':
data = _.keyBy(data, 'channel')
break
case 'categories':
case 'channels':
data = _.keyBy(data, 'id')
break
default:
data = _.keyBy(data, 'code')
break
}
db[filename] = data
}
const toCheck = program.args.length ? program.args : allFiles
for (const filepath of toCheck) {
const filename = file.getFilename(filepath)
if (!schemes[filename]) return handleError(`"${filename}" scheme is missing`)
const rows = Object.values(db[filename])
2022-02-11 21:55:50 -05:00
let fileErrors = []
if (filename === 'channels') {
2022-04-09 08:11:13 -04:00
fileErrors = fileErrors.concat(findDuplicatesById(rows))
2022-04-09 08:23:56 -04:00
for (const [i, row] of rows.entries()) {
2022-04-09 09:05:00 -04:00
fileErrors = fileErrors.concat(await validateChannelBroadcastArea(row, i))
fileErrors = fileErrors.concat(await validateChannelSubdivision(row, i))
2022-04-09 08:55:35 -04:00
fileErrors = fileErrors.concat(await validateChannelCategories(row, i))
fileErrors = fileErrors.concat(await validateChannelLanguages(row, i))
fileErrors = fileErrors.concat(await validateChannelCountry(row, i))
2022-04-09 08:23:56 -04:00
}
2022-04-08 20:44:51 -04:00
} else if (filename === 'blocklist') {
2022-04-09 08:23:56 -04:00
for (const [i, row] of rows.entries()) {
2022-04-09 08:55:35 -04:00
fileErrors = fileErrors.concat(await validateChannelId(row, i))
2022-04-09 08:23:56 -04:00
}
2022-02-11 21:55:50 -05:00
}
const schema = Joi.object(schemes[filename])
2022-04-09 08:11:13 -04:00
rows.forEach((row, i) => {
2022-02-11 21:55:50 -05:00
const { error } = schema.validate(row, { abortEarly: false })
if (error) {
error.details.forEach(detail => {
fileErrors.push({ line: i + 2, message: detail.message })
})
}
})
if (fileErrors.length) {
logger.info(`\n${chalk.underline(filepath)}`)
fileErrors.forEach(err => {
const position = err.line.toString().padEnd(6, ' ')
2022-04-09 08:11:13 -04:00
logger.info(` ${chalk.gray(position)} ${err.message}`)
2022-02-11 21:55:50 -05:00
})
2022-04-09 08:11:13 -04:00
globalErrors = globalErrors.concat(fileErrors)
2022-02-11 21:55:50 -05:00
}
}
2022-04-09 08:11:13 -04:00
if (globalErrors.length) return handleError(`${globalErrors.length} error(s)`)
2022-02-11 21:55:50 -05:00
}
main()
function findDuplicatesById(data) {
data = data.map(i => {
i.id = i.id.toLowerCase()
return i
})
const errors = []
const schema = Joi.array().unique((a, b) => a.id === b.id)
const { error } = schema.validate(data, { abortEarly: false })
if (error) {
error.details.forEach(detail => {
errors.push({
line: detail.context.pos + 2,
message: `Entry with the id "${detail.context.value.id}" already exists`
})
})
}
return errors
}
2022-04-08 21:02:02 -04:00
2022-04-09 08:55:35 -04:00
async function validateChannelCategories(row, i) {
2022-04-09 08:11:13 -04:00
const errors = []
2022-04-09 08:23:56 -04:00
row.categories.forEach(category => {
2022-04-09 08:55:35 -04:00
if (!db.categories[category]) {
2022-04-09 08:23:56 -04:00
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong category "${category}"`
2022-04-09 08:11:13 -04:00
})
2022-04-09 08:23:56 -04:00
}
})
2022-04-09 08:11:13 -04:00
return errors
}
2022-04-09 08:55:35 -04:00
async function validateChannelCountry(row, i) {
2022-04-09 08:11:13 -04:00
const errors = []
2022-04-09 08:55:35 -04:00
if (!db.countries[row.country]) {
2022-04-09 08:23:56 -04:00
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong country "${row.country}"`
2022-04-09 08:11:13 -04:00
})
}
return errors
}
2022-04-09 08:58:52 -04:00
async function validateChannelSubdivision(row, i) {
const errors = []
if (row.subdivision && !db.subdivisions[row.subdivision]) {
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong subdivision "${row.subdivision}"`
})
}
return errors
}
2022-04-09 09:05:00 -04:00
async function validateChannelBroadcastArea(row, i) {
const errors = []
row.broadcast_area.forEach(area => {
const [type, code] = area.split('/')
if (
(type === 'r' && !db.regions[code]) ||
(type === 'c' && !db.countries[code]) ||
(type === 's' && !db.subdivisions[code])
) {
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong broadcast_area "${area}"`
})
}
})
return errors
}
2022-04-09 08:55:35 -04:00
async function validateChannelLanguages(row, i) {
2022-04-09 08:23:56 -04:00
const errors = []
row.languages.forEach(language => {
2022-04-09 08:55:35 -04:00
if (!db.languages[language]) {
2022-04-09 08:23:56 -04:00
errors.push({
line: i + 2,
message: `"${row.id}" has the wrong language "${language}"`
})
}
})
return errors
}
2022-04-09 08:11:13 -04:00
2022-04-09 08:55:35 -04:00
async function validateChannelId(row, i) {
2022-04-09 08:11:13 -04:00
const errors = []
2022-04-09 08:55:35 -04:00
if (!db.channels[row.channel]) {
2022-04-09 08:23:56 -04:00
errors.push({
line: i + 2,
message: `"${row.channel}" is missing in the channels.csv`
2022-04-09 08:11:13 -04:00
})
}
return errors
}
function handleError(message) {
logger.error(chalk.red(`\n${message}`))
process.exit(1)
2022-04-08 21:02:02 -04:00
}