mirror of
https://github.com/iptv-org/database.git
synced 2025-05-10 03:30:01 -04:00
Init
This commit is contained in:
commit
26d5bf0436
27 changed files with 43517 additions and 0 deletions
23
scripts/db/export.js
Normal file
23
scripts/db/export.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const { csv } = require('../core')
|
||||
const path = require('path')
|
||||
const glob = require('glob')
|
||||
const fs = require('fs')
|
||||
|
||||
const DATA_DIR = process.env.DATA_DIR || './data'
|
||||
const OUTPUT_DIR = process.env.OUTPUT_DIR || './.gh-pages'
|
||||
|
||||
fs.exists(OUTPUT_DIR, function (exists) {
|
||||
if (!exists) {
|
||||
fs.mkdirSync(OUTPUT_DIR)
|
||||
}
|
||||
})
|
||||
|
||||
glob(`${DATA_DIR}/*.csv`, async function (err, files) {
|
||||
for (const inputFile of files) {
|
||||
const inputFilename = path.parse(inputFile).name
|
||||
const outputFile = `${OUTPUT_DIR}/${inputFilename}.json`
|
||||
|
||||
const json = await csv.load(inputFile)
|
||||
fs.writeFileSync(path.resolve(outputFile), JSON.stringify(json))
|
||||
}
|
||||
})
|
10
scripts/db/schemes/categories.js
Normal file
10
scripts/db/schemes/categories.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
const Joi = require('joi')
|
||||
|
||||
module.exports = {
|
||||
id: Joi.string()
|
||||
.regex(/^[a-z]+$/)
|
||||
.required(),
|
||||
name: Joi.string()
|
||||
.regex(/^[A-Z]+$/i)
|
||||
.required()
|
||||
}
|
27
scripts/db/schemes/channels.js
Normal file
27
scripts/db/schemes/channels.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
const Joi = require('joi')
|
||||
|
||||
module.exports = {
|
||||
id: Joi.string()
|
||||
.regex(/^[A-Za-z0-9]+\.[a-z]{2}$/)
|
||||
.required(),
|
||||
name: Joi.string()
|
||||
.regex(/^[\sa-zA-Z\u00C0-\u00FF0-9-!:&.+'/»#%°$@?()]+$/)
|
||||
.required(),
|
||||
network: Joi.string().allow(null),
|
||||
country: Joi.string()
|
||||
.regex(/^[A-Z]{2}$/)
|
||||
.required(),
|
||||
subdivision: Joi.string()
|
||||
.regex(/^[A-Z]{2}-[A-Z0-9]{1,3}$/)
|
||||
.allow(null),
|
||||
city: Joi.string().allow(null),
|
||||
broadcast_area: Joi.array().items(
|
||||
Joi.string().regex(/^(s\/[A-Z]{2}-[A-Z0-9]{1,3}|c\/[A-Z]{2}|r\/[A-Z0-9]{3,7})$/)
|
||||
),
|
||||
languages: Joi.array()
|
||||
.items(Joi.string().regex(/^[a-z]{3}$/))
|
||||
.allow(''),
|
||||
categories: Joi.array().items(Joi.string().regex(/^[a-z]+$/)),
|
||||
is_nsfw: Joi.boolean().required(),
|
||||
logo: Joi.string().uri().allow(null)
|
||||
}
|
16
scripts/db/schemes/countries.js
Normal file
16
scripts/db/schemes/countries.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
const Joi = require('joi')
|
||||
|
||||
module.exports = {
|
||||
name: Joi.string()
|
||||
.regex(/^[\sA-Z\u00C0-\u00FF().-]+$/i)
|
||||
.required(),
|
||||
code: Joi.string()
|
||||
.regex(/^[A-Z]{2}$/)
|
||||
.required(),
|
||||
lang: Joi.string()
|
||||
.regex(/^[a-z]{3}$/)
|
||||
.required(),
|
||||
flag: Joi.string()
|
||||
.regex(/^[\uD83C][\uDDE6-\uDDFF][\uD83C][\uDDE6-\uDDFF]$/)
|
||||
.required()
|
||||
}
|
6
scripts/db/schemes/index.js
Normal file
6
scripts/db/schemes/index.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
exports.channels = require('./channels')
|
||||
exports.categories = require('./categories')
|
||||
exports.countries = require('./countries')
|
||||
exports.languages = require('./languages')
|
||||
exports.regions = require('./regions')
|
||||
exports.subdivisions = require('./subdivisions')
|
8
scripts/db/schemes/languages.js
Normal file
8
scripts/db/schemes/languages.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
const Joi = require('joi')
|
||||
|
||||
module.exports = {
|
||||
code: Joi.string()
|
||||
.regex(/^[a-z]{3}$/)
|
||||
.required(),
|
||||
name: Joi.string().required()
|
||||
}
|
15
scripts/db/schemes/regions.js
Normal file
15
scripts/db/schemes/regions.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
const Joi = require('joi')
|
||||
|
||||
module.exports = {
|
||||
name: Joi.string()
|
||||
.regex(/^[\sA-Z\u00C0-\u00FF().,-]+$/i)
|
||||
.required(),
|
||||
code: Joi.string()
|
||||
.regex(/^[A-Z]{3,7}$/)
|
||||
.required(),
|
||||
countries: Joi.array().items(
|
||||
Joi.string()
|
||||
.regex(/^[A-Z]{2}$/)
|
||||
.allow('')
|
||||
)
|
||||
}
|
11
scripts/db/schemes/subdivisions.js
Normal file
11
scripts/db/schemes/subdivisions.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
const Joi = require('joi')
|
||||
|
||||
module.exports = {
|
||||
country: Joi.string()
|
||||
.regex(/^[A-Z]{2}$/)
|
||||
.required(),
|
||||
name: Joi.string().required(),
|
||||
code: Joi.string()
|
||||
.regex(/^[A-Z]{2}-[A-Z0-9]{1,3}$/)
|
||||
.required()
|
||||
}
|
84
scripts/db/validate.js
Normal file
84
scripts/db/validate.js
Normal file
|
@ -0,0 +1,84 @@
|
|||
const { logger, file, csv } = require('../core')
|
||||
const { program } = require('commander')
|
||||
const schemes = require('./schemes')
|
||||
const chalk = require('chalk')
|
||||
const Joi = require('joi')
|
||||
|
||||
program.argument('[filepath]', 'Path to file to validate').parse(process.argv)
|
||||
|
||||
async function main() {
|
||||
let errors = []
|
||||
const files = program.args.length
|
||||
? program.args
|
||||
: [
|
||||
'data/categories.csv',
|
||||
'data/channels.csv',
|
||||
'data/countries.csv',
|
||||
'data/languages.csv',
|
||||
'data/regions.csv',
|
||||
'data/subdivisions.csv'
|
||||
]
|
||||
for (const filepath of files) {
|
||||
if (!filepath.endsWith('.csv')) continue
|
||||
const data = await csv.load(filepath)
|
||||
|
||||
const filename = file.getFilename(filepath)
|
||||
|
||||
if (!schemes[filename]) {
|
||||
logger.error(chalk.red(`\nERR: "${filename}" scheme is missing`))
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
let fileErrors = []
|
||||
if (filename === 'channels') {
|
||||
fileErrors = fileErrors.concat(findDuplicatesById(data))
|
||||
}
|
||||
|
||||
const schema = Joi.object(schemes[filename])
|
||||
data.forEach((row, i) => {
|
||||
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, ' ')
|
||||
logger.error(` ${chalk.gray(position)} ${err.message}`)
|
||||
})
|
||||
errors = errors.concat(fileErrors)
|
||||
}
|
||||
}
|
||||
|
||||
if (errors.length) {
|
||||
logger.error(chalk.red(`\n${errors.length} error(s)`))
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue