mirror of
https://github.com/iptv-org/epg
synced 2025-12-17 02:47:02 -05:00
Created commands/create-database.js
This commit is contained in:
63
scripts/commands/create-database.js
Normal file
63
scripts/commands/create-database.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const { db, file, parser, logger } = require('../core')
|
||||
const { program } = require('commander')
|
||||
const _ = require('lodash')
|
||||
|
||||
const options = program
|
||||
.option(
|
||||
'--max-clusters <max-clusters>',
|
||||
'Set maximum number of clusters',
|
||||
parser.parseNumber,
|
||||
200
|
||||
)
|
||||
.option('--channels <channels>', 'Set path to channels.xml file', 'sites/**/*.channels.xml')
|
||||
.parse(process.argv)
|
||||
.opts()
|
||||
|
||||
const channels = []
|
||||
|
||||
async function main() {
|
||||
logger.info('Starting...')
|
||||
logger.info(`Number of clusters: ${options.maxClusters}`)
|
||||
|
||||
await loadChannels()
|
||||
await saveToDatabase()
|
||||
|
||||
logger.info('Done')
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
async function loadChannels() {
|
||||
logger.info(`Loading channels...`)
|
||||
|
||||
const files = await file.list(options.channels)
|
||||
for (const filepath of files) {
|
||||
const items = await parser.parseChannels(filepath)
|
||||
for (const item of items) {
|
||||
item.filepath = filepath
|
||||
channels.push(item)
|
||||
}
|
||||
}
|
||||
logger.info(`Found ${channels.length} channels`)
|
||||
}
|
||||
|
||||
async function saveToDatabase() {
|
||||
logger.info('Saving to the database...')
|
||||
|
||||
await db.reset()
|
||||
const chunks = split(_.shuffle(channels), options.maxClusters)
|
||||
for (const [i, chunk] of chunks.entries()) {
|
||||
for (const item of chunk) {
|
||||
item.cluster_id = i + 1
|
||||
await db.insert(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function split(arr, n) {
|
||||
let result = []
|
||||
for (let i = n; i > 0; i--) {
|
||||
result.push(arr.splice(0, Math.ceil(arr.length / i)))
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user